PTA (Advanced Level)1023 Have Fun with Numbers

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

题目解析
  本题给出一个长度再20以内的数字(由1~9组成),要求判断这个数字加倍后的新数字是不是这个数字的某一种排列,如果是的化输出Yes否则输出No,之后输出加倍后的数字。

  由于数字最大位数为20位,超过了long long int的记录范围我们用数组num记录这个数字,用数组cnt记录num中1~9出现的次数,将num加倍后判断其中1~9出现的次数是否发送改变,若没有发送改变则证明加倍后的数字是原数字的某一种排列,反之则不是。

AC代码

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int num[22];
 4 int cnt[10];
 5 string str;
 6 void toInt(){
 7     for(int i = 0; i < str.size(); i++)
 8         num[i] = str[i] - ‘0‘;
 9 }
10 void getCnt(){
11     for(int i = 0; i < str.size(); i++)
12         cnt[num[i]]++;
13 }
14 bool judge(int carry){
15     if(carry != 0)  //如果最高位进位不为零,则证明加倍后的数字比原数字多一位,那么其肯定不是原数字的一个排列
16         return false;
17     for(int i = 0; i < str.size(); i++)
18         cnt[num[i]]--;
19     for(int i = 1; i <= 9; i++){    //判断新的num中1~9的数量是否和加倍前一样
20         if(cnt[i] != 0)
21             return false;
22     }
23     return true;
24 }
25 int doubleNumber(){ //将数组num加倍并返回最高位进位
26     int carry = 0;
27     for(int i = str.size() - 1; i >= 0; i--){
28         int temp = num[i];
29         num[i] = (2 * temp + carry) % 10;
30         carry = 2 * temp / 10;
31     }
32     return carry;
33 }
34 int main()
35 {
36     cin >> str; //输入数字
37     toInt();    //将输入的数字转化为数组
38     getCnt();   //获取数组中1~9出现的次数
39     int carry = doubleNumber(); //将num加倍carry记录最高位的进位
40     if(judge(carry)){   //判断加倍后的数字是否为原数字的某一个排列
41         printf("Yes\n");
42
43     }else
44         printf("No\n");
45     if(carry != 0)  //判断是否需要输出进位
46         printf("%d", carry);
47     for(int i = 0; i < str.size(); i++) //输出加倍后的数组num
48         printf("%d", num[i]);
49     printf("\n");
50     return 0;
51 }

原文地址:https://www.cnblogs.com/suvvm/p/10311268.html

时间: 2024-11-14 12:44:03

PTA (Advanced Level)1023 Have Fun with Numbers的相关文章

PTA(Advanced Level)1023.Palindromic Number

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic numbers can be paired with palin

PAT (Advanced Level) 1023. Have Fun with Numbers (20)

手动模拟一下高精度加法. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; char s[30],t[30],c[30]; int lens,len

PTA(Advanced Level)1036.Boys vs Girls

This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students. Input Specification: Each input file contains one test case. Each case contains a positive integer N, f

PTA(Advanced Level)1025.PAT Ranking

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by e

PTA (Advanced Level)1066 Root of AVL Tree

Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. F

PTA (Advanced Level)1018 Public Bike Management

Public Bike Management There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Manag

PTA (Advanced Level)1001.A+B Format

Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). Input Specification: Each input file contains one test case. Each case contains a pa

PTA (Advanced Level)1022 Digital Library

Digital Library A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a r

PTA(Advanced Level)1011.World Cup Betting

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their