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;
}
相关推荐
程序猿编码1 分钟前
轻量又灵活:一款伪造TCP数据包的iptables扩展实现解析(C/C++代码实现)
linux·c语言·网络·c++·tcp/ip·内核·内核模块
j_xxx404_1 分钟前
LeetCode模拟算法精解I:替换问号,提莫攻击与Z字形变换
开发语言·数据结构·c++·算法·leetcode
青槿吖4 分钟前
第二篇:Spring MVC进阶:注解、返回值与参数接收的花式玩法
java·开发语言·后端·mysql·spring·mvc·mybatis
共享家95275 分钟前
Java入门(抽象类 与 接口)
java·开发语言
hanbr6 分钟前
C++ string类模拟实现(完整版,含全运算符重载)
java·开发语言
xUxIAOrUIII7 分钟前
【Go每日面试题】内存管理
java·开发语言·golang
勇闯逆流河8 分钟前
【Linux】linux进程概念(fork,进程状态,僵尸进程,孤儿进程)
linux·运维·服务器·开发语言·c++
森屿山茶9 分钟前
hot100题解 —— 146.LRU缓存
java·开发语言
70asunflower9 分钟前
CUDA基础知识巩固检验练习题【附有参考答案】(7)
c++·人工智能·cuda
superkcl202213 分钟前
C++初始化 和 赋值
开发语言·c++·算法