实验2
task 1
#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 5 int main() { int number; int i; srand(time(0)); for(i=0;i<N;i++); { number=rand()%500+1; printf("20228329%04d\n",number); } system("pause"); return 0; }
line15:从1-500中选择1个根据系统时间的伪随机数 功能:根据生产数推测时间
task 2
#include<stdio.h> #include<stdlib.h> #include<time.h> int main() { int n,m; printf("猜猜2022年11月哪一天会是你的lucky day\n"); printf("开始喽,你有三次机会,猜吧(1~31):"); srand(time(0)); m = rand() % 31 + 1; for(int i=1;i<=3;i++) {scanf_s("%d",&n); if (n < m) { printf("你猜的日期早了,你的lucky day还没到呢\n"); printf("再猜(1~31):"); continue; } if (n == m) { printf("哇,猜中了:-"); break; } else if(n>m) { printf("你猜的日期晚了,你的lucky day已经过啦\n"); printf("再猜(1~31):"); continue; } } if (n != m) { printf("次数用完啦。偷偷告诉你:11月你的lucky day是%d号\n", m); } system("pause"); return 0; }
task 3
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int main() { char ans; while(scanf("%c", &ans)!=EOF){ switch(ans) { case 'r':printf("stop!\n");break; case 'g':printf("go go go\n");break; case 'y':printf("wait a minute\n");break; default:printf("sth must be wrong\n"); break; } getchar(); } system("pause\n"); return 0; }
task 4
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int main() { unsigned int a = 1, n = 1, b = 1; while (scanf("%u%u", &n, &a) != EOF) { double s = 0, i, j; for (i = 1,b=a; i <= n; i++) { j = i / b; s = s + j; b = b * 10 + a; } printf("n=%u,a=%u,s=%lf\n", n, a, s); } system("pause"); return 0; }
task5
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int main() { int a,b,c; for (a = 1; a < 10; a++) { for (b = 1; b <=a; b++) { c = a* b; printf("%dx%d=%d\t",a,b,c); } printf("\n"); } system("pause"); return 0; }
task 6
#include<stdio.h> #include<stdlib.h> int main() { int i, j, x, n; printf("打印多少行"); scanf_s("%d", &n); for (i = 1; i <= n; i++) { for (j = 0; j < i - 1; j++) { printf("\t"); } for (x = 0; x < 2 * (n - i) + 1; x++) { printf(" o \t"); } printf("\n"); for (j = 0; j < i - 1; j++) { printf("\t"); } for (x = 0; x < 2 * (n - i) + 1; x++) { printf("<H>\t"); } printf("\n"); for (j = 0; j < i - 1; j++) { printf("\t"); } for (x = 0; x < 2 * (n - i) + 1; x++) { printf("I I\t"); } printf("\n"); }