10018 - Reverse and Add

All about problems in Volume 100. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

ccu496410022
New poster
Posts: 1
Joined: Thu Nov 01, 2007 12:29 pm

can someone help me pla >.<

Post by ccu496410022 » Thu Nov 01, 2007 12:36 pm

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int counter=0;
void readd(char a[100]){
    char b[100]={0};
    int len,i,counter1=0;
    len=strlen(a)-1;
    counter++;
    for(i=0;i<=len;i++){
       b[i]=a[i];
        }
        for(i=0;i<=100;i++) a[i]=48;
    for(i=0;i<=len;i++)
    {
        if(b[i]+b[len-i]+a[i]-144>=10){
            a[i+1]=a[i+1]+1;
            a[i]=a[i]+b[i]+b[len-i]-106;
            if(i==len) counter1++;
            }
        else{ a[i]=a[i]+b[i]+b[len-i]-96;}
        }
       for(i=len+counter1+1;i<100;i++) a[i]=0;
     }
void judge(char a[100]){
    int i,j,len,counter2=0,m;
    len=strlen(a)-1;
    for(i=0;i<=len;i++) {
        if(a[i]==a[len-i]){
            counter2++;
            }

        }
    if(counter2==len+1) {printf("%d ",counter);for(j=len;j>=0;j--) printf("%c",a[j]);}
    if(counter2!=len+1){
        readd(a);
        judge(a);
        }


    }
int main()
{
    int i,n;
    char a[100];
    scanf("%d",&n);
    for(i=0;i<n;i++){
    counter=0;
    scanf("%s",a);
    readd(a);
    judge(a);
    if(i<n-1) printf("\n");
    }
    return 0;
}


I've tried all numers I could find.And it's all right.But I still got WA.Can someone tell me why plz??

sapnil
Experienced poster
Posts: 106
Joined: Thu Apr 26, 2007 2:40 pm
Location: CSE-SUST
Contact:

Post by sapnil » Fri Nov 02, 2007 12:19 pm

To ccu496410022

Try with this case:

Code: Select all

Input:
2
1
2
Output:
0 1
0 2
Thanks
Keep posting
Sapnil
"Dream Is The Key To Success"

@@@ Jony @@@

bobo2000
New poster
Posts: 1
Joined: Fri Feb 01, 2008 12:02 am

10018 - Reverse and Add

Post by bobo2000 » Fri Feb 01, 2008 12:05 am

The judge keeps rejecting this. Please help
I tried it with all test cases that i know
:o


//reverse a string
string reverse(string original) {

string reversed = original;
char *orig = (char *) original.c_str();
char *rev = (char *) reversed.c_str();

while (*rev != '\0') *rev++;
*rev--;

while (*orig != '\0') {
*rev = *orig;
*orig++;
*rev--;
}

return reversed;

}

//main
int main() {

string str;

//get the number of case
int cases = 0;
getline(cin, str);
cases = atoi(str.c_str());

//process each case
for (int i= 1; i <= cases; i++) {

//get the first case
getline(cin, str);

bool palindrome = false;
unsigned long iterations = 0;

//check if the input number is palidrome
string reversed = reverse(str);
if (strcmp(str.c_str(), reversed.c_str()) == 0) {
cout << "0" << " " << str;
if (i != cases) cout << endl;
continue;
}


while (!palindrome){

iterations++;

string reversed = reverse(str); // reverse number
unsigned long originalInt = strtoul(str.c_str(), NULL, 10); // get the original int
unsigned long reversedInt = strtoul(reversed.c_str(), NULL, 10); // get the reversed int
unsigned long totalInt = originalInt + reversedInt; // add the two

stringstream stream;
stream << totalInt;
stream >> str;

reversed = reverse(str); // reverse the total

//check if reversed and total are the same
if (strcmp(str.c_str(), reversed.c_str()) == 0) {
palindrome = true;
cout << iterations << " " << str;
if (i != cases) cout << endl;
break;
}

}

}

return 0;

}

JSB
New poster
Posts: 1
Joined: Mon Mar 03, 2008 3:20 pm

Post by JSB » Tue Mar 04, 2008 9:26 am

Hi everyone. Someone please help...

I have tried all possible test cases and read every thread on this problem.
import java.io.*;

public class Main
{
static int[] Arr;
static BufferedReader in;;

public static void main(String[] args)
{
InputStreamReader converter = new InputStreamReader(System.in);
in = new BufferedReader(converter);
int x = inRead();
if (x != 0)
{
Arr = new int[x];
}

for (int i = 0; i < Arr.length; i++)
{
x = inRead();
if (x == 0)
break;
else
{
Arr = x;
}
}


if (x != 0)
{
LoopDaLoop();
}
}

public static void LoopDaLoop()
{
int c = Arr.length;
for (int i = 0; i < c; i++)
{
int count = 0;
long n = Arr;

while(!isPal(n, rev(n)))
{

n = add(n, rev(n));
count++;
}
System.out.println(count + " " + n);
}
}

public static int inRead()
{
String x = "";
try
{
x = in.readLine();
}
catch (Exception e)
{

}

return Integer.parseInt(x);

}

public static long add(long i, long y)
{
return i + y;
}

public static long rev(long i)
{
String str1 = String.valueOf(i);
String sBr;
StringBuffer sB = new StringBuffer(str1);
sBr = sB.reverse().toString();
return Integer.parseInt(sBr);
}



public static boolean isPal(long i, long y)
{
String str1 = String.valueOf(i);
String str2 = String.valueOf(y);
if (str1.equalsIgnoreCase(str2))
{
return true;
}
else
{
return false;
}
}

}


I constantly get a Runtime Error.
Any advice?

