計算機只能按照程序去執行,不可能本身「想出」一個解決問題的方法ios
面對一個問題,你必須本身找到解決方案,纔有可能作出相應的程序c++
因此,沒有解決方案的時候,不要急着動手寫程序算法
問題➡️解決方案:思考編程
解決方案➡️程序:描述數組
在結構化程序設計中,老是按照「先粗後細,先抽象後具體「的辦法,對索要描述的解決方案進行窮盡分解,知道分解爲順序、分支、循環三種結構測試
在寫程序前,先構思好程序的結構,能夠先寫出程序輪廓,再後補變量定義等細節ui
雞兔同籠問題spa
問題描述:一個籠子裏面關了雞和兔子,已知腳數a,問籠子裏面至少有多少動物,至多有多少種動物設計
輸入&輸出code
2➡️0 0
3➡️0 0
20➡️5 10
#include<iostream> using namespace std; int main() { int nCases,i,nFeet; cin>>nCases; for(i=0;i<nCases;i++) { cin>>nFeet; if(nFeet%2!=0) cout<<"0 0"<<endl; else if(nFeet%4!=0) cout<<nFeet/4+1<<" "<<nFeet/2<<endl; ekse cout<<nFeet/4<<" "<<nFeet/2<<endl; } return 0; }
#include<iostream> using namespace std; int main() { int x,y,z; cout<<"\t 母雞\t\t 公雞\t\t 小雞"<<endl; for(x=0;x<=33;x++) for(y=0;y<=50;y++) for(z=0;z<=100;z++) { if((x+y+z)== 100) if((3*x+2*y+0.5*z)==100) cout<<"\t"<<x<<"\t\t"<<y<<"\t\t"<<z<<endl; } return 0; } //可簡化一層循環:z=100-x-y
#include<iostream> using namespace std; int main() { int all[10],odd[10],even[10]; int i = 0; j = 0; for(; i<10; i++) cin>>all[i]; int numOdd = 0; int numEven = 0; for(i = 0; i < 10; i++) { if(all[i] % 2 != 0) { odd[numOdd] = all[i]; numOdd++; } else { even[numEven] = all[i]; numEven++; } } for(i = 0;i < numOdd;i++) cout<<odd[i]<<" "; for(i = 0;i < numEven;i++) cout<<even[i]<<" "; return 0; }
#include<iostream> using namespace std; int main() { int a[10]; int i=0,j=0; int temp = 0; for(i = 0;i < 10; i++) cin>>a[i]; for(i=0;i<0;i++) for(j=i+1;j<10;j++) { if(a[j]>a[i]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } for(i = 0;i < 10; i++) cout<<a[i]<<" "; return 0; }
整數奇偶排序
問題描述:輸入10個0~100之間的不一樣整數,彼此以空格分割從新排序之後輸出(也按空格分割)
要求:1.先輸出其中的奇數,並按從大到小排列;
2.而後輸出其中的偶數,並按從小到大排列。
#include<iostream> using namespace std; int main() { int all[10],odd[10],even[10]; int i=0,j=0; for(; i<10;i++) cin>>all[i]; int numOdd = 0; int numEven = 0; for(i = 0; I , 10; I++){ if(all[i]%2 !=0) { odd[numOdd] = all[i]; numOdd++; } else { even[numEven] = all[i]; numEven++; } } for(i = 0; i<numOdd-1;i++){ for(j = i;j<numOdd;j++) { if(odd[j]>odd[i]) { int tmp = odd[i]; odd[i] = odd[j]; odd[j] = tmp; } } } for(i = 0; i < numEven - 1; i++) { for(j = i; j < numEven;j++) { if(even[j]<even[i]) { int temp = even[j]; even[j] = even[i]; even[i] = temp; } } } for(i = 0; i < numOdd; i++) cout<<odd[i]<<" "; for(i = 0; i < numEVen; i++) cout<<even[i]<<" "; return 0; }
#include <iostream> using namespace std; int main() { int a; cin >> a; if (a == 1 || a == 3 || a == 5) cout << "NO" << endl; else cout << "YES" << endl; return 0; }
#include <iostream> using namespace std; int main() { int m, n, result = 0; cin >> m >> n; while (m <= n) { //對於m和n之間的每個數, 若是它是奇數,那麼就加入到咱們的結果裏。若是不是就跳過。 if (m % 2 == 1) result += m; m++; } //最後輸出 cout << result << endl; return 0; } //更快算法:若m爲奇數,則循環+2求和至大於等於n;若m爲偶數,則先+1再循環+2求和至大於等於n
#include <iostream> using namespace std; int main(){ int k; cin>>k; int n1=0, n5=0, n10=0; for (int i=0;i<k;i++){ int n; cin>>n; if (n == 1) n1++; else if (n == 5) n5++; else if (n == 10) n10++; } cout<<n1<<endl; cout<<n5<<endl; cout<<n10<<endl; return 0; }
# include <iostream> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int x, ans = 0; cin >> x; while (x > 0) { ans += x % 2; x /= 2; } cout << ans << endl; } return 0; } //這道題的解法就是反覆地除以2,看最低位是1仍是0。
#include <iostream> using namespace std; int a[100]; int main() { int n; cin >> n; for (int i = 0; i<n; i++) cin >> a[i]; while (n--) { //經常使用的倒序計數循環,等價於while(n-->0) cout << a[n]; if (n > 0) cout << " "; //若是不是最後一個數那麼就要用空格分隔開 } return 0; } //順序輸入倒序輸出,若是要求經過修改數組自己實現逆序重放,則使用臨時變量temp,將首尾元素逐個交換便可。