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;
}
相关推荐
XMYX-04 小时前
03 - Go 常用类型速查表 + 实战建议(实战向)
开发语言·golang
爱码驱动4 小时前
Java多线程详解(5)
java·开发语言·多线程
6Hzlia4 小时前
【Hot 100 刷题计划】 LeetCode 45. 跳跃游戏 II | C++ 贪心算法最优解题解
c++·leetcode·游戏
@atweiwei5 小时前
用 Rust 构建 LLM 应用的高性能框架
开发语言·后端·ai·rust·langchain·llm
九转成圣5 小时前
实战记录:用 Java 拼接长图/网格图,我踩了哪些坑?
java·开发语言
lzhdim5 小时前
SQL 入门 9:SQL 高级子查询:ANY、EXISTS 与多位置应用
java·开发语言·数据库·sql·mysql
Dream of maid5 小时前
Python(11) 进程与线程
开发语言·python
森G5 小时前
48、柱状图---------QChart
c++·qt
cici158745 小时前
非线性模型预测控制(NMPC)基于CasADi的MATLAB实现
开发语言·matlab
Tanecious.5 小时前
蓝桥杯备赛:Day8-小苯的异或和
c++·蓝桥杯