首页 > 其他分享 >实验2

实验2

时间:2024-10-15 16:03:22浏览次数:1  
标签:%. int else cost 实验 printf include

实验任务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;

}
line21功能:生成N1-N2的整数
line25功能:生成N2-N3的整数
程序功能:随机生成学号

实验任务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 = %.2g,x2 = %.2g\n",p1+p2,p1-p2);
		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,",p1,p2);
		}
}
return 0;

}
实验任务3

include<stdio.h>

int main()
{
char color;
while((color = getchar()) !=EOF){
if (color == 'r'){
printf("stop!\n");}
else if (color == 'y'){
printf("wait a minute\n");}
else if(color == 'g'){
printf("go go go\n");}
else{
printf("something must be wrong...\n");}
getchar();
}

return 0;

}
实验任务4

include<stdio.h>

int main()
{
printf("输入今日开销,直到输入-1时终止:\n");
double cost, maxcost = 0,mincost = 20000, totalcost = 0;
while(scanf("%lf",&cost) != EOF){
if(cost == -1){
break;}
if(cost > maxcost){
maxcost = cost;
}
if(cost < mincost && cost > 0){
mincost = cost;
}
totalcost += cost;
}
printf("最高一笔开销:%.1lf\n",maxcost);
printf("最低一笔开销:%.1lf\n",mincost);
printf("累计消费总额:%.1lf\n",totalcost);

	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");}
else{
printf("不能构成三角形\n");
return 0;}
if(a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a){
printf("可以构成直角三角形\n");}
else
if(a == b || a == c || b == c){
printf("可以构成等腰三角形\n");}
else
if(a == b == c){
printf("可以构成等边三角形\n");
}
}
return 0;

}
实验任务6

include<stdio.h>

include<stdlib.h>

include<time.h>

int main()
{
int day,luckyday,i = 3;
printf("猜猜2024年11月哪一天会是你的lucky day\n");
printf("开始喽,你有三次机会,猜吧:(1-30)");
srand(time(NULL));
luckyday = rand() % 30 + 1;
while (i > 0);{
scanf("%d",&day);
if(day == luckyday) {
printf("哇,猜中了");
return 0;
}
else if(day < luckyday){
printf("你猜的日期早了,你的lucky day还没到呢\n");
printf("再猜:(1-30)");
}
else{
printf("你猜的日期晚了,你的lucky day在前面呢\n");
printf("再猜:(1-30)");
}

   i--;}
   printf("次数用光啦,悄悄告诉你,你的lucky day是%d\n", luckyday);
   return 0;

}

标签:%.,int,else,cost,实验,printf,include
From: https://www.cnblogs.com/aydfw/p/18467691

相关文章

  • 数据采集实验一
    题目一(1)要求:用requests和BeautifulSoup库方法定向爬取给定网址(http://www.shanghairanking.cn/rankings/bcur/2020)的数据,屏幕打印爬取的大学排名信息。pytho......
  • 程序实际实验1实验报告
    实验任务11#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12voidtest2();13......
  • 实验一
    任务1#include#include#include#includeusingnamespacestd;//声明//模板函数声明templatevoidoutput(constT&c);//普通函数声明voidtest1();voidtest2();voidtest3();intmain(){ cout<<"测试1:\n"; test1(); cout<......
  • jsp高校空闲实验室资源预约管理系统77gmb
    jsp高校空闲实验室资源预约管理系统77gmb本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表项目功能学生,实验室信息,学生预约实验室,教师,设备信息,教师预约实验室,学生取消预约,教师取消预约技术要求:  ......
  • 实验1 现代C++编程初体验
    实验任务1代码:#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;//声明//模板函数声明template<typenameT>voidoutput(constT&c);//普通函数声明voidtest1();voidtest2();voidtest......
  • OpenAI发布了一项实验性框架“Swarm”
      每周跟踪AI热点新闻动向和震撼发展想要探索生成式人工智能的前沿进展吗?订阅我们的简报,深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同,从行业内部的深度分析和实用指南中受益。不要错过这个机会,成为AI领域的领跑者。点击订阅,与未来同行!订阅:https://......
  • 实验1 现代C++编程初体验
    实验任务1:task1.cpp点击查看代码//现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>......
  • 实验二
    任务一:源代码:`#include<stdio.h>include<stdlib.h>include<time.h>defineN5defineN1397defineN2476defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(cnt<N){random_major=rand(......
  • 实验1 现代C++编程初体验
    任务1:源代码task1.cpp1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12voidtes......
  • 实验1 c++
    任务1:task1.cpp://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorit......