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
題目大意:
請注意,數字123456789是一個9位數字,完全由1到9組成,沒有重複。將其加倍,我們將獲得246913578,它恰好是另一個9位數字,恰好由1到9組成,只是排列不同。如果再次將其加倍,請檢查結果!
現在,您應該檢查此屬性是否還有更多數字。也就是說,將給定數字與k位數字加倍,就可以知道結果數是否僅由原始數字中的數字排列組成。
輸入規格:
每個輸入包含一個測試用例。每個個案包含一個不超過20位的正整數。
輸出規格:
對於每個測試用例,如果將輸入數字加倍,則首先在“是”行中打印出一個數字,該數字僅包含原始數字中的數字排列,否則,則爲“否”。然後在下一行中,打印雙倍的數字。
樣本輸入:
1234567899
樣本輸出:
Yes
2469135798
思路:
因爲這個數可以有20位,顯然要用高精度算法,高精度加法和乘法都可以,加法就自己加自己,乘法就自己*2;;都可以算出。。。
這裏我兩個方法都用了一遍。。
代碼(高精度乘法):
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
int main()
{
string s; //輸入這個數
getline(cin,s);
int a[20],c=0; //用數組存
vector<int> v1,v2; //判斷是否只用相同的數字,兩個vector排序對比
for(int i=s.size()-1;i>=0;i--)
{
a[c++]=s[i]-'0';
v1.push_back(s[i]-'0'); //第一個數用到的每一個數
}
string ans; //存乘法後的答案
int carry=0; //進位
for(int i=0;i<c;i++)
{
int now=a[i]*2 + carry; //每一位都乘2 + 進位
ans += now%10+'0';
v2.push_back(now%10); //第二個數得到的每一個數
carry = now/10; //進位更新
}
while(carry) //如果最後進位還有,那麼還要加進去
{
ans += carry%10+'0';
v2.push_back(carry%10);
carry/=10;
}
reverse(ans.begin(),ans.end()); //string倒置後纔是答案
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end()); //兩個vector排序後進行對比
if(v1==v2) cout<<"Yes"<<endl<<ans;
else cout<<"No"<<endl<<ans;
return 0;
}
代碼(高精度加法):
代碼稍作修改,就開一個數組和a數組一模一樣的存數,然後對位相加就可以了。
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
int main()
{
string s; //輸入這個數
getline(cin,s);
int a[20],b[20],c=0; //用數組存
vector<int> v1,v2; //判斷是否只用相同的數字,兩個vector排序對比
for(int i=s.size()-1;i>=0;i--)
{
a[c] = b[c] =s[i]-'0'; //a[]和b[]一模一樣
c++;
v1.push_back(s[i]-'0'); //第一個數用到的每一個數
}
string ans; //存加法後的答案
int carry=0; //進位
for(int i=0;i<c;i++)
{
int now=a[i]+b[i] + carry; //每個位相加 + 進位
ans += now%10+'0';
v2.push_back(now%10); //第二個數得到的每一個數
carry = now/10; //進位更新
}
while(carry) //如果最後進位還有,那麼還要加進去
{
ans += carry%10+'0';
v2.push_back(carry%10);
carry/=10;
}
reverse(ans.begin(),ans.end()); //string倒置後纔是答案
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end()); //兩個vector排序後進行對比
if(v1==v2) cout<<"Yes"<<endl<<ans;
else cout<<"No"<<endl<<ans;
return 0;
}