实验三

任务一
//
生成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; }

// 生成N个0~31之间的随机整数,并打印输出 
#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() % 32;  // 生成一个0~31之间的随机整数
        printf("%3d", x);
    }
    
    printf("\n");
    
    return 0;
} 

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

 

任务三
#include<stdio.h> int main(){ printf("Enter a number:"); long int m,n,p,q; p=1; q=0; scanf("%ld",&m); while(m!=0){ n=m%10; m=m/10; if(n%2!=0) { q=q+n*p; p=p*10; } } printf("new number is:%d",q); 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; } 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> 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 i,m=1,p=0; for(i=1;i<=n;i++){ m=m*i; p+=1/m; m=m*-1; } return p; }

任务六
#include<stdio.h> #include<math.h> int isPrime(int m); int main(){ int m,p=0,l=0; for(m=100;m<=200;m++){ if(isPrime(m)){ printf("%4d",m); l=l+1; p++; } if(p==5) {printf("\n"); p=0; } } printf("\n100~200之间素数个数为:%d",l); return 0; } int isPrime(int m){ int i,n,q; n=sqrt(m); for(i=2;i<=n;i++){ q=m%i; if(q==0) { return 0; } } }

posted @ 2021-04-14 19:05  孙雪123  阅读(52)  评论(2)    收藏  举报