(for述句)韩信点兵中的最大值问题
#include <stdio.h> int main() { int max; printf("MAX = "); scanf("%d", &max); int number; int answer = 0; for (number = max;number >= 1 && answer == 0; --number) { if (number%3 == 2 && number%5 == 3 && number%7 == 2) { answer = number; } } if (answer != 0) { printf("%d\n",answer) ; } return 0; }
韩信点兵找第三个小的练习
#include <stdio.h> int main() { int count = 0; int number; for (number = 1; number <= 1000 && count <3;++number) { if (number%3 == 2 && number%5 == 3 && number%7 ==2) { ++count; if (count == 3) { printf("%d", number); } } } return 0; }
10.6质数判断练习
1.
#include <stdio.h> int main() { int N; printf("N = "); scanf("%d", &N); int number; for (number = 2; number < N && N % number != 0; ++number);
if (number == N) { printf("Yes\n"); }else { printf("No\n"); } return 0; }
N = 7
Yes
Yes
Process returned 0 (0x0) execution time : 2.835 s
Press any key to continue.
Press any key to continue.
2.
#include <stdio.h> int main() { int N; printf("N = "); scanf("%d", &N); int isPrime = 1; int number; for (number = 2; number < N && isPrime; ++number)//这里的&& isPrime 可以省略 { if (N % number == 0) { isPrime = 0; } } if (isPrime) { printf("Yes\n"); } else { printf("No\n"); } return 0; }
11.1 9x9连乘(9X9的正方形)
#include <stdio.h> int main() { int i,j; for (i =2; i <= 9; ++i) { for (j = 1; j <= 9; ++j) { printf("%d x %d = %d\n",i,j, i*j); // printf("*"); }// printf("\n"); } return 0; }
11.2 用文字绘制空心长方形的练习
# include <stdio.h> int main() { int N; printf("N = "); scanf("%d", &N); int i,j; for (i = 1; i <= N; ++i) { for (j = 1; j <= N; ++j) { if (i == 1 || i == N || j == 1 || j == N) // 这一步操作使它空心 { printf("*"); } else { printf(" "); } } printf("\n"); } return 0; }
N = 7
*******
* *
* *
* *
* *
* *
*******
*******
* *
* *
* *
* *
* *
*******
Process returned 0 (0x0) execution time : 3.764 s
Press any key to continue.
Press any key to continue.
三角形
# include <stdio.h> int main() { int N; printf("N = "); scanf("%d", &N); int i,j; for (i = 1; i <= N; ++i) { for (j = 1; j <= i; ++j) { printf("*"); } printf("\n"); } return 0; }
N = 7
*
**
***
****
*****
******
*******
*
**
***
****
*****
******
*******
Process returned 0 (0x0) execution time : 3.901 s
Press any key to continue.
Press any key to continue.
11.4 用文字绘制三角形的练习
# include <stdio.h> int main() { int N; printf("N = "); scanf("%d", &N); int i,j; for (i = 1; i <= N; ++i) { for (j = 1; j <= i; ++j) { printf("*"); } printf("\n"); } return 0; }
印 1 个星星 + 换行
印 1 个星星 + i个空白 + 1 个星星 + 换行 (i=0)
印 1 个星星 + i个空白 + 1 个星星 + 换行 (i=1)
印 1 个星星 + i个空白 + 1 个星星 + 换行 (i=2)
印 1 个星星 + i个空白 + 1 个星星 + 换行
11.5 基于坐标法用文字绘制三角形
#include <stdio.h> int main() { int N; printf("N = "); scanf("%d", &N); int i,j; for (i = 1; i <= N; ++i) { for (j = 1; j <= N; ++j) if (i+j >= N+1) { printf("*"); }else { printf(" "); } printf("\n"); } return 0; } * ** *** **** ***** ****** *******
// 可以有多种变化,只要看i,j变化
11.6 找简易方程式整数解(两种方法)
#include <stdio.h> int main() { int i,j; for (i = 1; i <= 30; ++i) { for (j =1; j <= 30; ++j) if (i+j == 30 && i*j == 221) { printf("%d, %d\n",i,j); } } return 0; } #include <stdio.h> int main() { int i; for (i=0;i <= 30/2; ++i) { int j = 30 - i; if (i*j == 221) { printf("%d, %d\n", i,j); } } return 0; }