ex3
作者:互联网
task1
#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 ; }
task2
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 1 int main(){ int random , n , x, i; srand(time(0)); for(n=1 ; n<=N; n++){ random = rand() % 30 + 1; } printf("猜猜2021年五月哪一天是你的幸运日\n只有三次机会哦,开始吧:\n"); for(i=1; i<=3 ;i++){ scanf("%d",&x); if(x < random) printf("日期早咯,luck day跑到后面了\n"); else if(x == random) return 0 ; else if(x > random) printf("日期晚了,lucky day在前面欸\n"); } printf("\n"); printf("对不起,次数用光咯,你没找到你的幸运日欸\n告诉你吧,是%d哦\n",random); return 0 ; }
task3
#include<stdio.h> int main(){ long x ; int y , n, N; printf("enter you number:"); while(scanf("%d", &x) != EOF){ printf("new number is:"); while(x > 0){ y = x % 10; if(y%2 == 1) printf("%d", y); x = x/10 ; } } return 0 ; }
task4
#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); } } }
task5
#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) { int i,m=1,q=1; double item,s=0; for(i=1; i <= n; i++){ q=q*i; item = m*1.0/q; s = s + item; m = -m; } return s ; }
task6
#include<stdio.h> #include<stdlib.h> int isprime(int x); int main(){ int i,n=0; int line=0; for(i=101;i<=200;i++){ if(isprime(i)){ printf("%6d",i); n++; line++; if(line == 5){ printf("\n"); line = 0; } } } printf("100-200之间的素数为:%d\n", n ); return 0 ; } int isprime(int x){ int m; for(m=2; m<x ;m++) if(x%m == 0) break; if(m >= x)return 1; else return 0; }
这次确实有点麻烦了,很多小细节注意不到使代码不正确,还是要加强实验理解。
标签:%.,int,double,delta,ex3,printf,include 来源: https://www.cnblogs.com/lddsbk/p/14664109.html