PAT Advanced 1023 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 diferent 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

题目分析

已知一个最多20位的数字N,判断N乘2后的数字M是否只是组成N的各位数字的重新排列

解题思路

  1. 用一个数组记录N中各位数字出现的次数,乘2后的数字M中出现相同数字则出现次数减一(该判断隐含M和N的长度不相等的情况,长度不相等则出现次数一定不相等,数组中出现次数不相等时元素值不为0)
  2. 大整数相乘算法

易错点

  1. 字符和数字的转换(字符-‘0‘=数字)

知识点

  1. 字符串转换
#include <algorithm>
reverse(ds.begin(),ds.end());

Code

Code 01

#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc,char * argv[]) {
    string s,ds;
    cin>>s;
    int sf[10]= {0},carry=0;
    for(int i=s.length()-1; i>=0; i--) {
        sf[s[i]-'0']++;
        int temp=(s[i]-'0')*2+carry;
        ds.push_back(temp%10+'0');
        carry=temp/10;
        sf[temp%10]--;
    }
    while(carry!=0) {
        ds.push_back(carry%10+'0');
        sf[carry%10]--;
        carry/=10;
    }

    bool flag = true;

//  strrev(s.c_str());
//  if(ds.length()!=s.length())flag=false;
//  else {}
    for(int i=0; i<10; i++) {
        if(sf[i]!=0) {
            flag = false;
            break;
        }
    }
    printf("%s", flag? "Yes\n" : "No\n");
    reverse(ds.begin(),ds.end());
    printf("%s",ds.c_str());
    return 0;
}

Code 02

#include <cstdio>
#include <string.h>
using namespace std;
int book[10];
int main() {
    char num[22];
    scanf("%s", num);
    int flag = 0, len = strlen(num);
    for(int i = len - 1; i >= 0; i--) {
        int temp = num[i] - '0';
        book[temp]++;
        temp = temp * 2 + flag;
        flag = 0;
        if(temp >= 10) {
            temp = temp - 10;
            flag = 1;
        }
        num[i] = (temp + '0');
        book[temp]--;
    }
    int flag1 = 0;
    for(int i = 0; i < 10; i++) {
        if(book[i] != 0)
            flag1 = 1;
    }
    printf("%s", (flag == 1 || flag1 == 1) ? "No\n" : "Yes\n");
    if(flag == 1) printf("1");
    printf("%s", num);
    return 0;
}

原文地址:https://www.cnblogs.com/houzm/p/12268856.html

时间: 2024-10-10 02:51:52

PAT Advanced 1023 Have Fun with Numbers (20) [?整数运算]的相关文章

PAT:1023. Have Fun with Numbers (20) AC

#include<stdio.h> #include<string.h> char str[30]; //输入的数字 int tmp[30]; //*2后逆序的数字 int cntstr[10]; //输入数字0-9的个数 int tmpI=0,jin=0; //*2的时候的int数组下标和进位数 void Double(char* str,int len1) { memset(tmp,0,sizeof(tmp)); //将str*2并按个位在左,高位在右方式存储 for(int

PAT 1069. The Black Hole of Numbers (20)

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in

PAT乙级1023. 组个最小数(20 分)

1023 组个最小数(20 分) 给定数字 0-9 各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意 0 不能做首位).例如:给定两个 0,两个 1,三个 5,一个 8,我们得到的最小的数就是 10015558. 现给定数字,请编写程序输出能够组成的最小的数. 输入格式: 输入在一行中给出 10 个非负整数,顺序表示我们拥有数字 0.数字 1.……数字 9 的个数.整数间用一个空格分隔.10 个数字的总个数不超过 50,且至少拥有 1 个非 0 的数字

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

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

PAT 甲级 1023 Have Fun with Numbers

https://pintia.cn/problem-sets/994805342720868352/problems/994805478658260992 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 b

1023 Have Fun with Numbers (20分)

模拟整数乘法,比较简单的乘法模拟,因为一个因数是2,只有一位.注意处理可能产生的进位,测试点2和7测的就是这个.(理解题意很重要,pat可能有的题不难,但是得仔细琢磨坑点在哪里) 用digit1[]记录原数字中各位数字的出现次数,digit2[]存储结果中各位数字出现的次数,逐个比较,如果次数不相等,可令标志flag=0,表明两数中有数字出现的数字不一样. 1 #include <iostream> 2 #include<cstdio> 3 #include<string&g

PAT (Advanced Level) 1001. A+B Format (20)

简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> using namespace std; int a,b,tot,cnt; int u[20]; char ans[100]; int main() { while(~scanf("%d%d",&a,&b)) { a=a+b

PAT (Advanced Level) 1005. Spell It Right (20)

简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<vector> using namespace std; const int maxn=100+10; char s[maxn]; int tmp[maxn],tot; char ans[15][7]={ "zero&q