不使用加減乘除符號的四則運算
阿新 • • 發佈:2020-11-19
實驗一
/* 一元二次方程求解 重複執行, 直到按Ctrl + Z結束 */ #include <math.h> #include <stdio.h> #include <stdlib.h> int main(){ float a, b, c, x1, x2; float delta, real, imag; printf("請輸入 a, b, c: "); while (scanf("%f%f%f", &a, &b, &c) != EOF) { // 輸入一元二次方程的a,b,c的值 if (a == 0) printf("不是二次方程\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("請輸入 a, b, c: "); } system("pause"); return 0; }
實驗二
// 生成N個0~9之間的隨機整數,並列印輸出 #include <stdio.h> #include <stdlib.h> #include <time.h> #define N 10 // 定義常量,使得輸出的數字數量為10個 int main(){ int x, n; srand(time(0)); // 以當前系統時間作為隨機種子 n = 0; do { n++; x = rand()%100 + 1 ; // 生成一個1~100之間的隨機整數 printf("%3d", x); }while(n < N); printf("\n"); system("pause"); return 0; }
實驗三
#include <stdio.h> #include <stdlib.h> int main() { int a, b, n; n = 0; for (a = 101; a <= 200; a++) { for (b = 2; b <= a; b++) { if (a % b == 0) { break; } else { if (b == a - 1) { printf("%d ", a); n = n + 1; } } } } printf("\n"); printf("101-200的素數共有%d個", n); system("pause"); return 0; }
這個實驗我其實聽困惑的,我之前寫了一個不一樣的程式碼,想求任意兩個區間的素數的個數,但是發現個數一直無法輸出,其他的都可以正常輸出,就很困惑.......
在同學的幫助下,才寫出來了正確的程式碼.......
另一個程式如下
/* 程式名稱:實驗任務3; 程式功能:計算區間素數個數; 建立日期:2020.11.17 */ #include <stdio.h> #include <stdlib.h> int main() { int min, max, i, n , j, m; n = 0; printf("請輸入兩個自然數"); scanf("%d %d", &min, &max); printf("%d 與 %d 之間的素數一共有 %d 個\n", min, max, m); printf("%d 與 %d 之間的素數分別是:", min, max); while( min <= max ) { j = 0; for(i = 2; i <= min/2; i++) { if(min % i == 0) { j = 1; break; } } if(j == 0) printf("%d ", min); min++; n++; } m = n; system("pause"); return 0; }
實驗四
#include <stdio.h> #include <stdlib.h> int main() { int a, b, c, d; printf("Enter a number:"); while (scanf("%d", &a) != EOF) { b = 0; c = 1; while(a != 0) { d = a % 10; if(d % 2 != 0) { b = d*c+b; c = c * 10; } a = a / 10; } printf("new name is:%d\n", b); printf("Enter a number:"); } system("pause"); return 0; }
這個程式對於我來說也有一定的難度,我寫了一個下午......
我以後會多加練習,學好C語言!
實驗五
#include <stdio.h> #include <stdlib.h> int main() { int n, k, m; double j, s; printf("Enter n(1~10):"); while(scanf("%d", &n) !=EOF) { m = 1; k = 1; s = 0.0; j = 1.0; while(k <= n ) { s = s + m / j; k++; m = -m; j = j * k; } printf("當n = %d 時,s = %lf \n", n, s); printf("Enter n(1~10):"); } system("pause"); return 0; }
實驗六
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int a, b, c, d; srand(time(0)); a = rand()%32; b = 1; printf("猜猜0202年21月哪一天會是你的lucky day\n"); printf("開始了,你有三次機會,猜吧(1~31):"); scanf("%d", &c); while(b < 3) { if(c == a) { printf("針不戳,蒸餾,蕪湖,起飛~~~"); break; } else if(c > a) { printf("It's too late. 拉挎,實在是拉挎。"); scanf("%d", &c); b++; } else if(c < a) { printf("早了早了,你猴急猴急的幹哈呀?"); scanf("%d", &c); b++; } } if(b = 3) { printf("次數用完了,偷偷告訴你,12月,你的lucky day 是%d號,多撈啊。", a); } system("pause"); return 0; }
這次任務對我的提升很大,學到了很多新東西
也發現了許多新的問題,比如說:邏輯混亂,演算法優化不好
以後會多多敲程式碼練習