比較水的一道題,用string存儲輸入,然后*2成另一個string,通過sort后比較是否一樣來判斷Y/N
#include<iostream>#include<string>#include<algorithm>using namespace std;string double_str(string str){ int temp=0; for (auto it = str.rbegin();it != str.rend();it++) { if ((*it - '0') * 2 + temp > 9) { *it = (*it - '0') * 2 + temp - 10 + '0'; temp = 1; } else { *it=(*it - '0') * 2 + temp + '0'; temp = 0; } } if (temp == 1) str.insert(str.begin(), '1'); return str;}int main(){ string str1, str2,str3; cin >> str1; str2 = double_str(str1); str3 = str2; sort(str1.begin(), str1.end()); sort(str2.begin(), str2.end()); if (str1 == str2) cout << "Yes" << endl<<str3 << endl; else cout << "No" << endl << str3 << endl;}新聞熱點
疑難解答