实验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> #include <math.h> int main(){ int m,n,x; for(n=101;n<=200;n++){ for(m=2;m<=sqrt(n);m++) if(n%m==0)break; if(m>sqrt(n)){ x++; printf("%5d",n); if(x%5==0) printf("\n"); } } printf("\n101~200之间共有%d个素数。",x); return 0; }

#include <stdio.h> int main(){ long int x,y; int p=0,q=1; printf("Enter a number:"); while(scanf("%d",&x)!=EOF){ p=0;q=1; while(x>0){ y=x%10; x=x/10; if(y%2==1){ p=p+y*q; q=q*10; } } printf("new number is:%d\n",p); printf("\nEnter a number:"); } return 0; }

对输入的数字对10取余数,可以依次得到每一位上面的数,每一位数再对2取余数,判断是否为奇数,是奇数就保留,再对奇数从低位开始依次乘以个十百千得到新数。
#include <stdio.h> #include <math.h> int main () { int n,i; printf("Enter n(1~10):"); while(scanf("%d",&n)!=EOF){ double s=0,p=1; for(i=1;i<=n;i++){ p=p*i; s=s+pow(-1,i-1)*(1/p); } printf("n=%d,s=%f\n",n,s); printf("\nEnter n(1~10):"); } return 0; }

#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int m,n; srand(time(0)); m=rand()%31; int q=3; printf("猜猜2020年12月哪一天会是你的luck day\n\n开始喽,你有三次机会,猜吧(1~31):"); while(scanf("%d",&n)!=EOF) { if(n>m){ printf("\n你猜的日期晚了,luck day悄悄溜到前面啦\n"); q--; if(q>0) printf("\n再猜(1~31):"); } else if(n<m){ printf("\n你猜的日期早了,luck day还没到呢\n"); q--; if(q>0) printf("\n再猜(1~31)"); } if(q==0) printf("\n次数用完啦,偷偷告诉你,12月,你的luck day是%d号",m); } return 0; }

总结 :
函数pow(x,y)的作用是计算x的y次方,要加入头文件math.h。
浙公网安备 33010602011771号