鱼C论坛

 找回密码
 立即注册
查看: 1860|回复: 4

[已解决]C语言实训题,求助大神

[复制链接]
发表于 2019-1-2 19:48:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
从键盘输入一个奇数n,输出n*n方阵(宫格)
最佳答案
2019-1-2 21:37:27
  1. #include<stdio.h>
  2. #include<windows.h>
  3. #include<stdlib.h>
  4. HANDLE hOut;
  5. void printfj(int);
  6. int color(int);
  7. void printfj(int a)
  8. {
  9.         int i,j;
  10.         color(6);
  11.         for(i=0;i<=a;i++)
  12.         {
  13.                 for(j=0;j<=a;j++)
  14.                 {
  15.                         printf("※");
  16.                 }
  17.                 printf("\n");
  18.         }
  19. }
  20. int color(int c)
  21. {
  22.         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
  23.         return 0;
  24. }
  25. int main(void)
  26. {
  27.         int a;
  28.         color(14);
  29.         printf("请输入一个奇数:");
  30.         scanf("%d",&a);
  31.         if(a%2==0)
  32.         {
  33.                 printf("输入错误输入的是一个偶数!!\n");
  34.         }
  35.         else
  36.         {
  37.                 printfj(a);
  38.         }
  39.         while(1);
  40.         return 0;
  41. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-2 21:37:27 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>
  2. #include<windows.h>
  3. #include<stdlib.h>
  4. HANDLE hOut;
  5. void printfj(int);
  6. int color(int);
  7. void printfj(int a)
  8. {
  9.         int i,j;
  10.         color(6);
  11.         for(i=0;i<=a;i++)
  12.         {
  13.                 for(j=0;j<=a;j++)
  14.                 {
  15.                         printf("※");
  16.                 }
  17.                 printf("\n");
  18.         }
  19. }
  20. int color(int c)
  21. {
  22.         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
  23.         return 0;
  24. }
  25. int main(void)
  26. {
  27.         int a;
  28.         color(14);
  29.         printf("请输入一个奇数:");
  30.         scanf("%d",&a);
  31.         if(a%2==0)
  32.         {
  33.                 printf("输入错误输入的是一个偶数!!\n");
  34.         }
  35.         else
  36.         {
  37.                 printfj(a);
  38.         }
  39.         while(1);
  40.         return 0;
  41. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-2 21:39:06 | 显示全部楼层
看看这个满足你的要求不:

#include “stdio.h”
#include “stdlib.h”
#include “math.h”
int main()
{   
    int a[20][20], n, i, j, m;
    scanf(“%d”, &n);;
    m=n/2;
    for(i=0; i<n; i++)
    for(j=0; j<n; j++)
    {
        if(abs(i-m)>=abs(j-m))
            a[i][j] =abs(i-m)+1;
        else
            a[i][j] =abs(j-m)+1;
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        printf(“%d ”, a[i][j]);
        printf(“\n”);
    }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-3 08:37:34 | 显示全部楼层
行客 发表于 2019-1-2 21:39
看看这个满足你的要求不:

#include “stdio.h”

谢谢大佬,能行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-3 08:38:45 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-16 00:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表