實驗一
#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; }
line21 生成N2-N1+1~N1之間的隨機數
line25生成N3~1間的隨機數
該程式生成隨機學號
實驗二
#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 i; while(scanf("%c",&i)!=EOF){ if(i=='r'){ printf("stop!\n"); }else if (i=='g'){ printf("go go go\n"); }else if(i=='y'){ printf("wait a minute\n"); }else{ printf("something must be wrong...\n"); } getchar(); } return 0; }
實驗四
#include<stdio.h> int main() { int t,max,min; int sum=0; max=0; min=20000; do { scanf("%d",&t); if (t>=0&&t<=20000) { sum+=t; } if (t>max){ max=t; } if (t<min){ min=t; } }while(t!=-1); printf("今日累計消費總額:%d\n今日最高一筆開銷:%d\n今日最低一筆開銷:%d\n",sum,max,min); return 0; }
實驗五
#include<stdio.h> int main(){ int a,b,c; while(scanf("%d%d%d",&a,&b,&c)!=EOF){ if(a+b<c||b+c<a||a+c<b){ printf("不能構成三角形\n"); }else if(a*a+b*b==c*c||c*c+b*b==a*a||a*a+c*c==b*b){ printf("能構成直角三角形\n"); }else if(a==b&&c==b&&a==c){ printf("能構成等邊三角形\n"); }else if(a==b||c==a||c==b){ printf("能構成等腰三角形\n"); }else { printf("能構成一般三角形\n"); } } return 0; }
實驗六
#include<stdio.h> #include <stdlib.h> #include <time.h> int main(){ int day,x; int k=3; srand(time(NULL)); day=rand()%30 + 1; printf("猜猜2024年11月哪一天是你的lucky day\n開始嘍,你有三次機會(1~30):"); do{ scanf("%d",&x); if (x<day){ k--; printf("你猜的日期早了,你的lucky day還沒到呢\n"); if(k>0) printf("再猜(1~30):"); }else if (x>day){ k--; printf("你猜的日期晚了,你的lucky day在前面哦\n"); if(k>0) printf("再猜(1~30):"); }else { printf("哇,猜中了:)") ; } }while(k>0); if(k==0){ printf("次數用光啦。偷偷告訴你,11月你的lucky day是%d",day); } return 0; }