Try   HackMD
tags: tgirc早修book

Ch.02 題目練習

if else

題目

  1. Zerojudge d064: ㄑㄧˊ 數?
  2. Zerojudge a003: 兩光法師占卜術
  3. Zerojudge d485: 我愛偶數
  4. Zerojudge d065: 三人行必有我師
  5. Zerojudge d066: 上學去吧!
  6. Zerojudge a006: 一元二次方程式
題解
  1. Zerojudge d064: ㄑㄧˊ 數?
#include<iostream> using namespace std; int main(){ int n; cin >> n; if(n % 2 ==0){ cout << "Even"; } else{ cout << "Odd"; } return 0; }

  1. Zerojudge a003: 兩光法師占卜術
#include <iostream> using namespace std; int main() { int M, D, S; cin >> M >> D; S = (M * 2 + D) % 3; if (S == 0){ cout << "普通\n"; } else if (S == 1){ cout << "吉\n"; } else { cout << "大吉\n"; } return 0; }

  1. Zerojudge d485: 我愛偶數
#include<iostream> using namespace std; int main(){ int a, b, ans; cin >> a >> b; ans = b/2 - a/2; ans += (a+1)%2; cout << ans; return 0; }

  1. Zerojudge d065: 三人行必有我師
#include<iostream> using namespace std; int main(){ int in, max=0; for(int i = 0; i < 3; i++){ cin >> in; if(in > max){ max = in; } } cout << max; return 0; }

  1. Zerojudge d066: 上學去吧!
#include<iostream> using namespace std; int main(){ int hh, mm; cin >> hh >> mm; if(hh>=8 && hh<=16){ cout << "At School" << endl; } else if(hh == 7){ if(mm >= 30){ cout << "At School" << endl; } else{ cout << "Off School" << endl; } } else{ cout << "Off School" << endl; } return 0; }

  1. Zerojudge a006: 一元二次方程式
#include<iostream> #include<cmath> using namespace std; int main(){ int a, b, c; int x1, x2; //兩解 int d; //判別式 cin >> a >> b >> c; d = sqrt( b*b - 4*a*c ); //判別式 if(d>0){ //判別式>0,兩實根 x1 = (-b + d) / (2*a); x2 = (-b - d) / (2*a); cout << "Two different roots x1=" << x1 << " , x2=" << x2; } else if(d == 0){ //判別式=0,重根 x1 = (-b) / (2*a); cout << "Two same roots x=" << x1; } else{ //判別式<0,無實根 cout << "No real root"; } return 0; }