第三次实验作业
1.
//一元二次方程求解 //重复执行,直到按Ctrl+Z结束 // #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; }
2.
//生成N个0~9之间的随机整数,并打印输出 #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; }
3.
#include <stdio.h> #include <math.h> int main(){ int i,m,n,count; count=0; for (m=101;m<=200;m++){ n=sqrt(m); for(i=2;i<=n;i++){ if(m%i==0)break; } if(i>n){ printf("%6d",m); count++; if (count%5==0) printf("\n"); } } printf("\n101~200间有%d个素数.",count); return 0; }
4.
#include<stdio.h> #include<math.h> int main(){ long x,y,z; while (printf("enter a biiiiiiiig number :"),scanf("%ld",&x)!=EOF){ long i=0,z=0; while (x>0){ y=x%10; if(y%2!=0){ z+=y*pow(10,i); i++; } x=x/10; } printf("neeeeeeeew number is:%ld\n",z); } return 0; }
5.
#include <stdio.h> #include <math.h> int main(){ int m,n; float s; printf("Enter n(1~10):"); while(scanf("%d",&n)!=EOF){ float s=0,i=1; for(m=1;m<=n;m++){ i=i*m; s=s+pow(-1,m-1)*(1/i); } printf("n=%d,s=%f\n",n,s); printf("Enter n(1~10):"); } return 0; }
6.
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int data,answer,count; srand((unsigned)time(NULL)); data= rand()% 31; printf("guess guess 2020年哪一天会是你的lucky day\n"); printf("开始喽,你居然有三次机会!猜吧 (1~31):"); while (count<3) { scanf("%d",&answer); if(data==answer) { printf("这就对了?\n");break;} else if(data>answer){ printf("猜的也太小气了吧!?\n"); } else{ printf("猜的也太大气了吧!?\n");} count++; } if(count>=3) printf("你无了,回复TD退订\n"); return 0; }