1.实验任务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()%65+1; printf("20238331%04d\n",number); } system("pause"); return 0; }
问题1:输出当前系统时间作为随机种子
问题2:生成一个随机学号并进行输出
2.实验任务2
#include<stdio.h> #include<stdlib.h> int main() { char color; printf("输入信号灯颜色:"); while(scanf("%c",&color)!=EOF) { getchar(); switch(color) { case 'r': printf("stop!\n"); break; case 'g': printf("go go go\n"); break; case 'y': printf("wait a minute\n"); break; default: printf("something must be wrong...\n"); break; } } system("pause"); return 0; }
3.实验任务3
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int lucky_day, guess, attempts = 3; time_t t; srand((unsigned) time(&t)); lucky_day = rand() % 31 + 1; printf("猜猜看5月的哪一天是你的幸运日?你有三次机会。\n"); while (attempts > 0) { printf("请输入一个1到31之间的日期:"); scanf("%d", &guess); if (guess < 1 || guess > 31) { printf("输入的日期无效,请输入一个1到31之间的整数。\n"); } else if (guess < lucky_day) { printf("你猜的日期早了,你的幸运日在后面哦。\n"); attempts--; } else if (guess > lucky_day) { printf("你猜的日期晚了,你的幸运日在前面哦。\n"); attempts--; } else { printf("恭喜你,猜中了!5月的第%d天是你的幸运日。\n", lucky_day); break; } } if (attempts == 0 && guess != lucky_day) { printf("很遗憾,你没有猜中。你在5月的幸运日是第%d天。\n", lucky_day); } system("pause"); return 0; }
4.实验任务4
#include<stdio.h> #include<stdlib.h> double dou(int x,int y) { int sum=0; for(int i=1;i<=x;i++) sum=sum*10+y; return sum; } int main() { int a,n; double s; while(scanf("%d%d",&n,&a)!=EOF) { s=0; for(int i=1;i<=n;i++) s=s+i/dou(i,a); printf("n = %d,a = %d,s = %6lf",n,a,s); } system("pause"); return 0; }
5.实验任务5
#include <stdio.h> #include<stdlib.h> int main() { int n; printf("请输入乘法表的最大行数:"); scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { printf("%d×%d=%d\t", j, i, i * j); } printf("\n"); } system("pause"); return 0; }
6.实验任务6
#include<stdio.h> #include<stdlib.h> int main() { int n; printf("input n:"); scanf("%d",&n); for(int i=n;i>=1;i--) { for(int j=n-i;j>0;j--) printf(" "); for(int j=1;j<=2*i-1;j++) printf(" O "); printf("\n"); for(int j=n-i;j>0;j--) printf(" "); for(int j=1;j<=2*i-1;j++) printf("<H> "); printf("\n"); for(int j=n-i;j>0;j--) printf(" "); for(int j=1;j<=2*i-1;j++) printf("I I "); printf("\n"); } system("pause"); return 0; }
标签:guess,int,lucky,实验,printf,include,day From: https://www.cnblogs.com/940804telck/p/18134922