类似◇形的图形,四边都是对应的行数的数字。

本文介绍了一个使用 C 语言编写的程序,该程序可以接收用户输入的一个介于1到35之间的数字,并根据此数字打印出特定的数字图案。程序通过双重循环实现了图案的上半部分和下半部分的打印,当输入的数字小于等于9时直接打印数字,大于9时则转换为字母进行打印。

#include <stdio.h>


int main()
{
 int row;
 printf("请输入1~35的数字!!!\n");
 label1:
 scanf("%d",&row);
 if (row>35||row<1)
 {
  printf("亲,你超过范围了,请重新输入:\n");
  goto label1;
 }

 //打印上半部分
 for(int i=0;i<=row;++i){
  for(int j=0;j<=row-i;++j)
   printf(" ");
  for(int k=0;k<=2*(i-1);++k)
  {
   if ((k==0)||(k==2*(i-1)))
   {
    if (i<=9)
    {
     printf("%d",i);
    }
    else
    {
     printf("%c",i+55);
    }
   }
   else
    {
     printf(" ");
    }
  }
  printf("\n");
 }
//打印下半部分
 for( i=row-1;i>=0;--i){
  for(int j=0;j<=row-i;++j)
   printf(" ");
  for(int k=0;k<=2*(i-1);++k)
  {
   if ((k==0)||(k==2*(i-1)))
   {
    if (i<=9)
    {
     printf("%d",i);
    }
    else
    {
     printf("%c",i+55);
    }
   }
   else
    {
     printf(" ");
    }
  }
  printf("\n");
 }

}

 

运行结果:

转载于:https://www.cnblogs.com/wangliangliang/p/3196953.html

clear; clc; close all; f% 螺旋置乱函数 function outputMatrix = spiralScramble(inputMatrix) % 获取输入矩阵的行数和列数 [rows, cols] = size(inputMatrix); % 检查输入矩阵是否为方阵 if rows ~= cols error('输入矩阵必须是方阵!'); end % 初始化输出矩阵,初始值输入矩阵相同 outputMatrix = inputMatrix; % 计算矩阵的层数,层数由矩阵边长决定 numLayers = ceil(rows / 2); % 遍历每一层进行螺旋置乱 for layer = 1:numLayers % 计算当前层的起始和结束索引 startIdx = layer; endIdx = rows - layer + 1; % 提取当前层的四条边元素 % 上边(从左到右) topRow = outputMatrix(startIdx, startIdx:endIdx); % 右边(从上到下,不包含上下端点) rightCol = outputMatrix(startIdx + 1:endIdx - 1, endIdx); % 下边(从右到左) bottomRow = fliplr(outputMatrix(endIdx, startIdx:endIdx)); % 左边(从下到上,不包含上下端点) leftCol = fliplr(outputMatrix(startIdx + 1:endIdx - 1, startIdx)); % 将四条边元素组合成一个向量 combinedElements = [topRow, rightCol', bottomRow, leftCol']; % 反转组合后的向量 reversedElements = fliplr(combinedElements); % 将反转后的元素重新填充回矩阵的当前层 % 填充上边 outputMatrix(startIdx, startIdx:endIdx) = reversedElements(1:length(topRow)); % 填充右边 outputMatrix(startIdx + 1:endIdx - 1, endIdx) = reversedElements(length(topRow) + 1:length(topRow) + length(rightCol))'; % 填充下边 outputMatrix(endIdx, startIdx:endIdx) = fliplr(reversedElements(length(topRow) + length(rightCol) + 1:length(topRow) + length(rightCol) + length(bottomRow))); % 填充左边 outputMatrix(startIdx + 1:endIdx - 1, startIdx) = fliplr(reversedElements(length(topRow) + length(rightCol) + length(bottomRow) + 1:end))'; end end % 示例使用 % 创建一个 4x4 的示例矩阵 exampleMatrix = reshape(1:16, 4, 4)'; % 显示原始矩阵 disp('原始矩阵:'); disp(exampleMatrix); % 调用螺旋置乱函数 scrambledMatrix = spiralScramble(exampleMatrix); % 显示置乱后的矩阵 disp('螺旋置乱后的矩阵:'); disp(scrambledMatrix);这个代码有问题,请修改后输出给我
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值