Page 8 of 12

can someone help me pla >.<

Posted: Thu Nov 01, 2007 12:36 pm
by ccu496410022

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

Posted: Fri Nov 02, 2007 12:19 pm
by sapnil
To ccu496410022

Try with this case:

Code: Select all

Input:
2
1
2
Output:
0 1
0 2
Thanks
Keep posting
Sapnil

10018 - Reverse and Add

Posted: Fri Feb 01, 2008 12:05 am
by bobo2000
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;

}

Posted: Tue Mar 04, 2008 9:26 am
by JSB
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

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

Posted: Fri Mar 21, 2008 3:06 pm
by alexified
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.

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

Posted: Fri Oct 03, 2008 4:29 pm
by gareve25
blame microsoft ..., atoll its not part of UNIX

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

Posted: Mon Oct 06, 2008 2:41 pm
by lnr
I can use atol()function to convert string to long integer.
But I can't use atoll() function.

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

Posted: Mon Oct 06, 2008 5:02 pm
by mf
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!)

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

Posted: Mon Oct 06, 2008 5:58 pm
by lnr
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;
}

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

Posted: Mon Oct 06, 2008 6:24 pm
by mf
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.

i have no idea why WAAAAAAAAAA 10018

Posted: Wed Nov 19, 2008 11:20 am
by abid_iut
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:

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

Posted: Wed Nov 19, 2008 11:43 am
by mf
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.

Re:10018 why WA

Posted: Sat Nov 22, 2008 8:14 pm
by abid_iut
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

Re: Re:10018 why WA

Posted: Sat Nov 22, 2008 8:37 pm
by mf
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?

Re: 10018 - Reverse and Add why WA

Posted: Tue Dec 02, 2008 10:01 am
by abid_iut
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: