PAT甲级1024

news/2024/8/22 14:14:39 标签: C++, C++程序设计, 程序设计

1024. Palindromic Number (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
bool checkPalindromic(deque<char>dq)
{
	int i = 0, j = dq.size() - 1;
	while (i <= j)
	{
		if (dq[i] != dq[j])
			return false;
		i++; j--;
	}
	return true;
}
int main()
{
	char N[20];
	int K;
	scanf("%s %d", N, &K);
	int len = strlen(N);
	deque<char> dq,dq1,dq2;
	for (int i = 0; i < len; i++)
	{
		dq.push_back(N[i]);
	}
	dq1 = dq2 = dq;
	reverse(dq2.begin(), dq2.end());
	int i = 1;
	int t, carry = 0;
	if (checkPalindromic(dq))
	{
		for (int i = 0; i < dq.size(); i++)
		{
			printf("%c", dq[dq.size() - 1 - i]);
		}
		printf("\n%d", 0);
		return 0;
	}//若本身就是回文数,则不需进行计算了
	while (i <=K)
	{
		for (int j = 0; j < dq.size(); j++)
		{
			t = dq1[j] - '0' + dq2[j] - '0' + carry;
			carry = 0;
			if (t >= 10)
			{
				dq[j] = '0' + t - 10;
				carry = 1;
			}
			else
				dq[j] = '0' + t;
		}
		if (carry)
		{
			dq.push_back('1');
			carry = 0;
		}
		if (checkPalindromic(dq))
		{
				break;
		}
		else
		{
			dq1 = dq2 = dq;
			reverse(dq2.begin(), dq2.end());
			i++;
		}
	}
	if (i > K)
	{
		for (int i = 0; i < dq.size(); i++)
		{
			printf("%c", dq[dq.size() - 1 - i]);
		}
		printf("\n%d",K);
	}
	else
	{
		for (int i = 0; i < dq.size(); i++)
		{
			printf("%c", dq[dq.size() - 1 - i]);
		}
		printf("\n%d", i);
	}
	return 0;
}


http://www.niftyadmin.cn/n/1145595.html

相关文章

微软承认Vista用户账户控制功能问题

10月13日消息&#xff0c;微软从事Windows7开发工作的工程师承认&#xff0c;Vista的“用户账户控制”功能干扰了用户的正常工作&#xff0c;并承诺将优化Windows7中的“用户账户控制”功能。据国外媒体报道称&#xff0c;微软工程师本法蒂在该公司的Windows7博客上写道&#x…

PAT甲级1039

1039. Course List for Student (25) 时间限制200 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueZhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to outp…

美国FCC支持建立全国性免费无线互联网网络

【搜狐IT消息】北京时间10月14日消息&#xff0c;据国外媒体报道&#xff0c;美国正向着建立一个全国性免费无线互联网网络前进。美国主要电信公司反对开放美国无线波段中未使用的部分&#xff0c;但是美国联邦通信委员会&#xff08;FCC&#xff09;工程师提交的一份报告挫败了…

PAT甲级1047

1047. Student List for Course (25) 时间限制400 ms内存限制64000 kB代码长度限制16000 B判题程序Standard作者CHEN, YueZhejiang University has 40000 students and provides 2500 courses. Now given the registered course list of each student, you are supposed to out…

Mozilla下周将推手机版火狐浏览器Fennec

【赛迪网讯】10月14日消息&#xff0c;据国外媒体报道,Mozilla移动部门副总裁杰伊沙利文(Jay Sullivan)近日表示&#xff0c;将于下周发布手机版火狐(Firefox)浏览器“Fennec”的测试版。据国外媒体报道&#xff0c;沙利文表示&#xff0c;目前该测试版只面向Mozilla社区成员&a…

PAT甲级1063

1063. Set Similarity (25) 时间限制300 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueGiven two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two …

科技部:高新技术企业降低认定门槛

昨天上午&#xff0c;国家科技部高新技术司巡视员耿战修在中国创业家论坛上透露&#xff0c;科技部、财政部和国家税务总局日前联合颁布了新的《高新技术企业认定管理办法》&#xff0c;新的《认定办法》做了很大改进&#xff0c;能让更多高科技企业享受到税收优惠&#xff0c;…

PAT甲级1060

1060. Are They Equal (25) 时间限制50 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueIf a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123*105 with sim…