00-自测4. 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.

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
#include<iostream>
#include<string>
using namespace std;
int main() {
    int i = 0;
    int j = 0;
    int len;
    int temp;
    string num;
    //判断是否为输入的副本
    bool is_dup = true;
    int judge[20];
    //保存输出结果
    int result[21];
    //保存进位
    int counter[20];

    cin >> num;
    len = num.length();

    for (i = 0; i < 20; i++) {
        counter[i] = 0;
        judge[i] = 0;
    }
    //计算输出结果
    for (i = len - 1; i >= 0; i--) {
        temp = (num[i] - ‘0‘);
        judge[temp]++;
        temp = temp * 2 + counter[i];
        result[j++] = temp % 10;
        if (i != 0)
            counter[i - 1] = temp / 10;
        else {
            result[j++] = temp / 10;
        }
    }
    if (result[j - 1] != 0)
        len = j;

    for (i = 0; i < len; i++) {
        judge[result[i]]--;
    }

    for (i = 0; i < 10; i++) {
        if (judge[i] != 0) {
            is_dup = false;
        }
    }

    if (is_dup)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;

    for (j = len - 1; j >= 0; j--) {
        cout << result[j];
    }
    return 0;
}
时间: 2024-10-02 16:48:42

00-自测4. Have Fun with Numbers的相关文章

pat00-自测4. Have Fun with Numbers (20)

00-自测4. Have Fun with Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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 24691

数据结构练习 00-自测4. Have Fun with Numbers (20)

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

00-自测4. Have Fun with Numbers (20)

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int a[20],b[20],i,len1,len2,flag,j,t,d; char s[25],s2[25]; while(~scanf("%s",s)) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(

PAT自测_打印沙漏、素数对猜想、数组元素循环右移、数字加倍重排、机器洗牌

00-自测1. 打印沙漏(20) 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号:各行符号中心对齐:相邻两行符号数差2:符号数先从大到小顺序递减到1,再从小到大顺序递增:首尾符号数相等. 给定任意N个符号,不一定能正好组成一个沙漏.要求打印出的沙漏能用掉尽可能多的符号. 输入格式: 输入在一行给出1个正整数N(<=1000)和一个符号,中间以空格分隔. 输出格式: 首先

世界顶级64K-3D裸女测试你的显卡

00.非测试图 3D裸女,并非色情(仅64k),未满18岁,显卡不好的朋友请勿下载世界顶级64K 3D美女演示动画 绿色版 软件简介:国外电脑狂人大作!3D美女演示动画,在结尾处会出现一段字幕,说明动画所包含的内容,如178兆多媒体纹理.159MB的wave文件.12条音轨.8亿5千万波样等.18岁以下,不建议下载! 按Win键退出 未满18岁,显卡不好的朋友请勿下载!!!只有64k ,高度压缩算法.... 3D裸女测试你的显卡 3D裸女测试你的显卡.rar 下载;http://pan.baid

JavaScript+svg绘制的一个动态时钟

结果图: 代码如下: <!DOCTYPE html> <html> <head> <title>动态时钟</title> </head> <body > <script type="text/javascript"> function updateTime(){ //更新svg时钟来显示当前时间 var now =new Date(); //当前时间 var min = now.getMin

WeQuant交易策略—KDJ

KDJ随机指标策略 策略介绍KDJ指标又叫随机指标,是一种相当新颖.实用的技术分析指标,它起先用于期货市场的分析,后被广泛用于股市的中短期趋势分析,是期货和股票市场上最常用的技术分析工具. 随机指标KDJ一般是用于股票分析的统计体系,根据统计学原理,通过一个特定的周期(常为9日.9周等)内出现过的最高价.最低价及最后一个计算周期的收盘价及这三者之间的比例关系,来计算最后一个计算周期的未成熟随机值RSV,然后根据平滑移动平均线的方法来计算K值.D值与J值,并绘成曲线图来研判股票走势.适合趋势分析.

WeQuant交易策略—NATR

策略名称:NATR策略关键词:规范真实波幅.价格突破. NATR,是对ATR指标进行了标准化.主要应用于了解价格的震荡幅度和节奏,在窄幅整理行情中用于寻找突破时机.本策略在当前价格高于之前价格一定倍数NATR时全仓买入,低于一定倍数NATR时全仓卖出.方法:1)利用规范化的真实波幅来构造上下轨:2)价格突破上轨买入:3)价格突破下轨卖出. 代码 # !/usr/bin/env python # -*- coding: utf-8 -*- # 策略代码总共分为三大部分,1)PARAMS变量 2)i

WeQuant交易策略—5日均线

简单的价格突破策略.当前价格超过最近5个收盘价的均价,则全仓买入:低于均价,则全仓卖出 代码 # 简单的价格突破策略.当前价格超过最近5个收盘价的均价,则全仓买入:低于均价,则全仓卖出 # PARAMS用于设定程序参数,回测的起始时间.结束时间.滑点误差.初始资金和持仓. PARAMS = { "start_time": "2017-02-01 00:00:00", # 回测起始时间 "end_time": "2017-08-01 00