实验三

// 生成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 <stdlib.h>
#include <time.h>
#define N 1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 1

int main() {
	int x,n,i; 
	
	srand(time(0));
	x = rand() % 32;  
	printf("猜猜2021年5月的哪一天会是你的luck day\n");
	printf("开始喽,你有三次机会,猜吧(1~31):");
	scanf("%d",&n);
	for(i=1;i<3;++i){
		if(n>x){
			printf("你猜的日期晚了,luck day悄悄溜到前面啦\n");
		    printf("再猜(1~31)");
			scanf("%d",&n);
		    continue;}
		if(n<x){
			printf("你猜的日期早了,luck day还没到呢\n");
		    printf("再猜(1~31)");
			scanf("%d",&n);
		    continue;}
	    if(n=x){
			 printf("恭喜你猜对了\n");
			 break;}
	}
	if(n>x)printf("你猜的日期晚了,luck day悄悄溜到前面啦\n");
	else if(n<x)printf("你猜的日期早了,luck day还没到呢\n");
	else return 0;
	printf("次数用完啦。偷偷告诉你:5月,你的luck day是%d号\n",x);
		return 0;
} 

#include<stdio.h>
#define EOF -1
int main(){
	int i,m,t,n,y;
	printf("Enter a number:");
	while(scanf("%d",&i)!= EOF)
    {   t=y=0;
		while(i!=0){
		m=i%10;
		t=t*10+m;
		i=i/10;
	}
    while(t!=0){
		n=t%10;
		if(n%2!=0)
			{y=y*10+n;
		     t=t/10;
		     continue;}
		else {t=t/10;
		continue;}
	}
	printf("new number is:%d\n",y);
	printf("Enter a number:");
	}
	return 0;
}

// 一元二次方程求解(函数实现方式)
// 重复执行, 直到按下Ctrl+Z结束 

#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;
}

// 函数定义
// 功能:求解一元二次方程,打印输出结果
// 形式参数:a,b,c为一元二次方程系数 
void 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);
		}
	}	
}

#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) {
	// 补足代码
	// ××× 
	int i;
	double m=0.0,t=1.0;
	for(i=1;i<=n;++i){
		if(i%2!=0)t=sqrt(t*t)*i;
		else t=t*-i;
		m=m+1.0/t;}
	return m;
}

#include<stdio.h>
#include<math.h>
int isprime(int n);
int main()
{
	int i,y=0,t=0;
	for(i=101;i<=200;i++)
	{ 
		if(isprime(i))
			{y++;
			 printf("%4d",i);
			if(y%5==0)
			printf("\n");
			t=t+1;
			 } 
	}
	printf("\n\n");
	printf("100~200之间的素数个数为:%d",t);
	printf("\n");
	return 0;
}
int isprime(int n)
{
	int k;
	for(k=2;k<=sqrt(n);k++)
		if(n%k==0)
			return 0;
	    return 1;
}

  

posted @ 2021-04-14 23:20  轻秋  阅读(65)  评论(1)    收藏  举报