C++: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 kk 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
cpp 复制代码
#include <iostream>
#include <bits/stdc++.h>
#include <cstring>
#include <iomanip>
using namespace std;
int main(){
    string s;cin>>s;
    string ans;
    int l=s.size();
    int q=0,k=0;
    for(int i=l-1;i>=0;--i){
        k=s[i]-'0';
        k=k*2+q;
        q=k/10;
        k=k%10;
        ans+=to_string(k);
    }
    if(q)ans+=to_string(q);
    reverse(ans.begin(),ans.end());
    int flag=1;
    if(ans.size()!=s.size()){
        flag=0;
    }
    else{
    for(int i=0;i<ans.size();++i){
        if(s.find(ans[i])==string::npos){
            flag=0;break;
        }
    }
    }
    if(flag){
        cout<<"Yes"<<endl;
        cout<<ans<<endl;
    }
    else{
        cout<<"No"<<endl;
        cout<<ans<<endl;
    }
    return 0;
}
相关推荐
mjhcsp1 小时前
C++轮廓线 DP:从原理到实战的深度解析
开发语言·c++·动态规划
ArturiaZ1 小时前
【day36】
数据结构·c++·算法
额,不知道写啥。2 小时前
P5354 [Ynoi Easy Round 2017] 由乃的 OJ
java·开发语言·算法
代码无bug抓狂人2 小时前
C语言之单词方阵——深搜(很好的深搜例题)
c语言·开发语言·算法·深度优先
青桔柠薯片2 小时前
Linux软件编程:线程和进程间通信
linux·开发语言·线程·进程
foundbug9992 小时前
基于C# WinForm实现串口数据读取与实时折线图显示
开发语言·c#
CodeJourney_J2 小时前
从“Hello World“ 开始 C++
c语言·c++·学习
匠心网络科技2 小时前
JavaScript进阶-ES6 带来的高效编程新体验
开发语言·前端·javascript·学习·面试