PTA-数学问题(1069/1104/1008/1049/1081/1088/1015/1078/1096/1059/1023/1024)

数学模拟

PTA-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 this manner we will soon end up at the number 6174 – the black hole of 4-digit numbers. This number is named Kaprekar Constant.

For example, start from 6767, we’ll get:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
… …
Given any 4-digit number, you are supposed to illustrate the way it gets into the black hole.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range (0,10^4).

Output Specification:

If all the 4 digits of N are the same, print in one line the equation N - N = 0000. Else print each step of calculation in a line until 6174 comes out as the difference. All the numbers must be printed as 4-digit numbers.

Sample Input 1:

6767

Sample Output 1:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174

Sample Input 2:

2222

Sample Output 2:

2222 - 2222 = 0000

思路

basic_string& insert (size_type pos, const basic_string& str);在原串下标为pos的字符前插入字符串str
basic_string& insert (size_type pos, size_type n, char c);在原串下标为pos的字符前插入n个字符c

这里参考了柳神的代码,简洁明了。a.insert(0, 4 - a.length(), '0')实现了在字符串前补0到四位,再利用字符串和整形的转换解决问题十分方便。

#include<bits/stdc++.h>
using namespace std;
bool cmp1(char a,char b)
{
	return a>b;
}
int main()
{
	string a;
	cin>>a;
	a.insert(0, 4 - a.length(), '0');
	while(1)
	{
		string x=a,y=a;
		sort(x.begin(),x.end(),cmp1);
		sort(y.begin(),y.end());
		int z=stoi(x)-stoi(y);
		a=to_string(z);
		a.insert(0,4-a.length(),'0');
		printf("%s - %s = %s\n",x.c_str(),y.c_str(),a.c_str());
		if(z==0||z==6174) break; 
	}
	return 0;
}

PTA-1104 Sum of Number Segments (20 分)

Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3, 0.4 }, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) and (0.4).

Now given a sequence, you are supposed to find the sum of all the numbers in all the segments. For the previous example, the sum of all the 10 segments is 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N, the size of the sequence which is no more than 10^5. The next line contains N positive numbers in the sequence, each no more than 1.0, separated by a space.

Output Specification:

For each test case, print in one line the sum of all the numbers in all the segments, accurate up to 2 decimal places.

Sample Input:

4
0.1 0.2 0.3 0.4

Sample Output:

5.00

思路

这是一道找规律的题,通过分析n=4,5,6,7时序列中每一个位置的数的出现次数,得出总的递推公式求解。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n; 
	long long sum=0;
	for(int i=1;i<=n;i++)
	{
		double s;
		cin>>s;
		sum+=(long long)(s*1000)*i*(n+1-i);
	}
	printf("%.2lf",sum/1000.0);
	return 0;
}

PTA-1008 Elevator (20 分)

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:

3 2 3 1

Sample Output:

41

思路

依照题意直接模拟即可。

#include<iostream>
using namespace std;
const int maxx=101;
int a[maxx];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++) cin>>a[i];
	int sum=6*a[0]+5*n;
	for (int i = 1; i < n; i++)
	{
		if(a[i]>=a[i-1]) sum+=6*(a[i]-a[i-1]);
		else sum+=4*(a[i-1]-a[i]);
	}
	cout<<sum<<endl;
	return 0;
}

PTA-1049 Counting Ones (30 分)

The task is simple: given any positive integer N, you are supposed to count the total number of 1’s in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1’s in 1, 10, 11, and 12.

Input Specification:

Each input file contains one test case which gives the positive N (≤2^30).

Output Specification:

For each test case, print the number of 1’s in one line.

Sample Input:

12

Sample Output:

5

思路

