tgirc早修book
題目
#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; }
#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; }
#include<bits/stdc++.h> using namespace std; int main(){ string str; cin >> str; reverse( str.begin(), str.end() ); cout << stoi(str); return 0; }
#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; }
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up