10018 - Reverse and Add
Moderator: Board moderators
-
- New poster
- Posts: 32
- Joined: Fri Oct 01, 2004 10:53 pm
10018 need help plz
[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]
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]
-
- New poster
- Posts: 32
- Joined: Fri Oct 01, 2004 10:53 pm
10018 compile error?
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)
could anyone help me?
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;
}
-
- New poster
- Posts: 32
- Joined: Fri Oct 01, 2004 10:53 pm
10018 WA
#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.
#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.
Some compilation errors...
First, don't use <iostream.h>, instead use
Second, the main function must return an int; always return from main with
And there's a warning about unused variable "char temp".
Your program failed for this input:
Good luck
First, don't use <iostream.h>, instead use
Code: Select all
#include <iostream>
using namespace std;
Code: Select all
return 0;
Your program failed for this input:
After changing some variables type and size, it produced correct output, which is1
429496295
I changed "unsigned int" to "long long" (and sprintf format becomes "%lld") and string sizes from 10 to 100.3 12574247521
Good luck

10018
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++;
}
}
I tried "long long" or "unsigned int", but all failed.
I don't know why....~>"<~
I got wrong answer
Re: 10018
Maybe overflow in atoi(), the return type of atoi is int, not usigned int or long long.lonelyone wrote:Code: Select all
outInt=atoi(inverse);
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.
Re: 10018
First of all, thanks for your reply...^^Maybe overflow in atoi(), the return type of atoi is int, not usigned int or long long.
But, i forgot to tell something, this method i already tried but it still failed..
I got wrong answer
-
- New poster
- Posts: 1
- Joined: Mon Feb 28, 2005 12:09 am
10018 WA
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;
}
}
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;
}
}
-
- New poster
- Posts: 1
- Joined: Mon Feb 28, 2005 2:09 am
10018 java WA help!
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();}}}
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);