發表文章

目前顯示的是 11月, 2014的文章

高中生程式解題系統:括號匹配問題 Balanced Parentheses

題目連結 http://zerojudge.tw/ShowProblem?problemid=a229 。 這題用 Depth-first search (DFS) 的觀念來解。 程式碼一: # include <stdio.h> int n; void DFS ( int index, int r, int l, char *s) { if (l == n) { puts (s -2 *n); return ; } if (r < n) *s = '(' , DFS(index+ 1 , r+ 1 , l, s+ 1 ); if (r > l) *s = ')' , DFS(index+ 1 , r, l+ 1 , s+ 1 ); } main() { char s[ 100 ]; while ( scanf ( "%d" , &n) == 1 ) s[ 2 *n] = '\0' , DFS( 0 , 0 , 0 , s), puts ( "" ); return 0 ; } 程式碼二: # include <cstdio> # include <iostream> using namespace std ; void prtPattern ( int index, int right, int left, char *pattern) { if (index == right && left == index) { puts (pattern - 2 * index); } if ( right < index ) { *pattern = '(' ; prtPattern(index, right + 1 , left, pattern + 1 ); } if ( left < right) { *pattern = ')' ; p...

高中生程式解題系統:數數愛明明

題目連結 http://zerojudge.tw/ShowProblem?problemid=a216 。 此題用題目給的公式: f(n) = n + f(n-1) g(n) = f(n) + g(n-1) 來導出公式即可,例如 f(n) 的公式為: f(n) = n * (n + 1 ) / 2 ; 請試試看 g(n) 如何導出公式。 程式碼: # include <cmath> # include <cstdio> using namespace std ; int main ( void ) { long long fn, gn; int n; while ( scanf ( "%d" , &n) != EOF) { fn = n * (n + 1 ) / 2 ; gn = 0 ; for ( int i = 1 ; i <= n; i++) { gn += ((i * (i + 1 )) / 2 ); } printf ( "%llu %llu\n" , fn, gn); } return 0 ; } 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

高中生程式解題系統:a003: 兩光法師占卜術

題目連結 https://zerojudge.tw/ShowProblem?problemid=a003 。 此題是練習餘數運算,底下用C、C++、Python、Java做練習。 C程式碼: # include <stdio.h> int main () { char *msg[] = { "普通" , "吉" , "大吉" }; int m; while ( scanf ( "%d" , &m) != EOF) { int d; scanf ( "%d" , &d); m = (m* 2 +d) % 3 ; printf ( "%s\n" , msg[m]); } return 0 ; } C++程式碼: # include <iostream> using namespace std ; int main () { char *msg[] = { "普通" , "吉" , "大吉" }; short m; while ( cin >> m){ short d; cin >> d; m = (m* 2 +d) % 3 ; cout << msg[m] << endl ; } return 0 ; } Python程式碼: import sys msg = [ "普通" , "吉" , "大吉" ] for line in sys.stdin: [m, d] = line.split() m = int(m) d = int(d) m = (m* 2 +d) % 3 print(msg[m]) Java程式碼: import java.util.Scanner; public clas...

高中生程式解題系統:a002: 簡易加法

題目連結 https://zerojudge.tw/ShowProblem?problemid=a002 。 此題是練習整數的加法運算,底下用C++、Python做練習。 C++程式碼: # include <iostream> using namespace std ; int main () { int a; while ( cin >> a){ int b; b = a; cin >> a; b += a; cout << b << endl ; } return 0 ; } Python程式碼: import sys for s in sys.stdin: num = list(map(int,s.split())) print(num[ 0 ]+num[ 1 ]) 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

高中生程式解題系統:a001: 哈囉

題目連結 https://zerojudge.tw/ShowProblem?problemid=a001 。 此題是練習輸入與輸出,底下用C、C++、Python、Java做練習。 C程式碼: # include <stdio.h> int main () { char s[ 9999 ]; while ( scanf ( "%s" ,s)!=EOF ) { printf ( "hello, %s\n" ,s); } return 0 ; } C++程式碼: # include <iostream> using namespace std ; int main () { string s; while ( cin >> s){ cout << "hello, " << s << endl ; } return 0 ; } Python程式碼: import sys for s in sys.stdin: print( 'hello, ' +s) Java程式碼: import java.util.Scanner; public class JAVA { public static void main (String[] args) { Scanner cin = new Scanner(System.in); String s; while (cin.hasNext()) { s=cin.nextLine(); System.out.println( "Hello " + s); } } } 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。