( i know the code isn't perfect, but it works as needed!)

*EDIT : new code, same error

alexified
New poster
Posts: 1
Joined: Thu Mar 13, 2008 5:12 pm
Location: Korea
Contact:

I have a question why I can't use atoll()

Post by alexified » Fri Mar 21, 2008 3:06 pm

Hi~ How are you? It's my first time to post here.

I have a question.

while I'm solving problem of 10018, I tried to use the function atoll() to change string to long long type but Visual C++ 2005 can't recognize it even though I included header file "stdlib.h".

Surely, I solved that problem with my own function but I'm still curious why it didn't work.
Last edited by alexified on Fri Jul 08, 2011 3:30 pm, edited 1 time in total.

gareve25
New poster
Posts: 5
Joined: Mon Feb 11, 2008 8:38 pm

Re: I have a question why I can't use atoll()

Post by gareve25 » Fri Oct 03, 2008 4:29 pm

blame microsoft ..., atoll its not part of UNIX
Just beginning to make my brain more strong

lnr
Experienced poster
Posts: 141
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: I have a question why I can't use atoll()

Post by lnr » Mon Oct 06, 2008 2:41 pm

I can use atol()function to convert string to long integer.
But I can't use atoll() function.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: I have a question why I can't use atoll()

Post by mf » Mon Oct 06, 2008 5:02 pm

atoll() function is part of C99 standard. Modern compilers (gcc 4.x, for example) should support it.

I've heard Visual C++ 2008 also now supports C99 (finally!)

lnr
Experienced poster
Posts: 141
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: I have a question why I can't use atoll()

Post by lnr » Mon Oct 06, 2008 5:58 pm

Microsoft Visual C++ 2008 (framework may be 2.5) does not support long long atoll(const char *s) function.

Here is the code:

#include <iostream>
#include<cstdlib>
#include <string>
using namespace std;

int main()
{
string s="346543";
long long a=atoll(s);
cout<<a<<endl;
return 0;
}

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: I have a question why I can't use atoll()

Post by mf » Mon Oct 06, 2008 6:24 pm

Oh, so I heard wrong :(, and Microsoft's compilers are as non-standards-conforming now as they've always been...

Well, ditch them, you can always use the free gcc compiler instead. There are two ports for Windows - MinGW and Cygwin.

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

i have no idea why WAAAAAAAAAA 10018

Post by abid_iut » Wed Nov 19, 2008 11:20 am

please help
every I/O is getting right but still WA
here is my code:

Code: Select all

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 1000


void reverse(char *from, char *to){
	int len=strlen(from);
	int l;
	for(l=0;l<len;l++)
		to[l]=from[len-l-1];
	to[len]='\0';
}

void call_sum(char *first,char *sec,char *result){
	char F[MAX],S[MAX],Res[MAX];
	int f,s,sum,extra,now;
	f=strlen(first);
	s=strlen(sec);
	reverse(first,F);
	reverse(sec,S);
	for(now=0,extra=0;(now<f && now<s);now++){
		sum=(F[now]-'0')+(S[now]-'0')+extra;
		Res[now]=sum%10+'0';
		extra=sum/10;
	}
	for(;now<f;now++){
		sum=F[now]+extra-'0';
		Res[now]=sum%10+'0';
		extra=sum/10;
	}
	for(;now<s;now++){
		sum=F[now]+extra-'0';
		Res[now]=sum%10+'0';
		extra=sum/10;
	}
	if(extra!=0)Res[now++]=extra+'0';
	Res[now]='\0';
	if(strlen(Res)==0)strcpy(Res,"0");
	reverse(Res,result);
}

int main()
{
	char fir[MAX],sec[MAX],res[MAX];
	int n,i,k,flag,count,it;
	scanf("%d",&n);
	for(it=0;it<n;it++){
		count=0;flag=0;
		scanf("%s",fir);
		int len=strlen(fir);
		k=len-1;
		if(len==1){printf("0 %s\n",fir);continue;}
		for(i=0;i<len/2;i++){
				if(fir[i]==fir[k]){
					flag=1;
					k--;
				}
				else {flag=0;break;}
		}
				if(flag==1){printf("0 %s\n",fir);continue;}
		reverse(fir,sec);
		call_sum(fir,sec,res);
		
		while(1){
			int len=strlen(res);
			flag=0;k=len-1;
			for(i=0;i<len/2;i++)
				if(res[i]==res[k]){
					flag=1;
					k--;
				}
				else {
					for(i=0;i<len+1;i++){fir[i]=res[i];}
					count++;
					reverse(fir,sec);
					call_sum(fir,sec,res);
				}
				if(flag==1){printf("%d %s\n",count+1,res);break;}			
		}
	}
	return 0;
}

pls pls pls help someone :cry:
Last edited by abid_iut on Thu Nov 20, 2008 11:08 am, edited 1 time in total.
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: I have a question why I can't use atoll()

Post by mf » Wed Nov 19, 2008 11:43 am

I'm afraid you posted that in the wrong place.

Search the board for a thread about the problem you're trying to solve (btw, you didn't even mention its number here, so how can you expect anyone to help you?), and post there, after reading existing posts, if they don't help.

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

Re:10018 why WA

Post by abid_iut » Sat Nov 22, 2008 8:14 pm

pls check someome why WA

Code: Select all

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 1000


void reverse(char *from, char *to){
	int len=strlen(from);
	int l;
	for(l=0;l<len;l++)
		to[l]=from[len-l-1];
	to[len]='\0';
}

void call_sum(char *first,char *sec,char *result){
	char F[MAX],S[MAX],Res[MAX];
	int f,s,sum,extra,now;
	f=strlen(first);
	s=strlen(sec);
	reverse(first,F);
	reverse(sec,S);
	for(now=0,extra=0;(now<f && now<s);now++){
		sum=(F[now]-'0')+(S[now]-'0')+extra;
		Res[now]=sum%10+'0';
		extra=sum/10;
	}
	for(;now<f;now++){
		sum=F[now]+extra-'0';
		Res[now]=sum%10+'0';
		extra=sum/10;
	}
	for(;now<s;now++){
		sum=F[now]+extra-'0';
		Res[now]=sum%10+'0';
		extra=sum/10;
	}
	if(extra!=0)Res[now++]=extra+'0';
	Res[now]='\0';
	if(strlen(Res)==0)strcpy(Res,"0");
	reverse(Res,result);
}

int main()
{
	char fir[MAX],sec[MAX],res[MAX];
	int n,i,k,flag,count,it;
	scanf("%d",&n);
	for(it=0;it<n;it++){
		count=0;flag=0;
		scanf("%s",fir);
		int len=strlen(fir);
		k=len-1;
		if(len==1){printf("0 %s\n",fir);continue;}
		for(i=0;i<len/2;i++){
				if(fir[i]==fir[k]){
					flag=1;
					k--;
				}
				else {flag=0;break;}
		}
				if(flag==1){printf("0 %s\n",fir);continue;}
		reverse(fir,sec);
		call_sum(fir,sec,res);
		
		while(1){
			int len=strlen(res);
			flag=0;k=len-1;
			for(i=0;i<len/2;i++)
				if(res[i]==res[k]){
					flag=1;
					k--;
				}
				else {
					for(i=0;i<len+1;i++){fir[i]=res[i];}
					count++;
					reverse(fir,sec);
					call_sum(fir,sec,res);
				}
				if(flag==1){printf("%d %s\n",count+1,res);break;}			
		}
	}
	return 0;
}

help help
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: Re:10018 why WA

Post by mf » Sat Nov 22, 2008 8:37 pm

Good, now we know what problem you're solving. But you're still posting in the wrong thread (I don't any atoll in your code). Please, do search the board for a thread about problem 10018 and post your code in that thread. (This thread, for instance, looks like the most popular.) Is that so hard to do?

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

Re: 10018 - Reverse and Add why WA

Post by abid_iut » Tue Dec 02, 2008 10:01 am

please help why I am getting WA
it is giving everything correct
here is the code:

Code: Select all

Removed after AC
pls help :cry:
Last edited by abid_iut on Thu Dec 04, 2008 9:21 am, edited 1 time in total.
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

Post Reply

Return to “Volume 100 (10000-10099)”