【PAT甲级】1024 Palindromic Number (25 分)

题意:

输入两个正整数N和K(N<=1e10,k<=100),求K次内N和N的反置相加能否得到一个回文数,输出这个数和最小的操作次数。

trick:

1e10的数字相加100次可能达到1e40,所以long long会爆,采用字符数组操作,以及代码注释中遇到的一些小问题。

代码:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
char num[107];
char s[107];//这里用string的时候会遇到以下的小问题,查阅资料后发现直接对string原有长度以后的位置赋值不能增加它的长度,建议采用s+=" xxx",的形式可以增加string的长度。
int k;
int main(){
cin>>s>>k;
int tot=k;
int flag=0;
int siz=strlen(s);
while(k--){
memset(num,0,sizeof(num));
int cnt=0;
for(int i=siz-1;i>=0;--i)
num[++cnt]=s[i];
flag=0;
for(int i=1;i<=cnt/2;++i)
if(num[i]!=num[cnt-i+1]){
flag=1;
break;
}
if(!flag){
flag=2;
break;
}
string y;
int jinwei=0;
for(int i=cnt;i;--i){
int tt=num[i]+s[i-1]-‘0‘-‘0‘+jinwei;
y[i]=tt%10+‘0‘;
jinwei=tt/10;
}
if(jinwei)
y[0]=‘1‘;
if(y[0]!=0)
for(int i=0;i<=cnt;++i)
s[i]=y[i];
else
for(int i=1;i<=cnt;++i)
s[i-1]=y[i];
if(s[cnt]!=0)
siz=cnt+1;//这里用siz来更新s的长度会在使用string s的时候出现段错误
else
siz=cnt;//这里如果不用siz来更新s的长度会在使用string s的时候s.size()不更新,依然是输入s的时候的size(),不是很懂,在size()-1以后的位置给s赋值,不能更新它的size(),用siz更新也会出现段错误
}
for(int i=0;i<siz;++i)
cout<<s[i];
cout<<endl;
if(flag==2)
cout<<tot-k-1;
else
cout<<tot;
return 0;
}

原文地址:https://www.cnblogs.com/ldudxy/p/11442958.html

时间: 2024-08-28 19:21:41

【PAT甲级】1024 Palindromic Number (25 分)的相关文章

PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

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 n

PAT Advanced 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 palin

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 palin

PAT 1024. Palindromic Number (25)

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 nu

1024 Palindromic Number (25)(25 point(s))

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

PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connec

PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, wh

PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if

PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be /, where N?c?? is the number of distinct common numbers shared by the two sets, and N?t?? is the total number of distinct numbers in the two sets. Yo