高中生程式解題系統:00494 - Kindergarten Counting Game

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

題目連結 http://zerojudge.tw/ShowProblem?problemid=a011

筆者的解法如下:
使用兩個布林變數
bool curLetter;
bool preLetter;
curLetter 判斷目前的字元是不是英文字母大小寫。
preLetter 判斷前一個字元是不是英文字母大小寫。

一個整數變數
short wordCount = 0;
計算目前算出的英文字個數。

演算法步驟:

  • Step 1. 將 preLetter 設為 false。
  • Step 2. 對每一個字元重複執行 Step 3到 Step 5。
  • Step 3. 判斷目前字元是不是英文字母。
  • Step 4. 若 (preLetter && !curLetter) ==> wordCount++
  • Step 5. preLetter = curLetter
  • Step 6. 若 curLetter 為true時,wordCount++。


程式碼:

#include <iostream>
using namespace std;
int main()
{
string str;
bool curLetter;
bool preLetter;
while( getline(cin, str ) )
{
short wordCount = 0;
preLetter = false;
int len = str.length();
for(int i = 0; i < len; i++)
{
curLetter = (str[i] >= 'a' && str[i] <= 'z')
|| (str[i] >= 'A' && str[i] <= 'Z');
if(preLetter && !curLetter)
wordCount++;
preLetter = curLetter;
}
if(curLetter)
wordCount++;
cout << wordCount << endl;
}
return 0;
}
view raw a011.cpp hosted with ❤ by GitHub

高中生程式解題系統:10055 - Hashmat the Brave Warrior

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

題目連結 http://zerojudge.tw/ShowProblem?problemid=a012

這題需要用到 long long int,也要取兩個整數差的絕對值。

程式碼:

#include <iostream>
using namespace std;
int main()
{
long long int a, b, d;
while( cin >> a >> b )
{
d = a - b;
if( d < 0 )
d = -d;
cout << d <<endl;
}
return 0;
}
view raw a012.cpp hosted with ❤ by GitHub