实验二
task -1
#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 5 int main() { int grade, number; int i; srand(time(0)); //设定随机种子数为1 for (i = 0; i < N; ++i) { grade = rand() % 4 + 2018; //生成一个2018-2021之间的随机数 number = rand() % 999 + 1; //生成一个1-999之间的随机数 printf("%d8330%04d\n", grade, number); } return 0; }//程序功能是生成2018-2021级学生的随机学号
task-2
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<time.h> #define N 3 int main() { int i,m,number; printf("猜猜2022年4月哪一天会是你的lucky day\n"); printf("开始喽,你有三次机会,猜吧(1`30):\t"); srand(time(0)); number = rand() % 30+1; for (i = 0; i < N; ++i) { scanf("%d",&m); if (m > number) { printf("你猜的日期晚了,你的lucky day已经过啦\n"); printf("再猜(1`30):"); } else if (m < number) { printf("你猜的日期早了,你的lucky day还没到呢\n"); printf("再猜(1`30):"); } else { printf("哇,猜中了"); break; } } if(i==3) printf("次数用完啦,偷偷告诉你:4月,你的lucky day是%d号\n",number); return 0; }
task-3
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> int main() { char m; while (scanf("%c", &m) != EOF) { if (m == 'r') printf("stop!\n"); else if (m == 'g') printf("go go go\n"); else if (m == 'y') printf("wait a minute\n"); else printf("something must be wrong\n"); getchar(); } return 0; }
task-4
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> int main() { int i,n,a; while (scanf("%d %d",&n,&a) != EOF) { double m = 0.0; double s = 0.0; for (i = 1; i <= n; ++i) { m+=a*pow(10,i-1); s += i / m; } printf("n=%d,a=%d,s=%lf", n, a, s); } return 0; }
task-5
#include<stdio.h> int main() { int i, j,p; for (i = 1; i <= 9; ++i) { for (j = 1; j <= i; ++j) { p = j * i; printf("%d*%d=%2d ", j, i, p); } printf("\n"); } return 0; }
task-6.1
#include<stdio.h> int main() { int i,n; printf("input n:"); scanf_s("%d", &n); for (i = 1; i <= n; ++i) { printf(" O\n"); printf("<H>\n"); printf("I I\n\n"); } }
task-6.2
#include<stdio.h> int main() { int i,n; printf("input n:"); scanf_s("%d", &n); for (i = 1; i <= n; ++i) printf(" O "); printf("\n"); for (i = 1; i <= n; ++i) printf("<H> "); printf("\n"); for (i = 1; i <= n; ++i) printf("I I "); }
task-6.3
#include<stdio.h> int main() { int i,n,m; printf("input n:"); scanf_s("%d", &n); for (n; n >= 0; --n) { m = (6 - n) * 7+1; printf("%*s", m, " "); for (i = 1; i <= 2 * n - 1; ++i) printf(" O "); printf("\n"); m = (6 - n) * 7+1; printf("%*s", m, " "); for (i = 1; i <= 2 * n - 1; ++i) printf("<H> "); printf("\n"); m = (6 - n) * 7+1; printf("%*s", m, " "); for (i = 1; i <= 2 * n - 1; ++i) printf("I I "); printf("\n\n"); } }
实验总结:
1.用scanf()输入函数是一定记得加地址符&
2.每一次的输出printf()要写在循环里
3.rand只能生成一次随机数,srand可生成多次
4.输入字符时要加' ',getchar()可以吞掉空格字符