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

Post Reply
Zoe
New poster
Posts: 9
Joined: Tue Jul 20, 2004 5:46 am
Location: Taiwen

Post by Zoe »

I use long long and "%lld".
You use unsigned long and "%lu".
I think two ways are OK.

JuaingFall
New poster
Posts: 13
Joined: Tue Aug 03, 2004 4:24 am
Location: CHINA

Post by JuaingFall »

I think so :P

randomtaiwanese
New poster
Posts: 32
Joined: Fri Oct 01, 2004 10:53 pm

10018 need help plz

Post by randomtaiwanese »

[java]import java.io.IOException;
import java.util.StringTokenizer;

class Main
{
long a;
StringTokenizer tokenizer;
static String ReadLn(int maxLg)
{
byte lin[]=new byte[maxLg];
int lg=0,car=-1;
String line="";
try
{
while(lg<maxLg)
{
car=System.in.read();
if ((car<0)||(car=='\n'))
{
break;
}
lin[lg++]+=car;
}
}
catch(IOException e)
{
return(null);
}
if ((car<0)&&(lg==0))
{
return(null);
}
return (new String (lin, 0, lg));
}
public static void main(String[] args)
{
Main newMain = new Main();
newMain.start();
}
void start()
{
String input;
short count = 0;
boolean innum = false;
byte in_times = 0, in_time = 0;
String[] results = new String[100];
while ((input=Main.ReadLn (255))!=null)
{
tokenizer = new StringTokenizer(input);
if(innum)
{
if(in_time<in_times)
{
count = 0;
a = Long.parseLong(tokenizer.nextToken());
while(true)
{
if(count==0)
a += Reversed();
else if(Reversed() == a)
break;
else if (count>=1000)
break;
else
a += Reversed();
count++;
}
results[in_time] = new String(count+" "+a);
in_time++;
if(in_time==in_times)
{
System.out.println();
for(byte i=0;i<in_times;i++)
{
System.out.println(results);
}
break;
}
}
}
else
{
byte temp = Byte.parseByte(tokenizer.nextToken());
if((temp>0)&&(temp<=100))
{
in_times = temp;
innum = true;
}
}
}
}
long Reversed()
{
String temp;
temp = Long.toString(a);
String rev = new String();
long reversed;
for(int j=temp.length()-1;j>=0;j--)
{
rev += temp.charAt(j);
}
tokenizer = new StringTokenizer(rev);
reversed = Long.parseLong(tokenizer.nextToken());
return reversed;
}
}[/java]

randomtaiwanese
New poster
Posts: 32
Joined: Fri Oct 01, 2004 10:53 pm

Post by randomtaiwanese »

my output
195 -> 4 9339
265 -> 5 45254
750 -> 3 6666
2 -> 1 4
99 -> 6 79497
6 -> 2 33
4000000000 -> 1 4000000004
20 -> 1 22
100 - > 1 101

wirjawan
New poster
Posts: 16
Joined: Fri Oct 01, 2004 10:48 pm
Location: Indonesia

Post by wirjawan »

195
4 9339
265
5 45254
750
3 6666
2
0 2
99
0 99
6
0 6
4000000000
1 4000000004
20
1 22
100
1 101
..

ccpz
New poster
Posts: 3
Joined: Sun Mar 02, 2003 5:25 am

10018 compile error?

Post by ccpz »

this program in my computer compile file
but the judge system alwas says compile error
i am using
gcc version 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)

Code: Select all

03035072_24.c: In function `int main()':
03035072_24.c:35: implicit declaration of function `int atoll(...)'

Code: Select all

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>

using namespace std;

void reverse(char* orig,char* store)
{
    int len=strlen(orig);
    int i;
    for(i=len-1;i>=0;i--)
    {   
        store[len-i-1]=orig[i];
        store[len]='\0';
    }
}

int main()
{
    char a[10000];
    char b[10000];
    long long int ia,ib;
    int p,i;
    int count=0;
    cin>>p;
    for(i=1;i<=p;i++)
    {   
        count=0;
        cin>>ia;
        while(1)
        {   
            sprintf(a,"%lld",ia);
            reverse(a,b);
            ib=atoll(b);
            if((ia >= 10 || count > 0) && ia == ib)
                break;
            ia+=ib;
            count++;
        }
        cout<<count<<' '<<ia<<endl;
    }
    return 0;
}
could anyone help me?

