Coursera課程筆記----計算導論與C語言基礎----Week 5

從現實問題到計算機程序(Week 5)

總結回顧

計算機只能按照程序去執行,不可能本身「想出」一個解決問題的方法ios

面對一個問題,你必須本身找到解決方案,纔有可能作出相應的程序c++

因此,沒有解決方案的時候,不要急着動手寫程序算法

  • 切餅
    • 假設:有一張足夠大的餅,有一把足夠長的刀
    • 要求:每次切一刀
    • 問題:n刀最多能切出多少塊餅
    • 結論:q(n)=q(n-1)+n ⬅️這個解決方案是計算機「想」不出來的

現有構想再寫程序

問題➡️解決方案:思考編程

解決方案➡️程序:描述數組

結構化程序設計中,老是按照「先粗後細,先抽象後具體「的辦法,對索要描述的解決方案進行窮盡分解,知道分解爲順序、分支、循環三種結構測試

寫程序前,先構思好程序的結構,能夠先寫出程序輪廓,再後補變量定義等細節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;
}
  • 百元買百雞問題
    • 問題描述:假定小雞每隻0.5元,公雞每隻2元,母雞每隻3元。如今有100元要求買100只雞,編程列出全部可能的購機方案。
    • 窮舉法:將可能出現的各類狀況一一測試,判斷是否知足條件。
#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
  • 分出奇偶數
    • 問題描述:從鍵盤上輸入10個整數,請將其中的奇數和偶數識別出來,分別放入不一樣的數組中並輸出
    • 輸入示例:23 34 65 43 67 12 67 341 61 34
    • 輸出示例:
      • 奇數:23 65 43 67 67 341 61
      • 偶數:34 12 34
#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;
}
  • 整數排序
    • 問題描述:從鍵盤上輸入10個整數,請按照從大到小的順序將他們排列好,並按新的次序輸出到屏幕上
    • 輸入示例:23 34 65 43 67 12 67 341 61 34
    • 輸出示例:341 67 67 65 61 43 34 34 23 12
    • 選擇排列:最簡單的排列方式
#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;
}

結構化程序的基本思想

  • 程序由若干個模塊組成
  • 模塊以內高內聚
  • 模塊之間低耦合

做業題

Quiz 1 晶晶赴約會

#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;
}

Quiz2 奇數求和

#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

Quiz3 整數的個數

#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;
}

Quiz4 1的個數

# 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。

Quiz5 數組逆序重放

#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,將首尾元素逐個交換便可。
相關文章
相關標籤/搜索