标签:com loading sqrt 开始 math fun and har else
------------恢复内容开始------------
实验任务1
#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; printf("%3d", x); } printf("\n"); return 0; }
#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; printf("%3d", x); } printf("\n"); return 0;}
实验任务2
#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 ("猜对了"); return 0; } n++; } printf("\n次数用完啦。偷偷告诉你,5月,你的luck day是%d号",x); return 0; }
实验任务3
#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; }
实验任务4
任务四#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); } } }
不能返回,因为得到了两个结果。
实验任务5
#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 p=1,q=0,i; for(i=1;i<=n;i++){ q=q+p; p=(-1)*p*(1/(i+1)); } return q; }
实验任务6
#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; }
标签:com loading sqrt 开始 math fun and har else
原文地址:https://www.cnblogs.com/wzq1117/p/14663848.html