实验2
实验任务1
程序代码:
#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);} system("pause"); return 0;}
运行结果:
回答1:随机生成一个586到701的整数;
回答2:随机生成五个202283300586到202283300701中的数;
实验任务2
程序代码:
#include <stdio.h>
#include <stdlib.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=%f,y=%lf\n",x,y);
system("pause");
return 0;
}
运行结果:
实验任务3
task3_1.c
程序代码:
#include <stdio.h> #include <stdlib.h> #include <math.h> int main() {double x,ans; scanf("%lf",&x); ans=pow(x,365); printf("%.2f的365次方:%.2f\n",x,ans); system("pause"); return 0;}
运行结果:
task3_2.c
程序代码:
#include <stdio.h> #include <stdlib.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); } system("pause"); return 0; }
运行结果:
task3_3.c
程序代码:
#include <stdio.h> #include <stdlib.h> int main() {double C,F; while(scanf("%lf",&C)!=EOF) {F=9*C/5+32; printf("摄氏度C=%.2lf时,华氏度F=%.2lf\n",C,F); } system("pause"); return 0; }
运行结果:
实验任务4
程序代码:
#include <stdio.h> #include <stdlib.h> int main() {char color; while(scanf("%c",&color)!=EOF) {switch(color){ case 'r':printf("stop!\n");break; case 'g':printf("go go go\n");break; case 'y':printf("wait a minute\n");break; case '\n':break; default:printf("something must be wrong\n"); }} system("pause"); return 0; }
运行结果:
实验任务5
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{int m,x,i;
srand(time(0));
m=rand()%30+1;
printf("猜猜2023年4月哪一天会是你的luck day\n开始喽,你有三次机会,猜吧(1~30):");
scanf("%d",&x);
for(i=1;i<3;scanf("%d",&x)){
if (x==m)
{printf("你猜中了!\n");break;}
else{if (x>m)
{printf("你猜的日期晚了,你的luck day已经过啦\n再猜(1~30):");}
else
{printf("你猜的日期早了,你的luck day还没到呢\n再猜(1~30):");}
i++;}
}
if (i==3)
printf("次数用完啦。偷偷告诉你:4月,你的luck day是%d号\n",m);
else ;
system("pause");
return 0;
}
运行结果:
实验任务6
程序代码:
#include <stdio.h> #include <stdlib.h> int main() {int c,l; for(c=1;c<=9;c++){ for(l=1;l<=c;l++){ printf("%dx%d=%2d ",l,c,l*c); } printf("\n"); } system("pause"); return 0; }
运行结果:
实验任务7
程序代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{int n,c,l,j,k;
printf("input n:");
scanf("%d",&n);
k=2*n-1;
for(c=1;c<=n;c++){
for(l=1;l<=(k-(2*(n-c+1)-1))/2;l++){
printf(" ");}
for(l=1;l<=2*(n-c+1)-1;l++){
printf(" 0 ");}
printf("\n");
for(l=1;l<=(k-(2*(n-c+1)-1))/2;l++){
printf(" ");}
for(l=1;l<=2*(n-c+1)-1;l++){
printf("<H> ");}
printf("\n");
for(l=1;l<=(k-(2*(n-c+1)-1))/2;l++){
printf(" ");}
for(l=1;l<=2*(n-c+1)-1;l++){
printf("I I ");}
printf("\n");}
system("pause");
return 0;
}
运行结果:
回答:当输入为n时,
第i行,需要打印2*(n-i+1)-1个字符小人
第i行,前面需要打印i-1个空白