日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【详细分析】1023 Have Fun with Numbers (20 分)_20行代码AC

發布時間:2024/2/28 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【详细分析】1023 Have Fun with Numbers (20 分)_20行代码AC 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

立志用最少的代碼做最高效的表達


PAT甲級最優題解——>傳送門


Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

Input Specification:
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

Output Specification:
For each test case, first print in a line “Yes” if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or “No” if not. Then in the next line, print the doubled number.

Sample Input:
1234567899

Sample Output:
Yes
2469135798


最開始把題看錯。。 以為輸出No后不需要輸出序列, 來來回回折騰了一個小時才A掉。 大意啦沒有閃(我可真蠢=_=|| ,乙級中文題都容易出錯,何況英文題)。

優化:只需開一個數組即可。遍歷輸入數字時++,遍歷其double數字時- -。最后判斷是否為0即可。


#include<bits/stdc++.h> using namespace std;int a[30];int main() {string s, double_s; cin >> s;int carry = 0, x = 0;for(int i = s.size()-1; i >= 0; i--) {a[s[i]-'0']++; x = ((s[i]-'0')*2+carry) % 10;carry = ((s[i]-'0')*2+carry)/10;double_s.insert(0, 1, '0'+x);a[x]--;}if(carry > 0) { a[carry]--; double_s.insert(0, 1, '0'+carry); }bool flag = false;for(int i = 0; i < 30; i++) if(a[i] != 0) flag = true;cout << (flag?"No":"Yes") << '\n' << double_s << '\n';return 0; }

耗時:

總結

以上是生活随笔為你收集整理的【详细分析】1023 Have Fun with Numbers (20 分)_20行代码AC的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。