randomtaiwanese
New poster
Posts: 32
Joined: Fri Oct 01, 2004 10:53 pm

Post by randomtaiwanese »

wirjawan wrote:195
4 9339
265
5 45254
750
3 6666
2
0 2
99
0 99
6
0 6
4000000000
1 4000000004
20
1 22
100
1 101
same results... but still WA

timerover
New poster
Posts: 1
Joined: Fri Dec 31, 2004 2:52 pm

10018 WA

Post by timerover »

#include <iostream.h>
#include <string.h>
#include <stdio.h>

void main()
{
char string[10];
char re[10];
char temp;
unsigned long int num,rev=0;
int len;
int cou;
int n;
int f;

while(cin>>n)
{
for(int i=0;i<n;i++){
cou=-1;
rev=0;
f=0;
cin>>num;
while(num!=rev||cou<1)
{
num=num+rev;
sprintf(string,"%ld",num);
len=strlen(string);

for(int j=0;j<len;j++)
{
re[len-1-j]=string[j];
}
re[len]='\0';
sscanf(string,"%ld",&num);
sscanf(re,"%ld",&rev);
cou++;
}
cout<<cou<<" "<<num<<endl;
}
}
}

>"< I got a "WA"

Please help me,thanks.

gits
New poster
Posts: 19
Joined: Sun Oct 26, 2003 10:08 pm
Location: Aveiro, Portugal
Contact:

Post by gits »

Some compilation errors...

First, don't use <iostream.h>, instead use

Code: Select all

#include <iostream>
using namespace std;
Second, the main function must return an int; always return from main with

Code: Select all

return 0;
And there's a warning about unused variable "char temp".

Your program failed for this input:
1
429496295
After changing some variables type and size, it produced correct output, which is
3 12574247521
I changed "unsigned int" to "long long" (and sprintf format becomes "%lld") and string sizes from 10 to 100.

Good luck :)

lonelyone
Learning poster
Posts: 65
Joined: Sat Feb 19, 2005 6:53 pm

10018

Post by lonelyone »

Code: Select all

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

main()
{
    char inverse[15];
    unsigned long int inInt,outInt,temp;
    int counter,check;
    int i=0,j,n;
    
    scanf("%d",&n);
    while(i!=n)
    {
        scanf("%lu",&inInt);
        outInt=0;
        counter=0;
        check=0;
        while(inInt!=outInt || check==1)
        {
            for(j=0;j<15;j++)
            	inverse[j]='\0';
        	temp=inInt+outInt;
        	inInt=temp;
        	for(j=0;temp!=0;j++)
        	{
            	inverse[j]=(char)(temp%10+'0');
            	temp/=10;
        	}     
        	outInt=atoi(inverse);
        	if(counter==0 && inInt==outInt)  
        		check=1;
       		else
       			check=0;
        	counter++;
        }    
        if(inInt==0)
        	printf("1 0\n");   	
        else
        	printf("%d %lu\n",counter-1,inInt);
        i++;
    }    
}   
Does there hava someone could help me to solve this problem?
I tried "long long" or "unsigned int", but all failed.
I don't know why....~>"<~
I got wrong answer

..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Re: 10018

Post by .. »

lonelyone wrote:

Code: Select all

  	outInt=atoi(inverse);
Maybe overflow in atoi(), the return type of atoi is int, not usigned int or long long.
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.

lonelyone
Learning poster
Posts: 65
Joined: Sat Feb 19, 2005 6:53 pm

Re: 10018

Post by lonelyone »

Maybe overflow in atoi(), the return type of atoi is int, not usigned int or long long.
First of all, thanks for your reply...^^
But, i forgot to tell something, this method i already tried but it still failed..
I got wrong answer

texasboi03
New poster
Posts: 1
Joined: Mon Feb 28, 2005 12:09 am

10018 WA

Post by texasboi03 »

