發表文章

高中生程式解題系統:解方程

圖片
題目連結 http://zerojudge.tw/ShowProblem?problemid=a410 。 用 Cramer's Rule 來解,若有一聯立方程式如下(下圖取自 Cramer's Rule ): 則x與y的解為(下圖取自 Cramer's Rule ): 程式碼: # include <cstdio> using namespace std ; int main () { float a,b,c,d,e,f; while ( scanf ( "%f%f%f%f%f%f" ,&a,&b,&c,&d,&e,&f) != EOF ){ float det = a*e - b * d; float detX = c * e - b * f; float detY = a * f - c * d; if (det == 0 && detX == 0 ) printf ( "Too many\n" ); else if ( det == 0 ) printf ( "No answer\n" ); else { printf ( "x=%.2f\n" ,detX / det); printf ( "y=%.2f\n" ,detY / det); } } return 0 ; } 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

高中生程式解題系統:排列最大值

題目連結 http://zerojudge.tw/ShowProblem?problemid=b051 。 此題改寫 C++語言sort 的比較函數comp即可。 程式碼: # include <iostream> # include <string> # include <algorithm> using namespace std ; bool cmp ( const string & s1, const string & s2) { return (s1 + s2).compare(s2 + s1) > 0 ; } int main () { int n; while ( cin >> n) { string s[n]; for ( int i = 0 ; i < n; i++) cin >> s[i]; sort(s, s+n, cmp); for ( int i = 0 ; i < n; i++) cout << s[i]; cout << endl ; } return 0 ; } 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

高中生程式解題系統: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 -...