C++與演算法

解答 - Sagit's 計分程式

先思考,作對題數為 0, 1, 11, 20, 24, ... 時,程式碼應該如何運作。

將每種情形都列出來,再簡化。


總共有4個題數區間需要處理:

  • 0~10
  • 11~20
  • 21~40
  • 40+

將三種區間的情況分別處理,就能寫出此題。

#include<iostream>
using namespace std;

int main()
{
    int N;

    cin >> N;
    if( 0<=N and N<=10 )
    {
        cout << 6*N << endl;
    }
    if( 11<=N and N<=20 )
    {
        cout << 60+(N-10)*2 << endl;
    }
    if( 21<=N and N<=40)
    {
        cout << 80+(N-20) << endl;
    }
    if( N>40 )
    {
        cout << 100 << endl;
    }

    return 0;
}