發表文章

高中生程式解題系統: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++。 程式碼: 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

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

題目連結 http://zerojudge.tw/ShowProblem?problemid=a012 。 這題需要用到 long long int,也要取兩個整數差的絕對值。 程式碼: 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

高中生程式解題系統:c002: 10696 - f91

題目連結 http://zerojudge.tw/ShowProblem?problemid=c002 。 此題看起來像是需要用遞迴的方式來解,但仔細分析後,可觀察到底下的結果: if ( n >= 101 ) return n - 10 ; else return 91 ; 程式碼: # include <iostream> using namespace std ; long f91 ( int n) { if ( n >= 101 ) return n - 10 ; else return 91 ; } int main () { int n; while ( cin >> n ) { if ( n == 0 ) break ; long result = f91(n); cout << "f91(" << n << ") = " << result << endl ; } return 0 ; } 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

高中生程式解題系統:c007: 00272 - TeX Quotes

題目連結 http://zerojudge.tw/ShowProblem?problemid=c007 。 此題是當 " 出現在第1、3、5、7...奇數次才需要替換。 程式碼一: # include <stdio.h> int main ( void ) { char ch; int count = 1 ; while ( scanf ( "%c" , &ch) != EOF ) { if (ch == '"' ) { if ( count == 1 ) printf ( "``" ); else if ( count == 0 ) printf ( "''" ); count = 1 - count; continue ; } printf ( "%c" , ch); } return 0 ; } 程式碼二: # include <iostream> using namespace std ; int main ( void ) { char ch; int count = 1 ; while ( cin .get(ch) ) { if (ch == '"' ) { if ( count == 1 ) cout << "``" ; else if ( count == 0 ) cout << "''" ; count = 1 - count; ...

高中生程式解題系統:d709: 判断质数(一)

題目連結 https://zerojudge.tw/ShowProblem?problemid=d709 。 想法是:「目前要判斷的整數 N,用已知的質數 prime 去除。」 程式碼: # include <stdio.h> # include <cmath> using namespace std ; const long long int limitValue = 1000000 ; const int num = 78500 ; bool primeFlag[limitValue + 1 ]; int prime[num+ 1 ] = { 2 , 3 , 5 , 7 , 11 , 13 }; int cnt = 0 ; void genPrimeArray () { int index = 6 ; for ( int i = 0 ; i < index; i++) primeFlag[prime[i]] = 1 ; bool isPrime; for ( int i = 15 ; i <= limitValue; i += 2 ) { isPrime = true ; int k = 1 ; int terminal = sqrt (i); for ( int c = prime[k]; c <= terminal; k++, c = prime[k]) { if ( i % c == 0 ) { isPrime = false ; break ; } } if (isPrime == true ) { prime[index] = i; primeFlag[prime[index]] = 1 ; index++; } } } int main ( void ) { genPrimeArray(); int p ; while ( scanf ( "%d" ,&p)&& p!= 0 ){ printf ( "%d\n" , 1 -...

高中生程式解題系統:a227: 三龍杯 -> 河內之塔

題目連結 http://zerojudge.tw/ShowProblem?problemid=a227 。 以兩盤子個為例: 讓竿子一叫做 A,竿子二叫做B ,竿子三叫做C  步驟一:從 A 移動 盤子1 到 B。 步驟二:從 A 移動 盤子2 到 C。 步驟三:從 B 移動 盤子1 到 C。 可看出規律: 從 A 移動 n-1個盤子 到 B。 從 A 移動 第 n個盤子 到 C。 從 B 移動 n-1個盤子 到 C。 程式碼: # include <cstdio> using namespace std ; void hanoi ( int i, char from, char axu, char to) { if (i == 1 ) { printf ( "Move ring %d from %c to %c\n" , i, from, to); } else { hanoi(i -1 , from, to, axu); printf ( "Move ring %d from %c to %c\n" , i, from, to); hanoi(i -1 , axu, from, to); } } int main ( void ) { int n; while ( scanf ( "%d" , &n) == 1 ) { hanoi(n, 'A' , 'B' , 'C' ); } } 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

高中生程式解題系統:c121: 00495 - Fibonacci Freeze

題目連結 http://zerojudge.tw/ShowProblem?problemid=c121 。 此題需要用到大數運算的做法,程式碼是用字串來處理。 程式碼: # include <iostream> # include <string> // Fibonacci using namespace std ; string addition ( string s, string s2) // 加法 { int len, lenS, MaxL, i, a, b, c = 0 ; string BNstr (s2) ; // 去掉多餘的數字0 len = BNstr.length(); // 讀取被加數的長度 lenS = s.length(); // 讀取加數的長度 for (i = 0 ; i < len -1 ; i++) if (BNstr[ 0 ] == '0' ) BNstr.erase(BNstr.begin()); else break ; for (i = 0 ; i < lenS -1 ; i++) if (s[ 0 ] == '0' ) s.erase(s.begin()); else break ; len = BNstr.length(); lenS = s.length(); MaxL = (len > lenS)?len:lenS; for (i = 0 ; i < MaxL; i++) { if (i < len) a = BNstr[len -1 -i]- '0' ; else a = 0 ; if (i < lenS) b = s[lenS -1 -i]-...