pat 甲级 训练真题集 1021! 1023. Have Fun with Numbers (20) 1024. Palindromic Number (25)

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

代码如下:

#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;



vector<int> compute(string s){
	vector<int> vec(10,0);
	int value=0;
	for(int i=0;i<s.size();++i){
		stringstream ss;
		ss<<s[i];
		ss>>value;
		vec[value]=vec[value]+1;
	}
	return vec;
}

int main(){
	string s1,s2="";
	cin>>s1;
	int value=0;
	int carry=0;
	string s;
	int flag1=0;
	for(int i=s1.size()-1;i>=0;--i){
		stringstream ss,ss1;
		ss<<s1[i];
		ss>>value;
		value=carry+(value<<1);
		flag1=0;
		if(value>=10){
			carry=value/10;
			value%=10;
			flag1=1;
		}else{
			carry=0;
		}
		ss1<<value;
		ss1>>s;
		s2+=s;
	}
	vector<int> vec1(10,0);
	vector<int> vec2(10,0);
	
	vec1=compute(s1);
	vec2=compute(s2);
	int flag=1;
	for(int i=0;i<vec1.size();++i){
			if(vec1[i]==vec2[i]){
				continue;
			}else{
				flag=0;
				break;
			}
	}
	if(flag==1){
		cout<<"Yes"<<endl;
	}else{
		cout<<"No"<<endl;
	}
	if(flag1==1) cout<<"1";
	for(int i=s2.size()-1;i>=0;--i){
		cout<<s2[i];
	}
	return 0;
}

1024. Palindromic Number (25)

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 palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (<= 1010) is the initial numer and K (<= 100) is the maximum number of steps. The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

Sample Input 1:

67 3

Sample Output 1:

484
2

Sample Input 2:

69 3

Sample Output 2:

1353
3

代码如下:

最开始没用到algorithm中的reverse,写了一个反转函数。。

用了之后爽翻。。

#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;

int judge_palindromic(string s){
	string s1="";
	for(int i=s.size()-1;i>=0;--i){
		s1+=s[i];
	}
	if(s1==s){
		return 1;
	}else{
		return 0;
	}
}
string add_num(string s){
	string sum="";
	string s1="";
	for(int i=s.size()-1;i>=0;--i){
		s1+=s[i];
	}
	int flag=0;
	int value=0;
	int carry=0;
	int a,b;
	string change_s;
	for(int i=s.size()-1;i>=0;--i){
		a=s[i]-'0';
		b=s1[i]-'0';
		value=(a+b)+carry;
		flag=0;
		if(value>=10){
			flag=1;
			carry=value/10;
			value%=10;
		}else{
			carry=0;
		}
		stringstream ss;
		ss<<value;
		ss>>change_s;
		sum+=change_s;
		
	}
	if(flag==1) sum+="1";
	return sum;

}
int main(){
	string N;
	int K;
	cin>>N>>K;
	int x=judge_palindromic(N);

	
	string sum=N;
	int i=0;
	for(;i<K;++i){
		x=judge_palindromic(sum);
		if(x==1){
			cout<<sum<<endl;
			cout<<i<<endl;
			return 0;
		}
		sum=add_num(sum);				
	}

	if(i==K){
		reverse(sum.begin(),sum.end());
		cout<<sum<<endl;
		cout<<K<<endl;
	}
	return 0;
}