problem
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 file 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
tip
answer
#include<iostream>
#include<set>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
string a, da;
int na[22], nda[22];
void GetNum(string t, int *n){
if(t == "0") {
n[0]++;
return ;
}
for(int i = 0; i < t.size(); i++){
// s.insert(t[i]-'0');
n[t[i]-'0']++;
}
return ;
}
string Double(string t){
string tt = "";
reverse(t.begin(), t.end());
int last = 0, th = 0;
for(int i = 0; i < t.size(); i++){
th = 2*(t[i]-'0') + last;
tt.push_back(th%10 + '0');
last = th/10;
}
if(last != 0) tt.push_back(last+'0');
// cout<<tt<<endl;
return tt;
}
void PrintStatus(int *a){
for(int i = 0; i < 10; i++){
printf("%d ", a[i]);
}
printf("\n");
}
int main(){
// freopen("test.txt", "r", stdin);
cin>>a;
da = Double(a);
memset(na, 0, sizeof(na));
memset(nda, 0, sizeof(nda));
GetNum(a, na);
GetNum(da, nda);
// PrintStatus(na);
// PrintStatus(nda);
bool flag = true;
for(int i = 0; i < 10; i++){
if(na[i] != nda[i]) flag = false;
}
if(flag) puts("Yes");
else puts("No");
reverse(da.begin(), da.end());
cout<<da;
return 0;
}
exprience
- 英語單詞
- puts 與cout<<endl 差異
- Cout是istream類的預約義對象,puts是預約義函數(庫函數)。
- cout是一個對象,它使用重載插入(<<)運算符函數來打印數據。 可是put是完整的函數,它不使用重載的概念。
- cout能夠打印數字和字符串。 而puts只能打印字符串。
- cout在內部使用flush而puts並無,爲了刷新stdout,咱們必須明確地使用fflush函數。
- 要使用puts,咱們須要包含stdio.h頭文件。 在使用cout時咱們須要包含iostream.h頭文件。
- puts函數會在結尾增長'\n'。