1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <iostream> #include <cstdlib> using namespace std; int main() { int a[10] = {86,46,62,71,17,3,7,25,44,17}; int x = 0,j = 0; for(int i=1;i<10;i++) { x = a[i]; for(j=i-1;j>=0;j--) { if(a[j]>x) a[j+1] = a[j]; else { a[j+1] = x; break; } } if(j < 0) a[0] = x; } for(int i=0;i<10;i++) cout<<a[i]<<" "; system ("pause"); return 0; } |
2013年12月11日 星期三
選擇排序法 (Selection sort)
2013年7月27日 星期六
資料型態轉型 Type Casting
Compile-time casting
unary operator with syntax: DataType(expression)
Ex: double(9)/2
Run-time casting
requested conversion checked at runtime, appiled if valid.
syntax: static_cast<DataType>(expression)
Ex: static_cast<double>(9)/2
兩者在效率上有所差別。
沒記錯的話,
應該是 Run-time casting 比較優。
2013年7月26日 星期五
Fibonacci 費氏數列
Fabonacci_recursive
遞迴版
Fabonacci
非遞迴版
遞迴版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<iostream> #include<cstdlib> using namespace std; int fibon(int num); int main() { int n = 0; cout<<"請輸入想知道的費氏數列第 n 項:"; cin>>n; cout<<fibon(n); system("pause"); return 0; } int fibon(int num) { if (num==1 || num==0)return num; else return (fibon(num-1) + fibon(num-2)); } |
Fabonacci
非遞迴版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include<iostream> #include<cstdlib> using namespace std; int fibon(int num); int main() { int n = 0; cout<<"請輸入想知道的費氏數列第 n 項:"; cin>>n; cout<<fibon(n); system("pause"); return 0; } int fibon(int num) { int ans = 0; int n1 = 0,n2 = 1; if (num==0 || num==1)return num; else { for (int i=0;i<num-1;i++) { ans = n1 + n2; n1 = n2; n2 = ans; } return ans; } } |
2013年7月16日 星期二
d478: 共同的數 - 簡易版 (TLE)
http://zerojudge.tw/ShowProblem?problemid=d478
暴力解法 TLE
還是得排序在比較
暴力解法 TLE
還是得排序在比較
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <iostream> #include <cstdlib> using namespace std; int main() { int m = 0,n = 0; const int k = 1000000; int s1[k],s2[k]; while(cin>>m>>n) while(m--) { for(int i=0;i<n;i++) { s1[i]='\0'; s2[i]='\0'; } for(int a=0;a<n;a++) cin>>s1[a]; for(int b=0;b<n;b++) cin>>s2[b]; int count = 0; for(int c=0;c<n;c++) for(int d=0;d<n;d++) if(s1[c]==s2[d]) count++; cout<<count<<endl; } return 0; } |
2013年7月15日 星期一
d483: hello, world
http://zerojudge.tw/ShowProblem?problemid=d483
還真怕沒有一次就 AC ......
還真怕沒有一次就 AC ......
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /**********************************************************************************/ /* Problem: d483 "hello, world" from Wikipedia */ /* Language: CPP (122 Bytes) */ /* Result: AC(4ms, 328KB) judge by this@ZeroJudge */ /* Author: jrsh0906 at 2013-07-15 16:53:06 */ /**********************************************************************************/ #include <iostream> #include <cstdlib> using namespace std; int main() { cout<<"hello, world"; return 0; } |
2013年7月12日 星期五
d636: 大爆炸bomb (WA)
http://zerojudge.tw/ShowProblem?problemid=d636
- 二分法
- 邊乘邊 Mod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> #include <cstdlib> #include <cmath> using namespace std; int main() { long long int a = 0,b = 0; while(cin>>a>>b) { a %= 10007; long long int Ans = 0; Ans = pow((long double)a,(long double)b); cout<<(Ans%10007)<<endl; } return 0; } |
2013年7月11日 星期四
d086: 態度之重要的證明
http://zerojudge.tw/ShowProblem?problemid=d086
魔鬼測資:0123→Fail,而不是結束程式
所以檢查字串時還要確認是單純的"0"(結束程式),還是一個"0"開頭的字串(輸出 Fail)
魔鬼測資:0123→Fail,而不是結束程式
所以檢查字串時還要確認是單純的"0"(結束程式),還是一個"0"開頭的字串(輸出 Fail)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | /**********************************************************************************/ /* Problem: d086 "態度之重要的證明" from 成功電研20th~21st C++ ~最後的競賽~ */ /* Language: CPP (650 Bytes) */ /* Result: AC(4ms, 464KB) judge by this@ZeroJudge */ /* Author: jrsh0906 at 2013-07-11 15:58:44 */ /**********************************************************************************/ /* Author: Hsu Shih Po Date: 11/07/13 15:41 Description: d086: 態度之重要的證明 */ #include <iostream> #include <cstdlib> using namespace std; int main() { string proof; while(cin>>proof) { if(proof[0]=='0' && proof[1]=='\0')break; int sum = 0; for(int i = 0;i < proof.length();i++) { if(proof[i]>='A' && proof[i]<='Z') sum += proof[i] - 64; else if(proof[i]>='a' && proof[i]<='z') sum += proof[i] - 96; else { sum = -1; break; } } if(sum != -1)cout<<sum<<endl; else cout<<"Fail"<<endl; } return 0; } /* A:65 a:97 Z:90 z:122 */ |
2013年7月8日 星期一
d985: Gran Turismo 5
http://zerojudge.tw/ShowProblem?problemid=d985
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | /**********************************************************************************/ /* Problem: d985 "Gran Turismo 5" from 學姊 */ /* Language: CPP (827 Bytes) */ /* Result: AC(4ms, 404KB) judge by this@ZeroJudge */ /* Author: jrsh0906 at 2013-07-09 13:50:17 */ /**********************************************************************************/ /* Author: Hsu Shih Po Date: 09/07/13 13:50 Description: d985: Gran Turismo 5 */ #include <iostream> #include <cstdlib> using namespace std; int main() { int N = 0,M = 0; cin>>N; for(int a = 1;a <= N;a++) { cin>>M; int min = 0,sec = 0; cin>>min>>sec; int best_time = 0; best_time = min*60 + sec; int avg_time = best_time; for(int b = 1;b < M;b++) { cin>>min>>sec; int time = min*60 + sec; if(time < best_time) best_time = time; avg_time += time; } avg_time /= M; cout<<"Track "<<a<<":"<<endl; cout<<"Best Lap: "<<best_time/60<<" minute(s) "<<best_time%60<<" second(s).\n"; cout<<"Average: "<<avg_time/60<<" minute(s) "<<avg_time%60<<" second(s).\n"; cout<<endl; } return 0; } |
訂閱:
文章 (Atom)