1023 Have Fun with Numbers
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.
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
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.
1234567899
- Yes
- 2469135798
2022.11.1(可能之前忘记写了)
代码:两次代码思路都是一样的
- //思路:先将原始出现的数字存储下来,然后将数字乘2(高精度乘法),最后判断2倍之后的数字是否
- //出现了和原始数字不同的数字
- #include
- #include
- using namespace std;
-
- vector<int> b(10);
- vector<int> mul(vector<int> &a,int b){
- vector<int> c;
- int t=0;
-
- for(int i=0;i
size() || t;i++){ - t+=a[i]*b;
- c.push_back(t%10);
- t/=10;
- }
- //除前导0的情况是,0000*1234,结果为0000,那么需要除去前导0,变成0
- while(c.size()>1 && c.back()==0) c.pop_back();
- return c;
- }
- int main(){
- string s;
- vector<int> a;
- cin >> s;
-
- for(auto c:s) b[c-'0']++;//记录出现的数字
- for(int i=s.size()-1;i>=0;i--) a.push_back(s[i]-'0');
- auto c=mul(a,2);
- bool sign=false;
- for(auto q:c){
- b[q]--;
- if(b[q]<0){
- sign=true;
- break;
- }
- }
- if(!sign) puts("Yes");
- else puts("No");
- for(int i=c.size()-1;i>=0;i--) printf("%d",c[i]);
- return 0;
- }
好好学习,天天向上!
我要考研!