实验一、
实验二
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 1
int main()
{
int n,x,i,a;
srand(time(0));
for(n=1; n<=N; n++) {
x = 1+rand() % 31;
}
printf("猜猜2021年5月哪一天会是你的luck day\n");
printf("开始喽,你有三次机会,猜吧(1~31):");
for(i=1;i<=3;i++)
{
scanf("%d",&a);
printf("\n");
if(a<x)
{
printf("你猜的日期早了,luck day还没到呢\n");
if(i!=3)
{
printf("再猜(1~31):");
}
else
{
printf("次数用完了。悄悄告诉你:5月,你的luck day是%d号",x);
}
continue;
}
else if(a==x)
{
printf("猜对啦");
break;
}
else
{
printf("你猜的日期晚了,luck day悄悄流到前面啦\n");
if(i!=3)
{
printf("再猜(1~31):");
}
else
{
printf("次数用完了。悄悄告诉你:5月,你的luck day是%d号",x);
}
continue;
}
}
return 0;
}
实验四
#include<math.h>
#include<stdio.h>
void solve(double a,double b,double c);
int main()
{
double a,b,c;
printf("Enter a , b , c:");
while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
{
solve(a,b,c);
printf("Enter a , b , c:");
}
return 0;
}
double solve(double a,double b,double c)
{
double x1,x2;
double delta,real,imag;
if(a==0)
printf("not quadratic equation.\n");
else
{
delta = b*b - 4*a*c;
if(delta >= 0)
{
x1 = (-b + sqrt(delta))/(2*a);
x2 = (-b - sqrt(delta))/(2*a);
printf("x1 = %.2f,x2 = %.2f\n",x1,x2);
}
else
{
real = -b/(2*a);
imag = sqrt(-delta)/(2*a);
printf("x1 = %.2f + %.2fi,x2 = %.2f - %.2fi\n",real,imag,real,imag);
}
}
}
//否,返回值只能返回一个值,不可以同时返回x1,x2两个值
// 生成N个0~99之间的随机整数,并打印输出
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
int main() {
int x, n;
srand(time(0)); // 以当前系统时间作为随机种子
for(n=1; n<=N; n++) {
x = rand() % 100; // 生成一个0~99之间的随机整数
printf("%3d", x);
}
printf("\n");
return 0;
}
实验五
#include <stdio.h>
#include <math.h>
double fun(int n); // 函数声明
int main() {
int n;
double s;
printf("Enter n(1~10): ");
while(scanf("%d", &n) != EOF) {
s = fun(n); // 函数调用
printf("n = %d, s= %f\n\n", n, s);
printf("Enter n(1~10): ");
}
return 0;
}
// 函数定义
double fun(int n) {
signed int i=1;
int x;
double a,b,c,d;
d=0;
while(i<=n)
{
b = 1;
a = pow(-1,i-1);
for(x=1;x<=i;x++)
{
b = b*x;
}
c = 1/b;
d = d + a*c;
i++;
}
return d;
}
实验三
#include <stdio.h>
int main()
{
long long s;
int t=0;
int n=1;
int x=1;
printf("Enter a number:");
while(scanf("%lld",&s) != EOF)
{
int y=0;
while(s!=0)
{
n=s%10;
s=s/10;
if(n%2!=0)
t=t*10+n;
}
while(t!=0)
{
x=t%10;
t=t/10;
y=y*10+x;
}
printf("new number is:%d\n\nEnter a number:",y);
}
return 0;
}
1 实验六
2 #include<stdio.h>
3 #include<math.h>
4 unsigned isPrime(int i);
5 int main()
6 {
7 int i,a,b,t,dec;
8 t=1;
9 scanf("%d%d",&a,&b);
10 for(i=a;i<=b;i++)
11 {
12 dec = isPrime(i);
13 if(dec==0)
14 {
15 printf(" %d ",i);
16 if(t%5==0)
17 printf("\n");
18 t++;
19 }
20 }
21 printf("\n\n %d~%d之间素数个数为: %d",a,b,t);
22 return 0;
23 }
24
25 unsigned isPrime(int i)
26 {
27 int y,c=0;
28 double x,z;
29 x = sqrt(i);
30 for(y=2;y<=x;y++)
31 {
32 z = i%y;
33 if(z==0)
34 c++;
35 }
36
37 return c;
38 }
![]()
![]()
![]()
![]()
![]()
![]()