实验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); } return 0; }
运行结果
问题1:第十八行代码实现随机生成586到701之间的数
问题2:随机生成5个数字。
实验任务2
实验源代码
#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 = %f , y = %lf\n",x, y); return 0; }
运行结果
实验任务3
实验源代码
#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); printf("\n"); } return 0; }
#include <stdio.h> #include <math.h> int main() { double F, C; while(scanf("%lf", &C) != EOF) { F = 1.8*C + 32; printf("摄氏度C = %.2lf, 华氏度F = %.2lf\n", C, F); printf("\n"); } return 0; }
运行结果
实验任务4
实验源代码
#include<stdio.h> #include<math.h> int main() { char r, g, y, m; while(scanf("%u",&m) != EOF) { m = getchar(); switch(m) { case 'r': printf("stop!");break; case 'g': printf("go go go");break; case 'y': printf("wait a minute");break; default:printf("something must be wrong"); } } return 0; }
运行结果
实验任务5
实验源代码
#include<stdio.h> #include<stdlib.h> int main() { double a, b; int i; a = rand()%(1 + 30 - 1); printf("猜猜2023年4月哪一天会是你的lucky day\n"); printf ("开始喽,你有三次机会,猜吧(1~30): "); scanf("%lf",&b); for(i = 1;i<4;i++) { if( b < a ) { printf("你猜的日期早了,你的lucky day还没到呢\n再猜(1~30):"); scanf("%lf", &b);} else if( b > a) { printf("你猜的日期晚了,你的lucky day已经过啦\n再猜(1~30): "); scanf("%lf",&b);} else { printf("哇,猜中了:-)"); break;} } return 0; }
运行结果
实验任务·6
实验源代码
#include<stdio.h> int main() { int i, j; for(i=1;i<=9;i++) { for(j=1;j<=i;j++) { printf("%d*%d = %d ", j, i, i*j); } printf("\n"); } return 0; }
运行结果
实验任务七
实验源代码
#include<stdio.h> #include<stdlib.h> int main() { int i,j,n; printf("input n:"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i-1;j++) printf("\t"); for(j=1;j<=2*(n-i)+1;j++) printf(" o\t"); printf("\n"); for(j=1;j<=i-1;j++) printf("\t"); for(j=1;j<=2*(n-i)+1;j++) printf("<H>\t"); printf("\n"); for(j=1;j<=i-1;j++) printf("\t"); for(j=1;j<=2*(n-i)+1;j++) printf("I I\t"); printf("\n"); printf("\n"); } return 0; }
运行结果
当输入n时
第i行需要打印2*(n-i)+1个小人
第i行需要打印i-1个空白