1023 Have Fun with Numbers (20分)

模拟整数乘法,比较简单的乘法模拟,因为一个因数是2,只有一位。注意处理可能产生的进位,测试点2和7测的就是这个。(理解题意很重要,pat可能有的题不难,但是得仔细琢磨坑点在哪里)

用digit1[]记录原数字中各位数字的出现次数,digit2[]存储结果中各位数字出现的次数,逐个比较,如果次数不相等,可令标志flag=0,表明两数中有数字出现的数字不一样。

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<vector>
 5 using namespace std;
 6 int main()
 7 {
 8   string s;
 9   cin>>s;
10   int size=s.size();
11   vector<int> res;
12   int carry=0,temp=0;
13   int digit1[10]={0}, digit2[10]={0};
14   for(int i=size-1;i>=0;i--){
15         temp=(s[i]-‘0‘)*2+carry;
16         res.push_back(temp%10);
17         carry=temp/10;
18
19         digit1[s[i]-‘0‘]++;
20         digit2[temp%10]++;
21
22         if(i==0&&carry!=0)//处理可能的进位
23         { res.push_back(carry);
24           digit2[carry]++;
25           size++;
26         }
27   }
28   int flag=1;
29   for(int i=0;i<10;i++)
30      if(digit1[i]!=digit2[i])
31          flag=0;
32
33   if(flag==1)
34      cout<<"Yes"<<endl;
35   else
36      cout<<"No"<<endl;
37   for(int i=size-1;i>=0;i--)
38      cout<<res[i];
39   return 0;
40 }

原文地址:https://www.cnblogs.com/wsshub/p/12541554.html

时间: 2024-07-30 16:00:52

1023 Have Fun with Numbers (20分)的相关文章

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

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

1120 Friend Numbers (20 分)

1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same sum of their digits, and the sum is their "friend ID". For example, 123 and 51 are friend numbers since 1+2+3 = 5+1 = 6, and 6 is their friend I

PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)

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

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

PAT Advanced 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 (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

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

1023 组个最小数(20 分)

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