任务1
代码:
#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确保数值在397-476之间
2确保数值在1-21之间
3随机选数
任务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 color;
while(scanf("%c",&color)!=EOF){
if (color=='y')
printf("wait a minute\n");
else if (color=='g')
printf("go go go\n");
else if (color=='r')
printf("stop!\n");
else
printf("something must be wrong\n");
while(getchar()!='\n');
}
return 0;
}
图:
任务4:
代码:
# include<stdio.h>
int main()
{
float a,sum, b=1000000000.0,c=0.0;
while(scanf("%f",&a)!=EOF){
if(a==-1)
break;
if(a>c){
c=a;}
if(a<b)
b=a;
sum+=a;
}
printf("今日累计消费总额:%f\n",sum);
printf("今日最高消费为:%f\n",c);
printf("今日最低消费为:%f\n",b);
return 0;
}
图:
任务5:
代码:
#include<stdio.h>
int main(){
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)!=EOF){
while(a+b<=c||a+c<=b||b+c<=a){
printf("非三角形\n");
break;}
while(a+b>c&&a+c>b&&b+c>a){
if(a==b&&b==c){
printf("等边\n");
break;}
else if(a==b||a==c||b==c){
printf("等腰\n");
break;}
else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a){
printf("直角\n");
break;}
else{
printf("普通\n");
break;}
}
}
return 0;
}
图:
任务6:
代码:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int a,b,c=1;
printf("猜猜2024年11月哪一天是你的幸运日:\n");
printf("开始,你有三次机会(1-30):\n");
srand(time(NULL));
a=rand()%30+1;
while(scanf("%d",&b)!=EOF){
while(c<3){
if(b<a){
printf("早了\n");
break;
}
else if(b>a){
printf("晚了\n");
break;
}
else{
printf("对了\n");
break;
}
c++;
}
while(c>3){
printf("你的幸运日是:%d",a);
break;
}
}
return 0;
}
图: