试验任务1
#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 5 #define N1 397 #define N2 476 #define N3 21 int main(){ int cnt; int random_major,random_no; srand(time(NULL)); cnt = 0; while(cnt<N){ random_major = rand()%2; if(random_major){ random_no = rand()%(N2-N1+1)+N1; printf("20248329%04d\n",random_no); } else{ random_no =rand()%N3 + 1; printf("20248329%04d\n",random_no); } cnt++; } return 0; }
问题一:随机获得N1到N2之间的一个三位数
问题二:随机生成1到21之间的一个整数
问题三:在11班,12班,奇安信班所有学号中随机生成一个学号
实验任务2
#include <stdio.h> #include <math.h> int main() { double a, b, c; double delta, p1, p2; while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) { if(a == 0) { printf("a = 0, invalid input\n"); continue; } delta = b*b - 4*a*c; p1 = -b/2/a; p2 = sqrt(fabs(delta))/2/a; if(delta == 0) printf("x1 = x2 = %.2g\n", p1); else if(delta > 0) printf("x1 = %.2g, x2 = %.2g\n", p1+p2, p1-p2); else { printf("x1 = %.2g + %.2gi, ", p1, p2); printf("x2 = %.2g - %.2gi\n", p1, p2); } } return 0; }
实验任务3
#include<stdio.h> int main() { char a; do{ scanf("%c",&a); getchar(); if (a=='y') { printf("wait a minute\n"); } else if(a=='g') { printf("go go go\n"); } else if(a=='r') { printf("stop!\n"); } else printf("something must be wrong...\n"); } while(1); return 0; }
实验任务4
1 #include<stdio.h> 2 int main() 3 { 4 float sum=0; 5 float num; 6 float min=20000; 7 float max=0; 8 9 printf("ÊäÈë½ñÈÕ¿ªÏú£¬Ö±µ½-1ÖÕÖ¹\n"); 10 11 while(scanf("%f",&num)!=EOF){ 12 13 14 if (num!=-1) 15 { 16 17 sum+=num; 18 19 if(num>max) 20 { 21 max=num; 22 } 23 else 24 { 25 max=max; 26 } 27 if(num<min) 28 { 29 min=num; 30 } 31 else 32 { 33 min=min; 34 } 35 continue; 36 } 37 38 else 39 { 40 break; 41 } 42 } 43 printf("½ñÈÕÀÛ¼ÆÏû·Ñ£º%.1f\n",sum); 44 printf("½ñÈÕ×î¸ß¿ªÏú£º%.1f\n",max); 45 printf("½ñÈÕ×îµÍ¿ªÏú£º%.1f\n",min); 46 47 return 0; 48 49 }
实验任务5
1 #include<stdio.h> 2 int main() 3 { 4 int a,b,c; 5 while(scanf("%d%d%d",&a,&b,&c)!=EOF){ 6 if(a+b<=c||b+c<=a||a+c<=b) 7 { 8 printf("²»Äܹ¹³ÉÈý½ÇÐÎ"); 9 } 10 else if(a==b&&b==c) 11 { 12 printf("µÈ±ßÈý½ÇÐÎ"); 13 } 14 15 16 17 else if(a==b||a==c||b==c) 18 { 19 printf("µÈÑüÈý½ÇÐÎ"); 20 } 21 22 else if (a*a+b*b==c*c||a*a==b*b+c*c||b*b==a*a+c*c) 23 { 24 printf("Ö±½ÇÈý½ÇÐÎ"); 25 } 26 27 28 29 else 30 printf("ÆÕͨÈý½ÇÐÎ"); 31 32 } 33 34 return 0; 35 36 37 38 }
实验任务6
1 int date; 2 #define N1 30 3 int guess; 4 int cnt; 5 srand(time(NULL)); 6 7 date=rand()%30+1; 8 printf("²Â²Â2024Äê11ÔÂÄÄÒ»Ìì»áÊÇÄãµÄlucky day\n"); 9 printf("¿ªÊ¼¿©£¬ÄãÓÐÈý´Î»ú»á£¬²Â°É(1~30):\n"); 10 cnt=0; 11 12 while(cnt<3){ 13 14 scanf("%d",&guess); 15 cnt++; 16 17 if(guess<date) 18 { 19 printf("Äã²ÂµÄÈÕÆÚÔçÁË£¬ÄãµÄlucky day»¹Ã»µ½ÄØ\n"); 20 printf("Ôٲ£¨1~30£©£º\n"); 21 continue; 22 23 } 24 25 else if(guess>date) 26 { 27 printf("Äã²ÂµÄÈÕÆÚÍíÁË£¬ÄãµÄlucky dayÔÚÇ°ÃæÄØ\n"); 28 printf("Ôٲ£¨1~30£©£º\n"); 29 continue; 30 } 31 32 else if(guess==date) 33 { 34 printf("ÍÛ£¬²ÂÖÐÁË\n"); 35 break; 36 } 37 38 39 } 40 while(cnt==3) 41 { 42 printf("´ÎÊýÓùâÀ²£¬ÍµÍµ¸æËßÄ㣬11ÔÂÄãµÄlucky dayÊÇ%d",date); 43 break; 44 } 45 return 0; 46 }
标签:%.,int,else,num,实验,printf,include From: https://www.cnblogs.com/88888888whq/p/18455405