实验3
一
#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> int main(){ int x; int sum; sum =0; for( x=100; x<=200; x++ ){ int i; int isPrime = 1; //x是素数 for ( i=2; i<x; i++ ) { if ( x % i == 0 ) { isPrime=0; break; } } if ( isPrime == 1 ) { printf("%d ",x ); sum++; if(sum%5==0) printf("\n"); } } printf("\n"); printf("101~200之间素数有%d个",sum); return 0; }

四
#include<stdio.h> int main(){ int s,a,x,f; printf("Ennter a number:"); while(scanf("%ld",&s)!=EOF){ f=1,x=0; do { a=s%10; if(a%2!=0){ a=a*f; x+=a; f=f*10; } else; s=s/10; }while(s!=0); printf("new number is :%ld\n\n",x); printf("Ennter a number:"); } return 0; }

1.%2!=0
2.f*10
五
#include<stdio.h> int main(){ int n,m; double s,x; printf("Enter n(1~10):"); while(scanf("%d",&n)!=EOF){ s=0; for(;n>0;n--){ x=1; for(m=n;0<m;m--){ x=x*m; } if(n%2==0) s = s - 1.0 / x; else s = s + 1.0 / x; } printf("n=%d, s=%f",n, s); } return 0; }

六
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int x, n,a; srand(time(0)); n = 0; x = rand()%31+1; printf("猜猜2020年12月会是你的luck day\n"); printf("开始了,你有三次机会,猜吧\n"); for(n=3;n>0;n--){ scanf("%d",&a); if(a==x){ printf("yes\n"); break; } else if(a<x) printf("小了\n"); else if(a>x) printf("大了\n"); } printf("the day is %d\n",x); return 0; }


浙公网安备 33010602011771号