这道题直接模拟会超时,应该找到其中的技巧进行模拟,我们对每一位可能出现的1进行统计。
对于70310这个数,从低位到高位进行分析:
第一位的0,在第一位出现1的可能只有00001-70301这7031个数,因为取前四位为7031时,只能取到70310;
第二位的1,在第二位出现1的可能在0001x-7031x这7032个数,因为取前三位为703时,可以取到7031x;还有可能在7031x时,取到当x个1,所以我们分类讨论:
当此位置为1时,左边能取到0001x-7030x这703*10个1,而右边能取到70310-70310这1个数;
第三位的3,在第三位出现1的可能在001xx-701xx这71*100个数;
由此,我们得到递推公式:
设当前位数为now,当前位左边的数为left,右边的数为right,ans为总共出现1的次数,a为当前的位数,初始为1
if(now==0) ans+=left*a; if(now==1) ans+=left*a+right+1; if(now>=2) ans+=(left+1)*a;
每次计算后要a=a*10;代表向高位移动一位。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	long long int ans=0;
	cin>>n;
	int a=1;
	while(n/a>0)
	{
		int left=n/(a*10);
		int now=n/a%10;
		int right=n%a;
		if(now==0) ans+=left*a;
		else if(now==1) ans+=left*a+right+1;
		else if(now>=2) ans+=(left+1)*a;
		a=a*10;
	}
	printf("%lld",ans);
	return 0;
}

分数的四则运算

PTA-1081 Rational Sum (20 分)

Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum.

Input Specification:

Each input file contains one test case. Each case starts with a positive integer N (≤100), followed in the next line N rational numbers a1/b1 a2/b2 … where all the numerators and denominators are in the range of long int. If there is a negative number, then the sign must appear in front of the numerator.

Output Specification:

For each test case, output the sum in the simplest form integer numerator/denominator where integer is the integer part of the sum, numerator < denominator, and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.

Sample Input 1:

5
2/5 4/15 1/30 -2/60 8/3

Sample Output 1:

3 1/3

Sample Input 2:

2
4/3 2/3

Sample Output 2:

2

Sample Input 3:

3
1/3 -1/6 1/8

Sample Output 3:

7/24

思路

这是一道分数四则运算的模拟题,了解分数四则运算的基本处理方式即可ac。
最后需要按照题目要求,将假分数化为带分数即可。

#include<bits/stdc++.h>
using namespace std;
struct Fra
{
	long long up,down;
};
long long gcd(long long a,long long b)
{
	if(b==0) return a;
	else return gcd(b,a%b);
}
Fra reduce(Fra a)
{
	if(a.down<0)
	{
		a.up=-a.up;
		a.down=-a.down;
	}
	if(a.up==0)
	{
		a.down=1;
	}
	else
	{
		long long d=gcd(abs(a.up),abs(a.down));
		a.up/=d;
		a.down/=d;
	}
	return a;
}
Fra add(Fra a,Fra b)
{
	Fra res;
	res.up=a.up*b.down+a.down*b.up;
	res.down=a.down*b.down;
	return reduce(res);
}
int main()
{
	int n;
	cin>>n;
	vector<Fra> ans(n);
	for(int i=0;i<n;i++)
	{
		scanf("%lld/%lld",&ans[i].up,&ans[i].down);
	}
	Fra a=ans[0];
	for(int i=1;i<n;i++)
	{
		a=add(ans[i],a);
	}
	if(a.down==1) printf("%lld",a.up);
	else if(a.up>a.down)
	{
		printf("%lld %lld/%lld",a.up/a.down,abs(a.up)%a.down,a.down);
	}
	else if(a.up<a.down)
	{
		printf("%lld/%lld",a.up,a.down);
	}
	return 0;
}

PTA-1088 Rational Arithmetic (20 分)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:

Each input file contains one test case, which gives in one line the two rational numbers in the format a1/b1 a2/b2. The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is number1 operator number2 = result. Notice that all the rational numbers must be in their simplest form k a/b, where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output Inf as the result. It is guaranteed that all the output integers are in the range of long int.

Sample Input 1:

2/3 -4/2

Sample Output 1:

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

Sample Input 2:

5/3 0/6

Sample Output 2:

1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf

思路

