第一部分练习:
1.最大公约数(辗转相除法)
#include<stdio.h> int main() { int a = 0, b = 0, c = 0; scanf("%d%d", &a, &b); while (c = a % b) { a = b; b = c; } printf("%d\n", b); return 0; }
2.打印1000年到2000年之间的闰年
#include<stdio.h> int main() { int year = 0; for (year = 1000; year <= 2000; year++) { if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { printf("%d ", year); } } return 0; }
3.打印100~200之间的素数
#include<stdio.h> #include<math.h> int main() { int i = 0; for (i = 101; i <= 200; i += 2) { int j = 0; for (j = 2; j <= sqrt(i); j++) { if (i % j == 0) { break; } } if (j > sqrt(i)) { printf("%d ", i); } } return 0; }
4.将三个整数数按从大到小输出。
#include <stdio.h> int main() { int a = 0 , b = 0 ,c = 0 ; scanf("%d%d%d",&a, &b,&c); if(a<b) { int tmp = a; a = b; b = tmp; } if(a<c) { int tmp = a; a = c; c = tmp; } if(b<c) { int tmp = b; b = c; c = tmp; } printf("a=%d b=%d c=%d\n", a, b, c); return 0; }
5.打印1-100之间所有3的倍数的数字
#include <stdio.h> int main() { int i = 0; for(i=1; i<=100; i++) { if(i%3==0) { printf("%d ", i); } } return 0; }
第二部分练习:
1.计算n的阶乘
#include<stdio.h> int main() { int n = 0, sum = 1; scanf("%d", &n); for (int i = 1; i <= n;i++) { sum *= i; } printf("%d", sum); return 0; }
2.计算1到10的阶乘的和(4037913)(把10改成3查看是否正确)
法一:
#include<stdio.h> int main() { int sum = 0; for (int n = 1;n <= 10;n++) { int ret = 1; for (int i = 1; i <= n;i++) { ret *= i; } sum += ret; } printf("%d", sum); return 0; }
法二(改进):
#include<stdio.h> int main() { int sum = 0, ret = 1; for (int n = 1;n <= 10;n++) { ret *= n;//比如已经计算好2的阶乘,再乘3就是3的阶乘 sum += ret; } printf("%d", sum); return 0; }
3.二分查找
#include<stdio.h> int main() { int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; int sz = sizeof(arr) / sizeof(arr[0]); int left = 0, right = sz - 1 ; int target = 0; scanf("%d", &target); while (left <= right) { int mid = left + ((right - left) / 2); if (arr[mid] < target) { left = mid + 1; } else if (arr[mid] > target) { right = mid - 1; } else { printf("找到了,下标是:%d", mid); break; } } if (left > right) printf("找不到"); return 0; }
4.编写代码,演示字符从两边向中间汇聚
#include<stdio.h> #include<string.h> #include<windows.h> int main() { char arr1[] = "hello world!"; char arr2[] = "############"; int left = 0, right = strlen(arr1) - 1; while (left <= right) { arr2[left] = arr1[left]; arr2[right] = arr1[right]; printf("%s\n", arr2); Sleep(1000);//睡眠一千毫秒 即一秒 S是大写 system("cls");//清空屏幕 left++; right--; } printf("%s\n", arr2); return 0; }
5.编写代码实现,模拟登录情景,只允许输入三次密码
#include<stdio.h> #include<string.h> int main() { char password[20] = { 0 }; //假设正确的密码字符串是123456 int i = 0; int j = 0; for (i = 2; i >=0; i--) { printf("请输入密码:"); scanf("%s", password); //if (password == "123456") 字符串比较不能直接用等号,这种比较是在比较首字符的地址 if (strcmp(password, "123456") == 0) { printf("登陆成功\n"); break; } else { printf("输入错误,还有%d次机会\n",i); } } if(i == -1) printf("密码三次错误,退出程序\n"); return 0; }
6.猜数字游戏
#include<stdio.h> #include<stdlib.h> //rand #include<time.h> //time void menu() { printf("**********************************\n"); printf("********** 猜数字游戏 **********\n"); printf("********** 1.play **********\n"); printf("********** 0.exit **********\n"); printf("**********************************\n"); } void game() { int random_num = rand() % 100 + 1;//模100变成0到99 int input = 0; while (1) { printf("请输入猜的数字(范围是1到100):"); scanf("%d", &input); if (input > random_num) { printf("猜大了\n"); } else if (input < random_num) { printf("猜小了\n"); } else { printf("恭喜你,猜对了\n"); break; } } } int main() { int input = 0; srand((unsigned int)time(NULL)); //随机数种子 do { menu(); printf("请选择>:"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: break; default: printf("选择错误,请重新输入!\n"); break; } } while (input); return 0; }
第三部分练习
1.编写程序数一下 1到 100 的所有整数中出现多少个数字9
#include<stdio.h> int main() { int sum = 0; for (int i = 8;i <= 100;i++) { //if (i % 10 == 9 || i / 10 == 9) 这样写99只算一个9 if (i % 10 == 9) //个位的9 sum++; if (i / 10 == 9) //十位的9 sum++; } printf("有%d个", sum); return 0; }
2.计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值,打印出结果
#include<stdio.h> int main() { double sum = 0; int flag = 1; for (int i = 1; i <= 100; i++) { sum += flag * 1.0 / i; flag = -flag; } printf("%lf\n", sum); return 0; }
3.求10 个整数中最大值
#include<stdio.h> int main() { int arr[10] = { 0 }; for (int i = 0; i < 10; i++) { scanf("%d", &arr[i]); } int max = arr[0]; for (int i = 1; i < 10; i++) { if (arr[i] > max) max = arr[i]; } printf("max = %d\n", max); return 0; }
4.在屏幕上输出9*9乘法口诀表
#include<stdio.h> int main() { for (int i = 1;i <= 9;i++) //外层循环穷举变量i从1至9 { //处理每一行(第i行) for (int j = 1;j <= i;j++) //内层循环输出第i行乘法表(j从1穷举到i共i项) { printf("%d*%d=%2d ", j, i, j * i);//输出一项(第i行第j列) } printf("\n");//输出回车 } return 0; }