实验三
试验任务一
#include <math.h> #include <stdio.h> int main() { float a, b, c, x1, x2; float delta, real, imag; printf("Enter a, b, c: "); while(scanf("%f%f%f", &a, &b, &c) != EOF) { if(a == 0) printf("not quadratic equation.\n\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\n", x1, x2); } else { real = -b/(2*a); imag = sqrt(-delta) / (2*a); printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n\n", real, imag, real, imag); } } printf("Enter a, b, c: "); } return 0; }
试验任务二
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 int main() { int x, n; srand(time(0)); n = 0; do { n++; x = rand()%10; printf("%3d", x); }while(n<N); printf("\n"); return 0; }
实验任务三
#include <stdio.h> #include <stdlib.h> int main() { int i, j; int n = 0; for (i = 100; i <= 200; i++) { for (j = 2; j < i; j++) { if (i % j == 0) break; } if (j == i) { printf("%d ", i); n++; } } printf("\n101到200之间共有%d个素数\n", n); system("pause"); return 0; }
试验任务四
#include<stdio.h> #include<math.h> int main(void){ long s; printf("Enter a number:"); while(scanf("%ld",&s)!=EOF){ int t,i,k,j=0,x=0,y=0; t=s; for(i=1;t/(10,i)!=0;i++); for(k=1;k<=i;k++){ j=s%10; s=s/10; if(j%2!=0){ x=pow(10,y)*j+x; y++; } } printf("new number is:%ld\n",x); printf("Enter a number:"); } }
试验任务五
#include<stdio.h> int main(void){ double s,i,j,k,t; int n; printf("Enter n(1~10):"); while(scanf("%d",&n)!=EOF){ s=0,t=-1,k=1; for(i=1;i<=n;i++){ for(j=1;j<=i;j++){ k=k*j; } t=t*(-1); s=s+t*(1/k); k=1; } printf("n=%d,s=%f\n",n,s); printf("Enter n(1~10):"); } return 0; }
试验任务六
#include<stdio.h> #include<stdlib.h> #include<time.h> int main(){ int x,n,m=3; srand(time(0)); x=rand()%31+1; printf("猜猜2020年12月哪一天会是你的luck day\n"); printf("开始喽,你有三次机会,猜吧(1~31):"); while(m--){ scanf("%d",&n); if(n==x){ printf("恭喜你,猜中了"); break; } else if(n<x) printf("你猜的日期早了,luck day还没到呢\n"); else if(n>x) printf("你猜的日期晚了,luck day溜到前面啦\n"); if(m>0) printf("再猜(1~31):"); } if(m==-1) printf("次数用光啦。偷偷告诉你:12月,你的luck day是%d号",x); return 0; }