相比于上一道题略微复杂,是分数的四则运算,负数加上括号,如果是0单独输出,如果分母为0,应输出Inf。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Fra
{
	ll up,down;
};
ll gcd(ll a,ll b)
{
	if(b==0) return a;
	else return gcd(b,a%b);
}
Fra reduce(Fra a)
{
	if(a.down<0)
	{
		a.up=-a.up;
		a.down=-a.down;
	}
	if(a.up==0)
	{
		a.down=1;
	}
	else
	{
		long long d=gcd(abs(a.up),abs(a.down));
		a.up/=d;
		a.down/=d;
	}
	return a;
}
void exchange(Fra a)
{
	if(a.down==0)
	{
		printf("Inf");
		return ;
	} 
	if(a.up==0)
	{
		printf("0");
		return ;
	} 
	if(a.down==1)
	{
		if(a.up<0) printf("(%lld)",a.up);
		else printf("%lld",a.up);
	} 
	else if(abs(a.up)>a.down)
	{
		if(a.up<0) printf("(%lld %lld/%lld)",a.up/a.down,abs(a.up)%a.down,a.down);
		else printf("%lld %lld/%lld",a.up/a.down,abs(a.up)%a.down,a.down);	
	} 
	else if(abs(a.up)<a.down)
	{
		if(a.up<0) printf("(%lld/%lld)",a.up,a.down);
		else printf("%lld/%lld",a.up,a.down);
	} 
	else if(abs(a.up)==a.down)
	{
		if(a.up<0) printf("(%d)",a.up/a.down);
		else printf("%d",a.up/a.down);
	} 
}
Fra add(Fra a,Fra b)
{
	Fra res;
	res.up=a.up*b.down+b.up*a.down;
	res.down=a.down*b.down;
	return reduce(res);
}
Fra minu(Fra a,Fra b)
{
	Fra res;
	res.up=a.up*b.down-b.up*a.down;
	res.down=a.down*b.down;
	return reduce(res);
}
Fra multi(Fra a,Fra b)
{
	Fra res;
	res.up=a.up*b.up;
	res.down=a.down*b.down;
	return reduce(res);
}
Fra divide(Fra a,Fra b)
{
	Fra res;
	res.up=a.up*b.down;
	res.down=a.down*b.up;
	return reduce(res);
}
int main()
{
	Fra a,b;
	scanf("%lld/%lld",&a.up,&a.down);
	scanf("%lld/%lld",&b.up,&b.down);
	a=reduce(a),b=reduce(b);
	Fra x,y,z,w;
	exchange(a); cout<<" + "; exchange(b); cout<<" = ";x=add(a,b);exchange(x); cout<<endl;
	exchange(a); cout<<" - "; exchange(b); cout<<" = ";y=minu(a,b);exchange(y); cout<<endl;
	exchange(a); cout<<" * "; exchange(b); cout<<" = ";z=multi(a,b);exchange(z); cout<<endl;
	exchange(a); cout<<" / "; exchange(b); cout<<" = ";w=divide(a,b);exchange(w); cout<<endl;
	return 0;
}

素数

PTA-1015 Reversible Primes (20 分)

A reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.
Now given any two positive integers N (<10^5) and D (1<D≤10), you are supposed to tell if N is a reversible prime with radix D.

Input Specification:

The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.

Output Specification:

For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.

Sample Input:

73 10
23 2
23 10
-2

Sample Output:

Yes
Yes
No

思路

现将10进制数转化为D进制数,然后再将转化后的数翻转,翻转后转化为10进制数并判断原数与翻转后的数是否都为素数。

#include<bits/stdc++.h>
using namespace std;
long long reverse(int x,int i)
{
	long long ans=0;
	vector<int> a;
	do
	{
		a.push_back(x%i);
		x=x/i;
	}while(x!=0);
	for(int j=0;j<a.size();j++)
	{
		ans+=a[j]*pow(i,a.size()-j-1);
	}
	return ans;
}
bool pan(long long x)
{
	if(x==0||x==1) return false;
	//cout<<x<<endl;
	for(int i=2;i<=sqrt(x*1.0);i++)
	{
		if(x%i==0) return false;
	}
	return true;
}
int main()
{
	int m,n;
	while(scanf("%d",&m)&&m>=0)
	{
		scanf("%d",&n);
		long long ans=reverse(m,n);
		if(pan(m)&&pan(ans))
		{
			printf("Yes\n");
		}
		else
		{
			printf("No\n");
		}
	}
	return 0;
}

