task1
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 5 #define N 5 6 7 int main() 8 { 9 int number; 10 int i; 11 12 srand( time(0) );// 以当前系统时间作为随机种子 13 14 for(i=0;i<N;++i){ 15 number=rand()%65+1; 16 printf("20238331%04d\n",number); 17 } 18 19 return 0; 20 }
line15行代码功能是生成一个1~65之间的随机数并存储到number中
整个程序的功能是以系统时间为种子生成5个以“20238331”为前缀,占4位字符的随机数
task2
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5 char C; 6 while(scanf("%c",&C)!=EOF){ 7 getchar(); 8 switch(C){ 9 case 'r':printf("stop!\n");break; 10 case 'g':printf("go go go\n");break; 11 case 'y':printf("wait a minute\n");break; 12 default: printf("something must be wrong\n"); 13 } 14 } 15 return 0; 16 }
task3
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() { 6 int lucky_day, guess, i, correct_guess = 0; 7 srand(time(NULL)); 8 9 lucky_day = rand() % 31 + 1; 10 11 printf("猜猜5月的哪一天是你的lucky day\n"); 12 printf("开始喽,你有三次机会,猜吧(1~31):"); 13 14 for (i = 1; i <= 3; i++) { 15 scanf("%d", &guess); 16 17 if (guess < lucky_day) { 18 printf("你猜的日期早了,你的lucky day还没到呢\n"); 19 printf("再猜(1~31):"); 20 } else if (guess > lucky_day) { 21 printf("你猜的日期晚了,你的lucky day在前面哦\n"); 22 printf("再猜(1~31):"); 23 } else { 24 printf("哇,猜中了:-)\n", lucky_day); 25 correct_guess = 1; 26 break; 27 } 28 } 29 30 if (!correct_guess) { 31 printf("次数用完啦。5月的lucky day是%d号。\n", lucky_day); 32 } 33 34 return 0; 35 }
task4
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5 int a,n,f,i; 6 double s; 7 while(1) 8 {i=1,f=0,s=0; 9 scanf("%d%d",&n,&a); 10 for(;i<=n;i++) 11 { 12 f=f*10+a; 13 s+=1.0*i/f;} 14 printf("n=%d,a=%d,s=%lf\n",n,a,s); 15 } 16 17 return 0; 18 }
task5
1 #include<stdio.h> 2 int main() 3 { 4 int i,j; 5 for(i=1;i<=9;i++) 6 { 7 for(j=1;j<=i;j++) 8 printf("%d*%d=%d\t",j,i,i*j); 9 printf("\n"); 10 } 11 return 0; 12 }
task6
1 #include<stdio.h> 2 int main() 3 { 4 int i,j,n,f; 5 printf("input n:"); 6 scanf("%d",&n); 7 8 for(i=n;i>=1;i--) 9 { 10 for(f=1;f<=n-i;f++) 11 printf("\t"); 12 for(j=1;j<=2*i-1;j++) 13 printf(" O \t"); 14 printf("\n"); 15 16 for(f=1;f<=n-i;f++) 17 printf("\t"); 18 for(j=1;j<=2*i-1;j++) 19 printf("<H>\t"); 20 printf("\n"); 21 22 23 for(f=1;f<=n-i;f++) 24 printf("\t"); 25 for(j=1;j<=2*i-1;j++) 26 printf("I I\t"); 27 printf("\n"); 28 29 } 30 31 return 0; 32 }
标签:main,int,lucky,实验,printf,include,day From: https://www.cnblogs.com/3115735030QQ/p/18135202