1023 Have Fun with Numbers (20分)+1024 Palindromic Number (25分) 兩道大數題

兩道大數題

目錄

1023 Have Fun with Numbers (20分)

Input Specification:

Output Specification:

Sample Input:

Sample Output:

1024 Palindromic Number (25分)

Input Specification:

Output Specification:

Sample Input 1:

Sample Output 1:

Sample Input 2:

Sample Output 2:


 

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 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位的字符串,將每一位乘2,得到的新字符串,與老字符串對比,如果是老字符串中數字的重新排列,輸出Yes,不是的話,輸出No,再輸出新字符串。

#include<iostream>
#include<cstring>
using namespace std;
const int r=25;
int d[r],count1[r],count2[r],cnt[r];
char a[r];
int main(){
	int i,j,n=0,l,len,f=0;
	fill(count1,count1+r,0);
	fill(count2,count2+r,0);
	fill(cnt,cnt+r,0);
	scanf("%s",a);
	len=strlen(a);
	for(i=0;i<len;i++){      // 逆置數組 
		d[len-1-i]=a[i]-'0';
		count1[d[len-1-i]]++;
	}
	for(i=0;i<len;i++){
		l=d[i]*2;
		if(l>9){
			d[i]=l%10;
			cnt[i+1]=l/10;
			if(i+1>=len){
				n=i+1;
			}
		}
		else
		d[i]=l;
		if(i!=0){
			d[i-1]+=cnt[i-1];
		}
	}
	d[len-1]+=cnt[len-1];   // len-1的進位別忘了 
	if(n>len-1){
		d[len]+=cnt[len];
		len=n+1;
	}
	
	for(i=len-1;i>=0;i--){  // 統計 
		count2[d[i]]++;
	}
	for(i=0;i<10;i++){
		if(count1[i]!=count2[i]){
			f=1;
			break;
		}
	}
	if(f){
		printf("No\n");
		for(i=len-1;i>=0;i--){
			printf("%d",d[i]);
		}
	}
	else
	{
		printf("Yes\n");
		for(i=len-1;i>=0;i--){
			printf("%d",d[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 (≤10​10​​) 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

思路很好想。

#include<iostream>
#include<algorithm>
#include<string>

using namespace std;
string add(string s){
	int i,j,k,len=s.length(),cnt=0;
	string t;
    t.assign(s.rbegin(),s.rend());
//    cout<<s<<"  "<<t<<endl;
    for(i=0;i<len;i++){
    	k=t[i]-'0'+s[i]-'0'+cnt;
//    	cout<<k<<"  "<<cnt<<endl;
    	if(k>9){
    		s[i]=k%10+'0';
    		cnt=k/10;
		}
		else{
			s[i]=k+'0';
			cnt=0;
		}
	}
	
	if(cnt!=0){    //  進位 
		s.append(1,cnt+'0');
		
	}
//	cout<<cnt<<"  "<<t<<endl;allocator_type
    reverse(s.begin(),s.end());
//    cout<<s<<endl;
	return s;
}
bool judge(string s){
	int i,j,len=s.length();
	j=len/2;
	for(i=0;i<j;i++){
		if(s[i]!=s[len-1-i])
		return false;
	}
	return true;
}
int main(){
	string d,t;
	int i,j,k;
	cin>>d;
	cin>>k;
	
	for(i=1;i<=k;i++){
		
		if(judge(d)){
			cout<<d<<endl<<i-1<<endl;
			return 0;
		}
		else{
		    t=add(d);
		    d=t;
		}
	}
	cout<<t<<endl<<k<<endl;
	return 0;
}

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章