实验二
任务一
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 #define R1 586 #define R2 701 int main () { int number; int i; srand(time(0)); for(i=0;i<N;++i){ number=rand()%(R2-R1+1)+R1; printf("20228330%04d\n",number); } return 0; }
line18:生成一个R1到R2之间的随机整数
生成五个末尾从0586到0701的随机学号
任务二
#include <stdio.h> int main () { double x,y; char c1,c2,c3; int a1,a2,a3; scanf ("%d%d%d",&a1,&a2,&a3); printf ("a1=%d,a2=%d,a3=%d\n",a1,a2,a3); scanf ("%c%c%c",&c1,&c2,&c3); printf ("c1=%c,c2=%c,c3=%c\n",c1,c2,c3); scanf ("%lf,%lf",&x,&y); printf ("x=%lf,y=%lf\n",x,y); return 0; }
任务三
#include <stdio.h> #include <math.h> int main () { double x,ans; while (scanf("%lf",&x)!=EOF) { ans=pow(x,365); printf("%.2f的365次方为:%.2f\n",x,ans); } return 0; }
#include <stdio.h> #include <math.h> int main () { double C,F; while (scanf("%lf",&C)!=EOF) { F=C*9/5+32; printf ("摄氏度为%.2f时,华氏度为%.2f\n",C,F); } return 0; }
任务四
#include <stdio.h> int main () { char Y; while (scanf("%c",&Y)!=EOF) { switch (Y) { case 'y':printf ("wait\n"); break; case 'g':printf ("go\n"); break; case 'r':printf ("stop\n"); break; default:printf ("wrong\n"); break; } getchar(); } return 0; }
任务五
#include <stdio.h> #include <stdlib.h> #include <time.h> int main () { printf ("guess which day in 4,2023 will be your lucky day\n"); printf ("let's start the game. you have three chances to try(1~30):\n"); int day,i,a; srand(time(0)); day=rand()%30+1; i=3; while (i>0) { i--; scanf ("%d",&a); if(a>day) { printf ("too late\n"); } if(a==day) { printf ("bingo\n"); break; } if(a<day) { printf ("too early\n"); } } printf ("your lucky day:%d",day); return 0; }
任务六
#include <stdio.h> #include <stdlib.h> int main () { int x,y,a; for (y=1;y<10;y++) { for (x=1;x<=y;x++) { a=x*y; printf ("%d*%d=%d\t",x,y,a); } printf ("\n"); } return 0; }
任务七
#include <stdio.h> #include <stdlib.h> int main () { int x,y,n; scanf ("%d",&y); for(n=1;n<=y;n++) { for(x=1;x<=n-1;x++) { printf("\t"); } for(x=1;x<=(y-n)*2+1;x++) { printf(" O \t"); } printf("\n"); for(x=1;x<=n-1;x++) { printf("\t"); } for(x=1;x<=(y-n)*2+1;x++) { printf("<H>\t"); } printf("\n"); for(x=1;x<=n-1;x++) { printf("\t"); } for(x=1;x<=(y-n)*2+1;x++) { printf("I I\t"); } printf("\n"); } return 0; }
第n行,打印2(y-n)+1个小人,需要2n-2个\t