實驗3 C語言分支語句和迴圈語句變成應用
阿新 • • 發佈:2020-11-19
實驗任務1
1 #include <math.h> 2 #include <stdio.h> 3 int main() 4 { 5 float a, b, c, x1, x2; 6 float delta, real, imag; 7 printf("Enter a, b, c: "); 8 while(scanf("%f%f%f", &a, &b, &c) != EOF) 9 { 10 if(a == 0) 11 printf("not quadratic equation.\n\n"); 12 else 13 { 14 delta = b*b - 4*a*c; 15 if(delta >= 0) 16 { 17 x1 = (-b + sqrt(delta)) / (2*a); 18 x2 = (-b - sqrt(delta)) / (2*a); 19 printf("x1 = %.2f, x2 = %.2f\n\n", x1, x2); 20 }21 else 22 { 23 real = -b/(2*a); 24 imag = sqrt(-delta) / (2*a); 25 printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n\n", real, imag, real, imag); 26 } 27 } 28 printf("Enter a, b, c: "); 29 }30 return 0; 31 }
實驗任務2
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #define N 5 5 int main() 6 { 7 int x, n; 8 srand(time(0)); 9 n = 0; 10 do 11 { 12 n ++; 13 x = rand() % 10; 14 printf("%3d", x); 15 } while(n < N); 16 printf("\n"); 17 return 0; 18 }
實驗任務3
方法一
1 #include <stdio.h> 2 #include <math.h> 3 main() 4 { 5 int n,s,i,m; 6 for(n = 101; n <= 200; n ++) 7 { 8 s = sqrt(n); 9 for(i = 2; i <= s; ++ i) 10 { 11 if(n % i == 0) 12 break; 13 if(i == s) 14 { 15 m ++; 16 printf("%d\t",n); 17 if(m % 5 == 0) 18 printf("\n"); 19 } 20 } 21 22 } 23 printf("\n101~200之間共有%d個素數.",m); 24 }
方法二(定義函式)
1 #include <stdio.h> 2 #include <math.h> 3 int isPrime(int); 4 5 main() 6 { 7 int i = 0, m = 0; 8 for(i = 101; i <= 200; i ++) 9 { 10 if(isPrime(i)) 11 { 12 13 m ++; 14 printf("%d\t",i); 15 if(m % 5 == 0) 16 printf("\n"); 17 } 18 } 19 20 21 22 printf("\n101~200之間共有%d個素數.",m); 23 } 24 25 int isPrime(int n) 26 { 27 if(n < 2) 28 return 0; 29 30 int k; 31 for(k = 2; k <= sqrt(n); k ++) 32 { 33 if(n % k == 0) 34 return 0; 35 } 36 37 return 1; 38 }
執行結果
實驗任務4
我的思路:用兩個迴圈從高位到低位取下數,算2的餘數判斷其是不是奇數。
1 #include<stdio.h> 2 int main() 3 { 4 long long s,n,m; 5 printf("Enter a number:"); 6 scanf("%ld",&s); 7 printf("new number is: "); 8 for(n = 1; s / n != 0; n *= 10 ) 9 ; 10 n /= 10; 11 for(; n >= 1; n /= 10) 12 { 13 m = s / n % 10; 14 if(m % 2 == 1) 15 printf("%ld",m); 16 } 17 return 0; 18 }
實驗任務5
1 #include <stdio.h> 2 main() 3 { 4 int n,i; 5 6 7 while(scanf("%d",&n) != EOF) 8 { 9 printf("Enter n(1~10): "); 10 double s = 0, t = 1; 11 if(n < 1 || n > 10) 12 printf("Invalid input."); 13 else 14 { 15 for(i = 1; i <= n; i ++, t *= i) 16 { 17 if(i % 2 == 1) 18 s += 1.0 / t; 19 else 20 s -= 1.0 / t; 21 } 22 printf("n = %d, s= %f",n,s); 23 } 24 25 } 26 }
實驗任務6
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 main() 5 { 6 int x, n, i = 0; 7 8 srand(time(NULL)); 9 x = rand() % 31; 10 11 printf("猜猜2020年12月哪一天會是你的lucky day~0.0\n"); 12 printf("Game start!你有三次機會,猜叭(1~31):"); 13 while(i <= 3) 14 { 15 i ++; 16 scanf("%d",&n); 17 if(n < x) 18 { 19 printf("害,你猜的日期早了,luck day還沒到呢。\n"); 20 if(i <= 2) 21 printf("再猜(1~31):"); 22 else 23 printf("次數用完啦TAT,偷偷告訴你,12月,你的luck day是%d號",x); 24 } 25 else if(n > x) 26 { 27 printf("哈哈你猜的日期晚了,luck day悄悄溜到前面去了。\n"); 28 if(i <= 2) 29 printf("再猜(1~31):"); 30 else 31 printf("次數用完啦TAT,偷偷告訴你,12月,你的luck day是%d號",x); 32 } 33 else if(n==x) 34 { 35 printf("Good game, you are right!"); 36 break; 37 } 38 } 39 }