Try   HackMD
tags: tgirc早修book

Ch.03 題目練習

while 迴圈

題目

  1. Zerojudge a004: 文文的求婚
  2. Zerojudge a024: 最大公因數(GCD)
  3. Zerojudge a010: 因數分解
  4. Zerojudge a005: Eva 的回家作業
  5. Zerojudge b572: 忘了東西的傑克
  6. Zerojudge d490: 我也愛偶數
  7. Zerojudge d039: 11044 - Searching for Nessy
題解
  1. Zerojudge a004: 文文的求婚
#include<iostream> using namespace std; int main(){ int y; while(cin >> y){ if(y%4 == 0){ if(y%100 == 0 && y%400 != 0){ cout << "平年" << endl; } else{ cout << "閏年" << endl; } } else{ cout << "平年" << endl; } } return 0; }

  1. Zerojudge a024: 最大公因數(GCD)

    使用輾轉相除法

#include<iostream> using namespace std; int main(){ int a, b; cin >> a >> b; while(b > 0){ a %= b; swap(a, b); } cout << a; return 0; }

  1. Zerojudge a010: 因數分解
#include<iostream> #include<cmath> using namespace std; int main(){ int n, times; cin >> n; for(int i=2; i<=n; i++){ times = 0; //用來計算除的次數 while(n%i == 0){ n /= i; times++; } if(times > 1){ cout << i << '^' << times; if(n != 1){ cout << " * "; } } else if(times == 1){ cout << i; if(n != 1 ){ cout << " * "; } } } return 0; }

  1. Zerojudge a005: Eva 的回家作業
#include<iostream> using namespace std; int main(){ int n; int a, b, c, d; cin >> n; for(int i=0; i<n; i++){ cin >> a >> b >> c >> d; cout << a << " " << b << " " << c << " " << d << " "; if( b-a == c-b && c-b == d-c ){ cout << (d-c) + d << "\n"; } else if( b/a == c/b && c/b == d/c ){ cout << (d/c) * d << "\n"; } } return 0; }

  1. Zerojudge b572: 忘了東西的傑克
#include<iostream> using namespace std; int main(){ int n, h1, h2, m1, m2, ans, t; cin >> n; for(int i=0; i<n; i++){ cin >> h1 >> m1 >> h2 >> m2 >> ans; if(m2 < m1){ m2 += 60; //借位 h2--; } t = 60 * (h2-h1) + (m2-m1); if(t >= ans) cout << "Yes\n"; else cout << "No\n"; } return 0; }

(待補)

for 迴圈

題目

  1. Zerojudge a244: 新手訓練 ~ for + if
  2. Zerojudge d074: 電腦教室
  3. Zerojudge d498: 我不說髒話
  4. Zerojudge a148: You Cannot Pass?!
  5. Kattis FizzBuzz
  6. TOJ 577 / 力量
題解

(待補)

巢狀迴圈

題目

  1. 反覆輸出高是 n 的正三角形

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. Zerojudge d649: 數字三角形
  2. Zerojudge c013: 00488 - Triangle Wave
  3. TOJ 104 / 星星樹
  4. TOJ 110 / 六芒星的咒符
題解
  1. 反覆輸出高是 n 的正三角形
#include <iostream> using namespace std; int main() { int n; while(cin >> n){ for(int i=1; i<=n; i++){ for(int space=0; space<n-i ; space++) cout << " "; for(int stars=0; stars<i*2-1; stars++) cout << "*"; cout << "\n"; } } return 0; }

  1. Zerojudge d649: 數字三角形
#include<iostream> using namespace std; int main(){ int n; while(cin>>n && n){ for(int i=1; i<=n; i++){ for(int j=0; j<n-i; j++) cout << '_'; for(int j=0; j<i ; j++) cout << '+'; cout<<'\n'; } } return 0; }

  1. Zerojudge c013: 00488 - Triangle Wave
#include <iostream> using namespace std; int main(){ int t; cin>>t; while(t--){ //輸出多少組 waves int h,n; cin>>h>>n; while(n--){ //一組 wave 輸出多少個 for(int i=1;i<=h;i++){ //上半 for(int j=0;j<i;j++){ //輸出數字 cout<<i; } cout<<'\n'; } for(int i=1;i<h;i++){ //下半 for(int j=h;j>i;j--){ //輸出數字 cout<<h-i; } cout<<'\n'; } cout<<'\n'; } } return 0; }

(待補)

switch

題目

  1. Zerojudge a053: Sagit's 計分程式
  2. Zerojudge a244: 新手訓練 ~ for + if (請試著用switch解)
題解

(待補)