Posted: Tue Aug 03, 2004 5:18 pm
I use long long and "%lld".
You use unsigned long and "%lu".
I think two ways are OK.
You use unsigned long and "%lu".
I think two ways are OK.
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;
}
same results... but still WAwirjawan 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
Code: Select all
#include <iostream>
using namespace std;
Code: Select all
return 0;
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
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++;
}
}
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);
First of all, thanks for your reply...^^Maybe overflow in atoi(), the return type of atoi is int, not usigned int or long long.
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();}}}
Code: Select all
s = in.readLine();
long i = Long.parseLong(s);
eval(i);