PTA-1078 Hashing (25 分)

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.
Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (≤10^4) and N (≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print “-” instead.

Sample Input:

4 4
10 6 4 15

Sample Output:

0 1 4 -

思路

这是一道哈希表模拟题,用到质数的地方就是找到大于所给长度的最小质数作为表长进行模拟。

#include<bits/stdc++.h>
using namespace std;
struct node
{
	int num,loc;
};
node no[10005];
bool pan(int x)
{
	if(x==0||x==1) return false;
	for(int i=2;i<=sqrt(x*1.0);i++)
	{
		if(x%i==0) return 0;
	}
	return 1;
}
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i=n;;i++)
	{
		if(pan(i))
		{
			n=i;
			break;
		}
	}
	vector<int> hash(n,0);
	for(int i=0;i<m;i++)
	{
		bool flag=false;
		cin>>no[i].num;
		int t=no[i].num;
		for(int j=0;j<n;j++)
		{
			int s=(t%n+j*j)%n;
			if(hash[s]==0)
			{
				hash[s]=t;
				no[i].loc=s;
				flag=true;
				break;
			}
		}
		if(!flag) no[i].loc=-1;
	}
	for(int i=0;i<m;i++)
	{
		if(no[i].loc<0)
		{
			printf("-");
		}
		else
		{
			printf("%d",no[i].loc);
		}
		if(i!=m-1) printf(" ");
	}
	return 0;
}

质因子分解

PTA-1096 Consecutive Factors (20 分)

Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

Input Specification:

Each input file contains one test case, which gives the integer N (1<N<2^31).

Output Specification:

For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format factor[1]factor[2]…*factor[k], where the factors are listed in increasing order, and 1 is NOT included.

Sample Input:

630

Sample Output:

3
567

思路

题目要求求出连续的因子序列,我们按照质因子分解的一般方法,因子一般成对出现,在所求数的开方左右分布,如果没有找到,那么说明所求数为质数,因子只有1和它本身。
用left和right记录连续因子序列的起始和结束,用i记录下标,j记录横移量,如果找到连续的序列比之前更长就更新,最后输出即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int a;
	cin>>a;
	int left=0,len=0;
	for(int i=2;i<=sqrt(a*1.0);i++)
	{
		int temp=1,j=i;
		while(1)
		{
			temp*=j;
			if(a%temp!=0) break;
			if(j-i+1>len)
			{
				left=i;
				len=j-i+1;
			}
			j++;
		}
	}
	if(!len)
	{
		printf("%d\n%d",1,a);
	}
	else
	{
		printf("%d\n",len);
		for(int i=left;i<left+len;i++)
		{
			printf("%d",i);
			if(i!=left+len-1) printf("*");
		}
	}
	return 0;
}

PTA-1059 Prime Factors (25 分)

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1k1 ×p 2k2×⋯×p m km .
Input Specification:
Each input file contains one test case which gives a positive integer N in the range of long int.

Output Specification:

Factor N in the format N = p 1 ^ k 1* p 2 ^ k2… * p m^ k m, where pi’s are prime factors of N in increasing order, and the exponent ki is the number of pi-- hence when there is only one pi, k iis 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^2* 11* 17* 101* 1291

思路

用一个结构体储存分解后的质因数及其出现的次数,最后输出即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> su;
bool pan(int x)
{
	if(x==0||x==1) return 0;
	for(int i=2;i<=sqrt(x*1.0);i++)
	{
		if(x%i==0) return false;
	}
	return true;
}
void Init()
{
	for(int i=2;;i++)
	{
		if(pan(i)) su.push_back(i);
		if(su.size()==100) return ;
	}
}
struct fac
{
	int num,n;
};
vector<fac> ans; 
int main()
{
	ll a;
	cin>>a;
	Init();
	if(a==1)
	{
		printf("1=1");
		return 0;
	}
	printf("%lld=",a);
	for(int i=0;i<su.size()&&su[i]<=sqrt(a*1.0);i++)
	{
		fac f;
		bool pan=false;
		if(a%su[i]==0)
		{
			f.num=su[i];
			f.n=0;
		}
		while(a%su[i]==0)
		{
			f.n++;
			a=a/su[i];
			pan=true;
		}
		if(pan) ans.push_back(f);
		if(a==1) break;
	}
	if(a!=1)
	{
		fac f;
		f.num=a;
		f.n=1;
		ans.push_back(f);
	}
	for(int i=0;i<ans.size();i++)
	{
		if(ans[i].n!=1) printf("%d^%d",ans[i].num,ans[i].n);
		else printf("%d",ans[i].num);
		if(i!=ans.size()-1) printf("*");
	}
	return 0;
}

