Problem Type : String
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char ch[5000];
int stringrev(char ch[],int value)
{
int i,j,k,l,carry,m,n,ans,p=1,count=value;
;
char des[5000];
l=strlen(ch);
strcpy(des,ch);
reverse(ch,ch+l);
if(strcmp(des,ch)==0)
{
return count;
}
else
{
count++;
carry=0;
for(i=0; i<l; i++)
{
m=ch[i]-48;
n=des[i]-48;
ans=n+m+carry;
if(ans>9)
{
carry=1;
ans=ans%10;
ch[i]=ans+48;
}
else
{
carry=0;
ch[i]=ans+48;
}
}
if (carry==1){
p=0;
ch[l]=1+48;
l=l+1;
ch[l]='\0';
}
else if (carry==0)
{
ch[l]='\0';
}
if(p=0)
reverse (ch,ch+(l+1));
else if(p=1)
reverse(ch,ch+l);
}
return stringrev(ch,count);
}
int main()
{
int i,j,k,count,n,value;
while(scanf("%d",&n)==1)
{
for(i=0; i<n; i++)
{
value=0;
scanf("%s",ch);
count = stringrev(ch,value);
printf("%d %s\n",count,ch);
}
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char ch[5000];
int stringrev(char ch[],int value)
{
int i,j,k,l,carry,m,n,ans,p=1,count=value;
;
char des[5000];
l=strlen(ch);
strcpy(des,ch);
reverse(ch,ch+l);
if(strcmp(des,ch)==0)
{
return count;
}
else
{
count++;
carry=0;
for(i=0; i<l; i++)
{
m=ch[i]-48;
n=des[i]-48;
ans=n+m+carry;
if(ans>9)
{
carry=1;
ans=ans%10;
ch[i]=ans+48;
}
else
{
carry=0;
ch[i]=ans+48;
}
}
if (carry==1){
p=0;
ch[l]=1+48;
l=l+1;
ch[l]='\0';
}
else if (carry==0)
{
ch[l]='\0';
}
if(p=0)
reverse (ch,ch+(l+1));
else if(p=1)
reverse(ch,ch+l);
}
return stringrev(ch,count);
}
int main()
{
int i,j,k,count,n,value;
while(scanf("%d",&n)==1)
{
for(i=0; i<n; i++)
{
value=0;
scanf("%s",ch);
count = stringrev(ch,value);
printf("%d %s\n",count,ch);
}
}
return 0;
}
No comments:
Post a Comment