实验2 c语言分支与循环基础应用编程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; }
问1:生成包括N1到N2的随机数,即计科11,12班的学生学号
问2:生成包括1到N3的随机数,即奇安信班的学生学号
问3:随机专业后随机点名该专业的学生
#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 a; while(scanf("%c",&a)!=EOF){ getchar(); if (a=='r') printf("stop!\n"); else if(a=='y') printf("wait a minute\n"); else if(a=='g') printf("go go go\n"); else printf("something must be wrong\n"); } return 0; }
#include<stdio.h> int main(){ printf("输入一天内的开销,输入-1时终止:\n"); double a=0,max=0,min=1000000,sum=0; while(a!=-1){ scanf("%lf",&a); if(a==-1) break; if(max<a) max=a; else if(min>a) min=a; sum=sum+a; } printf("一天总开销:%lf\n",sum); printf("最高一笔开销:%lf\n",max); printf("最低一笔开销:%lf\n",min); return 0; }
#include<stdio.h> int main(){ int a,b,c,d; while(scanf("%d%d%d",&a,&b,&c)!=EOF){ if(b>a&&b>c){ d=a;a=b;b=d;} else if(c>a&&c>b){ d=a;a=c;c=d;} if(a*a==b*b+c*c) printf("直角"); else if(a>=b+c) printf("不能构成"); else if(b==c&&a==b) printf("等边"); else if(b==c) printf("等腰"); else printf("普通"); } return 0; }
#include <stdio.h> #include <stdlib.h> int main(){ printf("哪一天是lucky day呢(1~30)?\n"); printf("3次机会~\n"); int a,b,n=0; srand(time(NULL)); a=rand()%30+1; while(n!=3){ scanf("%d",&b); if(b<a) printf("早了,还没到~\n"); else if(b>a) printf("晚了,再早点~\n"); else{ printf("wow,猜中!\n"); return 0; } printf("再猜(1~30):"); n++; } printf("次数用尽,你的lucky day是%d号哦~",a); return 0; }