Try   HackMD
tags: tgirc早修book

Ch.01 題目練習

四則運算

題目

  1. Zerojudge a002: 簡易加法
  2. Zerojudge d049: 中華民國萬歲!
  3. Zerojudge d073: 分組報告
題解
  1. Zerojudge a002: 簡易加法
#include<iostream> using namespace std; int main(){ int a,b; //宣告整數變數a與b cin>>a>>b; //輸入a與b cout<<a+b; //輸出(a+b)的值 return 0; }

  1. Zerojudge d049: 中華民國萬歲!

民國年=西元年-1911

#include<iostream> using namespace std; int main(){ int n; //宣告整數變數n cin>>n; //輸入n cout<<n-1911; //輸出(n-1911)的值 return 0; }

  1. Zerojudge d073: 分組報告

這題稍微複雜些,可以先依每個座號列出其組別,然後再觀察其關係。

1/30 ,在第 1
2/30 ,在第 1
3/31 ,在第 1
4/31 ,在第 2
5/31 ,在第 2
6/32 ,在第 2
7/32 ,在第 3

可以觀察出,每個座號先向後移1後,再除以3,然後再加上1就是他的組別了。
改寫成運算式即為: (n+1)/3 +1

#include<iostream> using namespace std; int main(){ int n; cin>>n; cout<<(n-1)/3+1; return 0; }

資料型態

題目

  1. 98 / c - Speed of Light
題解
  1. 98 / c - Speed of Light

按照題目需求,依分、時、日、周,一次一次乘完後輸出。
周除7變回日,再乘上365就是年了。

#include <iostream> using namespace std; int main(){ long long int n = 299792458; cout << "1 Light-second(LS) is " << n << " metres.\n"; n = n * 60; //LM cout << "1 Light-minute(LM) is " << n << " metres.\n"; n = n * 60; cout << "1 Light-hour(LH) is " << n << " metres.\n"; n = n * 24; cout << "1 Light-day(LD) is " << n << " metres.\n"; n = n * 7; cout << "1 Light-week(LW) is " << n << " metres.\n"; n = (n / 7) * 365; cout << "1 Light-year(LY) is " << n << " metres.\n"; return 0; }

ASCII

題目

  1. 輸出 *8}H 的 ASCII 編號
  2. 讓輸入的字母都後移 3 位吧!(超出 Z 或是 z 時,從 A 或 a 開始)
  3. TOJ 101 / e' - English Alphabet Prime
題解
  1. 輸出 *8}H 的 ASCII 編號

直接使用 int( ' 字元' ) 的方式取得ASCII碼。

#include <iostream> using namespace std; int main(){ cout << int( '*' ) << "\n"; cout << int( '8' ) << "\n"; cout << int( '}' ) << "\n"; cout << int( 'H' ); return 0; }

  1. 讓輸入的字母都後移 3 位吧!(超出 Z 或是 z 時,從 A 或 a 開始)

直接將ASCII碼加上3即可,另設一個 if 判斷是否超過 zZ,超過就扣26回去。

#include <iostream> using namespace std; int main(){ char c; cin >> c; c += 3; if( (c > 'Z' && c < 'a') || (c > 'z') ){ c -= 26; } cout << c; return 0; }

  1. TOJ 101 / e' - English Alphabet Prime

'A' 的ASCII碼為 65,而輸入 n 為 1 時應輸出 'A'
因此輸出為 64 + n 對應到的字元。

#include <iostream> using namespace std; int main(){ int n; cin >> n; cout << char( 64 + n ); return 0; }