大整数运算

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

思路

利用大整数运算解题的一般方法,利用结构体存储后使大整数乘以2,最后排序对比排序后的序列是否一致,输出结果即可。

#include<bits/stdc++.h>
using namespace std;
struct bign
{
	int x[21],len;
	bign()
	{
		memset(x,0,sizeof(x));
		len=0;
	}
};
bign multi(bign a,int b)
{
	bign c;
	int carry=0;
	for(int i=0;i<a.len;i++)
	{
		int temp=a.x[i]*b+carry;
		c.x[c.len++]=temp%10;
		carry=temp/10;
	}
	while(carry!=0)
	{
		c.x[c.len++]=carry%10;
		carry/=10;
	}
	return c;
}
bool Judge(bign a,bign b)
{
	if(a.len!=b.len) return false;
	bign c=a,d=b;;
	sort(c.x,c.x+c.len);
	sort(d.x,d.x+d.len);
	for(int i=0;i<c.len;i++)
	{
		if(c.x[i]!=d.x[i])
		{
			return false;
		}
	}
	return true;
}
void print(bign a)
{
	reverse(a.x,a.x+a.len);
	for(int i=0;i<a.len;i++)
	{
		printf("%d",a.x[i]);
	}
}
int main()
{
	string str;
	cin>>str;
	reverse(str.begin(),str.end());
	bign a,ans;
	for(int i=0;i<str.size();i++)
	{
		a.x[a.len++]=str[i]-'0';
	}
	ans=multi(a,2);
	if(Judge(a,ans)) printf("Yes\n");
	else printf("No\n");
	print(ans);
	return 0;
}

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

思路

累加一定次数k,直到累加结果为回文或次数用尽,输出回文和累加到回文的次数或输出k和累加k次的数。
大整数运算的模拟题。

#include<bits/stdc++.h>
using namespace std;
struct bign
{
	int x[100];
	int len;
	bign()
	{
		memset(x,0,sizeof(x));
		len=0;
	}
};
bign add(bign a,bign b)
{
	int carry=0;
	bign c;
	for(int i=0;i<a.len||i<b.len;i++)
	{
		int temp=a.x[i]+b.x[i]+carry;
		c.x[c.len++]=temp%10;
		carry=temp/10;
	}
	if(carry!=0) c.x[c.len++]=carry;
	return c; 
}
bool Judge(bign a)
{
	bign c=a; 
	reverse(c.x,c.x+c.len);
	for(int i=0;i<a.len;i++)
	{
		if(a.x[i]!=c.x[i]) return false;
	}
	return true;
}
void print(bign a)
{
	reverse(a.x,a.x+a.len);
	for(int i=0;i<a.len;i++)
	{
		printf("%d",a.x[i]);
	}
	printf("\n");
}
int main()
{
	string str;
	int k;
	bign a,b,ans;
	cin>>str>>k;
	reverse(str.begin(),str.end());
	for(int i=0;i<str.size();i++)
	{
		a.x[a.len++]=str[i]-'0';
	}
	if(Judge(a))
	{
		print(a);
		printf("0");
		return 0;
	}	
	for(int i=0;i<k;i++)
	{
		b=a;
		reverse(b.x,b.x+b.len);
		ans=add(a,b);
		if(Judge(ans))
		{
			print(ans);
			printf("%d",i+1);
			return 0;
		}
		a=ans; 
	}
	print(ans);
	printf("%d",k);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新西兰做的饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值