1023 Have Fun with Numbers (20分)

Posted wsshub

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

以上是关于1023 Have Fun with Numbers (20分)的主要内容,如果未能解决你的问题,请参考以下文章

1023 Have Fun with Numbers

1023 Have Fun with Numbers

PAT 1023 Have Fun with Numbers

PAT 甲级 1023 Have Fun with Numbers

1023 Have Fun with Numbers (20)

1023 Have Fun with Numbers (20)(20 point(s))