实验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("20248395%04d\n", random_no); } cnt++; } return 0; }
问题一:使生成的随机数在N1,N2范围内
问题二:使生成的随机数在N3范围内
问题三:随机抽取学号
实验二
#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; }
实验三
#include<stdio.h> int main(){ char color; 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; }
实验四
#include<stdio.h> int main(){ double ex,maxex=0.0,minex=20000.0,total=0.0; printf("输入一天内的若干笔开销,输入-1时终止:\n"); while(1){ scanf("%lf",&ex); if(ex == -1) { break;} total+=ex; if(ex>maxex) maxex = ex; if(ex<minex) minex = ex; } printf("今日消费总额:%.1lf\n",total); printf("今日最高一笔开销:%.1lf\n",maxex); printf("今日最低一笔开销:%.1lf\n",minex); return 0; }
实验5
#include<stdio.h> int main(){ int a,b,c; while(scanf("%d%d%d",&a,&b,&c)!=EOF){ if((a+b)<=c||(a+c)<=b||(b+c)<=a) { printf("不能构成三角形\n"); continue;} if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(b*b+c*c==a*a)) { printf("直角三角形\n"); continue;} if(a==b&&c==b) { printf("等边三角形\n"); continue; } if(a==b||a==c||b==c) { printf("等腰三角形\n"); continue;} if((a+b>c)&&(a+c>b)) printf("普通三角形\n"); } return 0; }
实验6
#include<stdio.h> #include<stdlib.h> #include<time.h> int main(){ int i,x; srand(time(NULL)); int j = rand()%30+1; printf("猜猜2024年11月那一天会是你的lucky day\n"); printf("开始喽,你有三次机会,猜吧(1~30):"); for(i = 0;i<2;i++){ scanf("%d",&x); if(j==x) { printf("哇,猜中了:)\n"); return 0;} else if(j>x) { printf("你的日期猜早了,你的lucky day在后面哦\n再猜(1~30):"); } else if(j<x) printf("你的日期猜晚了,你的lucky day还没到呢\n再猜(1~30):"); } for(;i<3;i++){ scanf("%d",&x); if(j==x) { printf("哇,猜中了:)\n"); return 0;} else if(j>x) printf("你的日期猜早了,你的lucky day在后面哦\n"); else if(j<x) { printf("你的日期猜晚了,你的lucky day还没到呢\n");} } printf("次数用光了。偷偷告诉你,11月你的lucky day是%d号\n",j); return 0; }
标签:%.,int,random,else,实验,printf,include From: https://www.cnblogs.com/xia2005/p/18462165