实验2——c语言分支与循环基础应用编程
task1
#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 5 #define N1 374 #define N2 465 int main() { int number; int i; srand(time(0)); for (i = 0; i < N; i++) { number = rand() % (N2 - N1 + 1) + N1; printf("202383290409%04d\n", number);/*那个是谁的学号:)*/ } return 0; } /*随机生成五个数,这些数在N1到0500之内*/ /*功能:在统一学号后生成五个随机数,阿是班号还是抽签顺序?*/
task2
#include<stdio.h>; #include<stdlib.h>; int main() { char n; while (scanf("%c", &n) != EOF) { getchar();/*不要再忘了!!!*/ switch (n) { case 'y': printf("wait a minute\n"); break; case 'g': printf("go go go\n"); break; case 'r': printf("stop!\n"); break; default: printf("something must be wrong...\n"); } } system("pause"); return 0; }
task3
#include<stdlib.h> #include<stdio.h> #include<time.h> int main() { int a;/*随机数*/ int n = 0; int i; srand((unsigned)time(NULL));/*通过 srand() 函数来重新“播种”*/ a = rand()%30+1; /*可以看成两个部分:rand()%30是产生0~29的随机数,后面的+1保证a最小只能是1*/ printf("赛博神婆上线,输入1~30中的一个数字查询您的幸运日~\n"); printf("但是您只有三次机会~\n"); for (i = 1; i <= 3; i++) { printf("深呼吸,与赛博宇宙进行能力量交换,追随心中的意志输入一个数字:\n"); scanf("%d", &n); if (n < a) { printf("早咯~\n"); printf("机会只剩下%d咯~~~\n", 3 - i); } else if (n > a) { printf("晚咯~\n"); printf("机会只剩下%d咯~~~\n", 3 - i); } else if (n == a) { printf("猜对咯!!!\n"); exit(0); } } printf("其实每天都是您的幸运日!"); system("pause"); return 0; }
task4
#include<stdio.h> #include<stdlib.h> double sum10_16(int n, int a); int main() { int n = 0, a = 0; while (scanf("%d", &n) != EOF) { scanf("%d", &a); printf("%lf\n", sum10_16(n, a)); } system("pause"); return 0; } double sum10_16(int n, int a) { double s = 0; double b = 1; double c = 1; int i; for (i = 1; i <= n; i++) { s = s + i/(a*c); c = c * 10 + 1; } return s; }
task5
#include<stdio.h> #include<stdlib.h> int main() { int column, line; for (column = 1; column <= 9; column++) { for (line = 1; line <= column; line++) { printf("%dx%d=%3d ", line, column, line * column); } printf("\n"); } system("pause"); return 0; }
task6
#include<stdio.h> #include<stdlib.h> int main() { int n = 0; int i; printf("输入您需要的小人行数:"); scanf("%d", &n); for (i = 1; i <= n; i++) { int l; for (l = 1; l <= 2 * i - 2; l++) { printf(" "); } int head; for (head = 1;head <= 2 * n - 2 * i + 1; head++) { printf(" O "); } printf("\n"); for (l = 1; l <= 2 * i - 2; l++) { printf(" "); } int body; for (body = 1; body <= 2 * n - 2 * i + 1; body++) { printf("<H> "); } printf("\n"); for (l = 1; l <= 2 * i - 2; l++) { printf(" "); } int leg; for (leg = 1; leg <= 2 * n - 2 * i + 1; leg++) { printf("I I "); } printf("\n"); } system("pause"); return 0; }
注意点:
1.getchar()
2.EOF——— -1
3.时间种子(time待看)
4.srand用法
5.实验六待改进(用递归做)