实验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
将随机数限制在R1和R2之间
问题2
随机生成五个在586和701之间的数字,并以202283300xxx的形式输出
实验任务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("%s%s%s", &c1, &c2, &c3); printf("c1 = %c, c2 = %c, c3 = %c\n", c1,c2,c3); scanf("%lf,%f", &x, &y); printf("x = %lf, y = %lf\n", x,y); system("pause"); return 0; }

任务3
#include <stdio.h> #include<stdlib.h> int main() { float temp; float result; printf("请输入温度值:"); scanf("%f", &temp); result = temp * 1.8 + 32; printf("%.2f摄氏度 = %.2f华氏度\n", temp, result); system("pause"); return 0; }

任务4
#include <stdio.h> int main() { char color; printf("Please input a traffic light color (r, g or y): \n"); while (scanf("%c", &color) != EOF) { if (color == 'r') { printf("Stop!\n"); } else if (color == 'g') { printf("Go go go!\n"); } else if (color == 'y') { printf("Wait a minute.\n"); } else { printf("Something must be wrong...\n"); } getchar(); } return 0; }

任务5
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int lucky_day; int guess; int chance = 3; srand((unsigned)time(NULL)); lucky_day = rand() % 30 + 1; printf("猜一下吧,开始咯,三次机会(1~30): \n"); while (chance > 0) { scanf("%d", &guess); if (guess == lucky_day) { printf("哇,猜中了👍:(\n"); break; } else if (guess > lucky_day) { printf("晚了👎\n", --chance); } else { printf("早了👎\n", --chance); } } if (chance == 0) { printf("👎👎👎 %d.\n", lucky_day); } system("pause"); return 0; }


任务6
#include <stdio.h> #include<stdlib.h> int main() { int i, j; for (i = 1; i <= 9; i++) { for (j = 1; j <= i; j++) { printf("%d*%d=%-2d ", j, i, i*j); // %-2d 表示左对齐,占2个字符位置 } printf("\n"); } system("pause"); return 0; }

任务7
#include<stdio.h> #include<stdlib.h> int main() { printf("请输入行数:"); int n,i,y,v,a=1; scanf("%d",&n); for(i=n;i>=1;i--){ for(v=1;v<a;v++) printf("\t"); for(y=1;y<=(2*i-1);y++){ printf(" O \t"); } printf("\n"); for(v=1;v<a;v++) printf("\t"); for(y=1;y<=(2*i-1);y++){ printf("<H>\t"); } printf("\n"); for(v=1;v<a;v++) printf("\t"); for(y=1;y<=(2*i-1);y++){ printf("I I\t"); } printf("\n"); a++; } system("pause"); return 0; }


浙公网安备 33010602011771号