import java.io.*;
class Main
{
public static void main(String[] args)
{
String s = "";
int cases = Integer.parseInt(readln());
String [] anArray = new String [cases];
for(int i = 0; i < cases; ++i)
{
anArray = readln();
}
String number = "";
String temp = "";
long total = 0;
System.out.println(" ");
for(int j = 0; j < anArray.length; ++j)
{
number = anArray[j];
temp = reverse(anArray[j]);
total = Long.parseLong(number);
long count = 0;
while(!number.equals(temp))
{
++count;
total = Long.parseLong(number) + Long.parseLong(temp);
number = total + "";
temp = reverse(number);
}
System.out.println(count + " " + total);
}
}
static String readln()
{
try
{
StringBuffer s = new StringBuffer();
int i = 0;
while (i != '\n')
{
i = System.in.read();
if (i < 0) return null;
if (i == '\n') return s.toString();
s.append((char)i);
}
return s.toString();
}
catch (IOException e)
{
return null;
}
}
static String reverse(String num)
{
String ret = "";
for(int i = num.length() - 1; i >= 0; --i)
ret += num.charAt(i);
return ret;
}
}

MystikAngel
New poster
Posts: 1
Joined: Mon Feb 28, 2005 2:09 am

10018 java WA help!

Post by MystikAngel »

Code: Select all

package revadd;


import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.StringTokenizer;


interface MyReader {
    String readLine () throws IOException;}

final class MyBufferedReader implements MyReader {
    private InputStream in;

    public MyBufferedReader (InputStream in) {
        this.in = in;}

    public String readLine () throws IOException {
        final StringBuffer s = new StringBuffer(255);
        int                i = 0;
        while (((i = in.read()) != '\n') && (i != -1))
            if (i != '\r')
                s.append((char) i);
        if (i == -1)
            return null;
        return s.toString();}}

final class Main {
    public static void eval (long j) {
        boolean isPali = false;
        int count = 0;
        long k =0;
        if (isPalindrome(j))
            System.out.println(count + " " + j);
        else
         {
             while(!isPali){
             count++;
             j = add(j);
             if(isPalindrome(j))
                { isPali = true;
                    System.out.println(count + " " + j);
                }
             }
          }

        }
     /*---------------
      * IsPalindrome
      * --------------
      * */
     public static boolean isPalindrome(long j){
     long k = j;
     boolean result = false;
     j = reverse(j);

     if(j == k)
     result = true;

     return result;
     }
     /*------------------
      * Reverse and Add
      * ------------------*
      */

    public static long reverse(long j)
    {
        long orig, factor, result;

        orig = j;
        result = 0;
        factor = 1;

    while ( orig != 0 )
    {
        factor = factor * 10;
        orig = orig / 10;
        }

    factor = factor / 10;
    orig = j;

    while ( j != 0 )
    {
        orig = j % 10;
        result = result + orig * factor;
        j = j / 10;
        factor = factor / 10;
        }
        return result;

    }
    public static long add(long j)
    {
        j = reverse(j) + j;
        return j;
    }



    public static void main (String[] args) {

            /*MyReader in = new MyBufferedReader(System.in);*/
 try {
           MyReader in = new MyBufferedReader(new FileInputStream("main.txt"));

                String   s="";
                StringTokenizer st = new StringTokenizer(s);
                int count = Integer.parseInt(st.nextToken());
                for (int i =0; i<count; i++){
                    long j = Long.parseLong(st.nextToken());
                    eval(j);
                  }
 }catch (IOException e) {
            e.printStackTrace();}}} 
I've ran several test cases and get the right answer for everyone of them. I submit and get WA any help??? I think its the buffered reader....i just ran it again and i got a noSuchElement Excpetion...any help here would be great

reZnor13
New poster
Posts: 1
Joined: Mon Feb 28, 2005 3:26 am

Post by reZnor13 »

first of all, you need to be reading from standard input, not a file. also you dont need stringtokenizer, just do:

Code: Select all

s = in.readLine();
long i = Long.parseLong(s);
eval(i);

Post Reply

Return to “Volume 100 (10000-10099)”