数据结构练习 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 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>
#include <sstream>
using namespace std;
int sort(int a[],int n){
    int temp;
    for(int i=0;i<n;i++){
        for(int j=i;j<n;j++){
            if(a[i]>a[j]){
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
    return 0;
}
int main(){
    string num;
    stringstream ss;
    int size,j=0;
    cin>>num;
    size=num.size();
    int *a=new int[size];
    int *b=new int[size+1];
    int *doubleNum=new int[size+1];
    for(int i=0;i<size+1;i++){
        doubleNum[i]=0;
        b[i]=0;
    }
    for(int i=0;i<size;i++){
        a[i]=num[i]-48;
    }
    for(int i=size;i>0;i--){
        if(a[i-1]+a[i-1]>=10){
            doubleNum[i]+=(a[i-1]+a[i-1])%10;
            doubleNum[i-1]+=1;
        }else{
            doubleNum[i]+=a[i-1]+a[i-1];
        }
    }
    if(doubleNum[0]==0){
        for(int i=0;i<size;i++){
            b[i+1]=doubleNum[i+1];
        }
        sort(doubleNum,size+1);
        sort(a,size);
        for(int i=0;i<size;i++){
        if(a[i]==doubleNum[i+1]){
            j++;
        }
        }
        if(j==size){
            cout<<"Yes"<<endl;
            for(int i=0;i<size;i++){
                cout<<b[i+1];
            }

        }else{
            cout<<"No"<<endl;
            for(int i=0;i<size;i++){
                cout<<b[i+1];
            }

        }
    }else{
        cout<<"No"<<endl;
        for(int i=0;i<size+1;i++){
            cout<<doubleNum[i];
        }
    }

}
测试结果
 
时间: 2024-10-17 10:57:09

数据结构练习 00-自测4. Have Fun with Numbers (20)的相关文章

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)

#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(

数据结构-起步能力自测题 自测-5 Shuffling Machine(20 分)

Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid "inside jobs" where employees collaborate with gamblers by performing inadequate shuffles, many cas

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

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

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

压测:celey backend为rabbitmq pk redis

使用celery的backend异步获取结果,本文使用rabbitmq 和 redis分别作为backend,代码对比如下 from celery import Celery, platforms import time import os from datetime import datetime app = Celery('proj', broker='amqp://admin:[email protected]:5672', //rabbitmq backend='amqp://admin

大话数据结构——线性表-链式存储之头插法创建链表

1 #include<iostream> 2 3 #include<time.h> 4 #include <stdlib.h> 5 6 using namespace std; 7 8 #define OK 1 9 #define TRUE 1 10 #define FALSE 0 11 #define ERROR 0 12 13 typedef int status;//返回的状态值 14 typedef int elemtype;//节点里数据的类型 15 16 /

电力102规约报文解析

IEC102 应用报文分6大类:通讯连接,读时钟,读历史数据,读实时数据,读事件信息,对时. 传输过程的启动仅限于某一固定点,在规约中主站端为启动站,而电能计量数据终端设备位于计数站,始终为从动站.本系统中的1 级用户数据包括较早时段存储起来的电量和读-应用服务数据单元请求的全部信息.本系统中的数据采用低字节在前,高字节在后的传输方式. 102报文的基本格式: 帧长可以变化102报文的基本格式: 102报文的基本说明: a) 单字符用于子站向主站传输的确认(肯定或否定): b) 控制域是用来区别

Linux 系统应用编程——进程基础

一.Linux下多任务机制的介绍 Linux有一特性是多任务,多任务处理是指用户可以在同一时间内运行多个应用程序,每个正在执行的应用程序被称为一个任务. 多任务操作系统使用某种调度(shedule)策略(由内核来执行)支持多个任务并发执行.事实上,(单核)处理器在某一时刻只能执行一个任务.每个任务创建时被分配时间片(几十到上百毫秒),任务执行(占用CPU)时,时间片递减.操作系统会在当前任务的时间片用完时调度执行其他任务.由于任务会频繁地切换执行,因此给用户多个任务运行的感觉.所以可以说,多任务