C语言编程小练习4(嵌套循环)

标签: C语言编程小练习4

C语言编程小练习4(嵌套循环)

4.1输入如下图所示的图案(注:星号之间没有空格)
4.1

  1 #include<stdio.h>
  2
  3 int main(void)
  4 {
  5     int i, j;
  6     for (i = 1; i <= 9; i++) {
  7         for (j = 1; j <= 9 - i;j++)
  8             printf(" ");
  9         for (j = 1; j <= i; j++)
 10             printf("*");
 11         printf("\n");
 12     }
 13     return 0;
 14 }

4.2根据输入的行数输出图案。例如:输入的行数为7,则输出如下图所示的图案(注:星号之间没有空格)。

PS:我在(ubuntu )GCC 5.4.0里编译如图代码时,出现以下警告。

具体原因如下:

The abs() function is declared in <stdlib.h> which you’ve not included.

GCC 4.9.2 didn’t complain because the default compilation mode was C89/C90 (-std=gnu89) and functions did not need to be declared before being used in C89 as long as they returned an int, but the default compilation mode was changed to C11 (-stdd=gnu11) in GCC 5.1.0 (see the release notes) and in C11 functions must be declared (or defined) before they are used.

意思就是abs() 函数被声明在stdlib库里,而我没包含进去,改一下就好了;后面是解释GCC版本更新,编译模式改变,新版本在函数使用前必须先被声明。

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3
  4 int main(void)
  5 {
  6     int n, i, j;
  7     printf("Input an odd number: ");
  8     scanf("%d", &n);
  9     for (i = -n / 2; i <= n / 2; i++) {
 10         for (j = 1; j<= n / 2 - abs(i); j++)
 11             printf(" ");
 12         for (j = 1; j <= 2 * abs(i) + 1; j++)
 13             printf("*");
 14         printf("\n");
 15     }
 16     return 0;
 17 }

4.3输入若干大于1的整数,以 -1结束,判断这些数是不是质数。

PS:如果是GCC编译,当源代码其中预处理包含math库,需加上-lm参数链接math库,因为默认GCC不链接math库不加会出现如下错误信息。

  1 #include<stdio.h>
  2 #include<math.h>
  3
  4 int main(void)
  5 {
  6     int m, n, i;
  7     printf("Input some integers > 1 and ending with -1: ");
  8     scanf("%d", &n);
  9     while (n != -1) {
 10         m = (int)(sqrt(n) + 1);
 11         for (i = 2; i <m; i++)
 12             if (n % i == 0)
 13                 break;
 14         if ( i == m)
 15             printf("%d is a prime number.\n", n);
 16         else
 17             printf("%d is not a prime number.\n", n);
 18
 19         scanf("%d", &n);
 20     }
 21     return 0;
 22 }

4.4输出所有的水仙花数。水仙花数是一个三位正整数,每位数字的三次方之和等于该数本身。例如:13+53+33=153,即153是水仙花数。

  1 #include<stdio.h>
  2
  3 int main(void)
  4 {
  5     int i, j, k, m, n;
  6     for (i = 1; i<=9; i++)
  7         for (j = 0; j <= 9; j++)
  8             for(k = 0; k <= 9; k++) {
  9                 m = i * 100 + j * 10 + k;
 10                 n = i * i * i + j * j * j + k * k * k;
 11                 if ( m == n)
 12                     printf("%5d", m);
 13             }
 14     printf("\n");
 15     return 0;
 16 }

4.5口袋里有12个球,3个红色(red)的,4个白色(white)的,5个黑色(black)的,从中任意取8个,列出所有可能的取法。

  1 #include<stdio.h>
  2
  3 int main(void)
  4 {
  5     int red, white, black;
  6     for (red = 0; red <= 3; red++)
  7         for (white = 0; white <= 4; white++)
  8             for(black = 0; black <= 5; black++) {
  9                 if (red + white + black == 8 )
 10                     printf("red: %d, white: %d, black: %d\n", red, white, black );
 11             }
 12     return 0;
 13 }
版权声明:本文为sunshineddMMZ原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sunshineddMMZ/article/details/79677940