ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验3

2021-04-11 16:35:00  阅读:84  来源: 互联网

标签:return int double num 实验 printf include


// 生成N个0~99之间的随机整数,并打印输出
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10

int main() {
int x, n;

srand(time(0)); // 以当前系统时间作为随机种子

for(n=1; n<=N; n++) {
x = 1+rand() %31; // 生成一个1~31之间的随机整数
printf("%3d", x);
}

printf("\n");

return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int x,y1,y2,y3;
srand(time(0));
x=1+rand()%31;
printf("猜猜五月的幸运日是几号?\n");
printf("你有三次机会,1~31 :");
scanf("%d",&y1);
if(y1==x) printf("恭喜你,猜中了!");
else {if(y1>x) printf("你猜晚了呦,再试试吧\n");
if(y1<x) printf("猜早了呢,再试试吧\n");
scanf("%d",&y2);
if(y2==x)printf(" 你猜对了!");
else{
if(y2>x)printf("你猜晚了呦,再试试吧\n");
if(y2<x)printf("猜早了呢,再试试吧\n");
scanf("%d",&y3);
if(y3==x)printf("你终于猜对了");
else {if(y3>x)printf("你猜晚了呦,再试试吧\n");
if(y3<x)printf("猜早了呢,再试试吧\n");
printf("你没机会了,偷偷告诉你,你的lucky day 是%d号",x);

}
}


}
return 0;

}

#include<stdio.h>
#include<math.h>
int main(){
int i;
for(i=1;i<=3;i++){
long int num,r=0;
printf("Enter a number : ");
scanf("%d",&num);
int j=0;
while(num){
int k;
k=num%10;
if(k%2){
r+=k*pow(10,j);
j++;
}
num=num/10;
}
printf("new number is :%d\n",r);
}
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) {
double a=0;
for(int i=1;i<=n;i++){
int c=1;
for(int k=1;k<=i;k++)
c*=k;
a+=pow(-1,i-1)/c;
}
return a;
}

 

#include<stdio.h>
#include<math.h>
int isPrime(int n){
int n1,c=0;
n1=sqrt(n);
for(int j=2;j<=n1;j++){
if(n%j==0){
c=1;
break;}
}
return c;
}
int main(){
int num=0;
for(int i=101;i<=200;i++){
if(isPrime(i)==0){
printf("%d ",i);
num++;
if(num%5==0)printf("\n");
}
}
printf("101~200之间素数的个数为: %d",num);
return 0;
}

标签:return,int,double,num,实验,printf,include
来源: https://www.cnblogs.com/zxynb/p/14644264.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有