首页 > 其他分享 >实验二

实验二

时间:2024-10-14 21:22:31浏览次数:1  
标签:%. int random else 实验 printf include

任务一:
源代码:
`#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.1:随机选取379到476中的数
问题1.2:随机选取1到21中的数
问题1.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 = bb - 4a*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=='g') printf("go go go\n"); else if(a=='y') printf("wait a minute\n"); else printf("something must be wrong...\n"); } return 0; }
运行结果:

任务四:
源代码:
`#include <stdio.h>
int main(){
double a;
printf("输入今日开销,直到输入-1时终止:\n");
double max=0,min=20000,sum=0;
while(scanf("%lf",&a)!=EOF){
if(a==-1)
break;

if(a>max)
    max=a;
    
if(a<min)
    min=a;

sum+=a;
}
printf("今日累计消费总额:%0.1f\n",sum);
printf("今日最高一笔开销:%0.1f\n",max);
printf("今日最低一笔开销:%0.1f\n",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<a+c&&c<a+b) { if(a==b&&a==c) { printf("等边三角形\n"); } else if(a==b||a==c||b==c) { printf("等腰三角形\n"); } else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a) { printf("直角三角形\n"); } else { printf("普通三角形\n"); } } else { printf("不能构成三角形\n"); } } return0; }
运行结果:

任务六:
源代码:
`#include <stdio.h>

include <stdlib.h>

include <time.h>

int main() {
int x,n;
srand(time(0));
n=rand()%30+1;
printf("猜猜2024年11月哪一天会是你的lucky day\n");
printf("开始喽,你有三次机会,猜吧(1~30):");
for(int i=0;i<3;i++){
scanf("%d",&x);

    if(x==n){
       printf("哇,猜中了:)\n");
       break;
	   }
    else if(x<n){
       printf("你猜的日期早了,你的lucky day还没到呢\n");
	   }
    else if(x>n){
       printf("你猜的日期晚了,你的lucky day在前面哦\n");
	   }
	   
	if(i==0||i==1)
	   printf("再猜(1~30):");
	else
	   printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号",n);       
}
return 0;

}`
运行结果:

标签:%.,int,random,else,实验,printf,include
From: https://www.cnblogs.com/hcjinniuzuo/p/18466176

相关文章

  • 实验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......
  • 实验1 现代C++编程初体验
    任务1:源代码task1.cpp1#include<bits/stdc++.h>2usingnamespacestd;34//模板函数声明5template<typenameT>6voidoutput(constT&c);78//普通函数声明9voidtest1();10voidtest2();11voidtest3();1213intmain(){14cout&l......
  • 实验一
    代码一:迭代器的v.end()是指容器最后一个元素的后面一个代码二:代码三:点击查看代码boolis_palindrome(std::stringt){inti=0,j,flag=0;j=t.size()-1;for(;i<j;i++,j--){if(t[i]!=t[j])flag=1;break;}if(f......
  • 实验1 现代C++编程初体验
    任务11//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#include......
  • 实验1 C++
    task1:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213//普通函数声明......
  • 实验一
    任务一:代码:#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;template<typenameT>voidoutput(constT&c);voidtest1();voidtest2();voidtest3();intmain(){ cout<......
  • 实验1现代c++编程初体验
    1.实验任务一task1.cpp//现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<......
  • 实验4-2-3-for 验证“哥德巴赫猜想C++解法
    #include<iostream>#include<cmath>boolvia(longlongi);usingnamespacestd;intmain(){  longlongn=0,i=3,p=0,q=0,a=0,b=0;  cin>>n;  if(n>4)  {    for(i=3;i<n/2;i+=2)    {......
  • 国防科大:反事实验证LLM在RAG的生成质量
    ......