Try   HackMD
tags: tgirc早修book

Ch.06 題目練習

題目

  1. Zerojudge a442: Necklace Problem (NP)
  2. Toj 92 / 天線寶寶說你好
  3. Zerojudge d535: 2. 密碼驗證與擷取
  4. Zerojudge a038: 數字翻轉
  5. Zerojudge e267: 11192 - Group Reverse
  6. Kattis Quick Estimates
  7. Toj Reversed card open!
  8. Kattis Line Them Up
  9. Toj Hello world!
  10. Kattis Ragged Right
  11. Toj J. Julius Caesar
  12. Toj 跳躍
  13. Kattis The Key to Cryptography
  14. UVA Q11577 Letter Frequency
  15. Kattis Trik
  16. Toj 4. Hello, TFcis!
  17. Zerojudge a011: 00494 - Kindergarten Counting Game
題解
  1. Zerojudge a442: Necklace Problem (NP)
#include <bits/stdc++.h> using namespace std; int main(){ string str1, str2; while(cin >> str1 >> str2){ int count = 0; str2 += str2; if( str2.find(str1, 0) != string::npos ) count++; reverse( str1.begin(), str1.end() ); if( str2.find(str1, 0) != string::npos ) count++; if( count != 0) cout << "same\n"; else cout << "difference\n"; } return 0; }
  1. Zerojudge d535: 2. 密碼驗證與擷取
    函數解:
#include <iostream> #include <algorithm> #include <string> using namespace std; int main(){ string s; cin >> s; string ss = s; reverse(s.begin(), s.end()); //翻轉字串,以確認是否為迴文 if(ss == s){ string ans = ""; int a, b; bool add = false; for(int i=1; i<ss.size(); i++){ a = ss[i-1] - '0'; b = ss[i] - '0'; if(b > a*2){ cout << "INCORRECT\n"; return 0; } if(a%2 == 0){ ans += ss[i - 1]; add = true; } } if(b%2 == 0){ ans += char(b + '0'); } else if(!add) cout << "0"; cout << ans << '\n'; } else cout << "INCORRECT\n"; return 0; }

非函數解

#include <iostream> #include <algorithm> #include <string> using namespace std; int main(){ string s; cin >> s; int len = s.size(); for(int i=0; i<len/2; i++){ if(s[i] != s[len-i-1]){ cout << "INCORRECT\n"; return 0; } } string ans=""; for(int i=0; i<len-1; i++){ if( s[i+1] - s[i] > s[i] - '0' ){ cout << "INCORRECT\n"; return 0; } if( int( s[i] - '0' )%2 == 0 ) ans += s[i]; } if( int( s[len-1] - '0')%2 == 0) ans += s[len-1]; if(ans.size() == 0) cout << '0'; cout << ans << '\n'; return 0; }
  1. Zerojudge a038: 數字翻轉
#include<bits/stdc++.h> using namespace std; int main(){ string str; cin >> str; reverse( str.begin(), str.end() ); cout << stoi(str); return 0; }
  1. Zerojudge e267: 11192 - Group Reverse
#include<bits/stdc++.h> using namespace std; int main(){ string str; int G, n; while( cin >> G && G!=0){ cin >> str; n = str.size() / G; for(int i=0; i<G; i++){ reverse( str.begin() + i*n, str.begin() + (i+1)*n ); } cout << str << '\n'; } return 0; }