【一只蒟蒻的刷题历程】 【PAT】 A1023 玩数字游戏

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


题目大意:

请注意,数字123456789是一个9位数字,完全由1到9组成,没有重复。将其加倍,我们将获得246913578,它恰好是另一个9位数字,恰好由1到9组成,只是排列不同。如果再次将其加倍,请检查结果!

现在,您应该检查此属性是否还有更多数字。也就是说,将给定数字与k位数字加倍,就可以知道结果数是否仅由原始数字中的数字排列组成。

输入规格:

每个输入包含一个测试用例。每个个案包含一个不超过20位的正整数。

输出规格:

对于每个测试用例,如果将输入数字加倍,则首先在“是”行中打印出一个数字,该数字仅包含原始数字中的数字排列,否则,则为“否”。然后在下一行中,打印双倍的数字。

样本输入:

1234567899

样本输出:

Yes
2469135798


思路:

因为这个数可以有20位,显然要用高精度算法,高精度加法和乘法都可以,加法就自己加自己,乘法就自己*2;;都可以算出。。。

这里我两个方法都用了一遍。。

代码(高精度乘法):

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
int main() 
{
   string s;  //输入这个数
   getline(cin,s);
   int a[20],c=0;  //用数组存
   vector<int> v1,v2; //判断是否只用相同的数字,两个vector排序对比
   for(int i=s.size()-1;i>=0;i--)
   {
   	  a[c++]=s[i]-'0';
   	  v1.push_back(s[i]-'0'); //第一个数用到的每一个数
   }
   
   string ans; //存乘法后的答案
   int carry=0; //进位
   for(int i=0;i<c;i++)
   {
   	  int now=a[i]*2 + carry; //每一位都乘2 + 进位
   	  ans += now%10+'0';
   	  v2.push_back(now%10); //第二个数得到的每一个数
	  carry = now/10;   //进位更新
   }
   while(carry)  //如果最后进位还有,那么还要加进去
   {
   	 ans += carry%10+'0';
   	 v2.push_back(carry%10);
   	 carry/=10;
   }

   reverse(ans.begin(),ans.end()); //string倒置后才是答案
   sort(v1.begin(),v1.end());
   sort(v2.begin(),v2.end()); //两个vector排序后进行对比
   if(v1==v2) cout<<"Yes"<<endl<<ans;
   else cout<<"No"<<endl<<ans;
    return 0;
}

代码(高精度加法):

代码稍作修改,就开一个数组和a数组一模一样的存数,然后对位相加就可以了。

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
int main() 
{
   string s;  //输入这个数
   getline(cin,s);
   int a[20],b[20],c=0;  //用数组存
   vector<int> v1,v2; //判断是否只用相同的数字,两个vector排序对比
   for(int i=s.size()-1;i>=0;i--)
   {
   	  a[c] = b[c] =s[i]-'0';   //a[]和b[]一模一样
   	  c++;
   	  v1.push_back(s[i]-'0'); //第一个数用到的每一个数
   }
   
   string ans; //存加法后的答案
   int carry=0; //进位
   for(int i=0;i<c;i++)
   {
   	  int now=a[i]+b[i] + carry; //每个位相加 + 进位
   	  ans += now%10+'0';
   	  v2.push_back(now%10); //第二个数得到的每一个数
	  carry = now/10;   //进位更新
   }
   while(carry)  //如果最后进位还有,那么还要加进去
   {
   	 ans += carry%10+'0';
   	 v2.push_back(carry%10);
   	 carry/=10;
   }

   reverse(ans.begin(),ans.end()); //string倒置后才是答案
   sort(v1.begin(),v1.end());
   sort(v2.begin(),v2.end()); //两个vector排序后进行对比
   if(v1==v2) cout<<"Yes"<<endl<<ans;
   else cout<<"No"<<endl<<ans;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章