10035 - Primary Arithmetic

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

shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim »

consider

1 999

there should be 3 carry operations, your program gives 4 as the answer.
ravingavin
New poster
Posts: 21
Joined: Sun Sep 14, 2003 11:44 pm
Location: USA
Contact:

Post by ravingavin »

Thanks for your response! I've just run the code for:

Code: Select all

1 999
I recieve:

Code: Select all

3 carry operations.
not 4. I haven't changed my code at all.

GCS
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

I get "4 carry operations" as well for "1 999".

Hint: You're trying to access negative array locations in your code somewhere, which would explain why it might work for you, but not us, since negative arrays cause unpredictable problems.
ravingavin
New poster
Posts: 21
Joined: Sun Sep 14, 2003 11:44 pm
Location: USA
Contact:

SOLVED

Post by ravingavin »

I fixed my error and got the problem solved!

Thanks everyone who replied!


GCS
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

10035WA(i've tried all possible numbers i can imagin!!)

Post by Morning »

i'll be really appreciate if someone can some one give me some test numbers to let me know what's wrong with my code.
i've tried
1 999
999 1
0 123
123 123
etc...
[java]
import java.io.*;
import java.util.*;

class Main
{

static String ReadLn (int maxLg) // utility function to read from stdin
{
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); // eof
return (new String (lin, 0, lg));
}

public static void main (String args[]) // entry point from OS
{
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}

void Begin()
{
int loop;
long a,b;
int length1=0;
int length2=0;
int times=0;
boolean add=false;
byte [] num1;
byte [] num2;
StringTokenizer idata;
String aLine="";
byte [] array=new byte[255];
while ((aLine = Main.ReadLn (255)) != null)
{
times=0;
add=false;
length1=length2=0;
idata = new StringTokenizer (aLine);
a = Long.parseLong (idata.nextToken());
b = Long.parseLong (idata.nextToken());
if((a==b)&&(a==0)) break;
for(loop=0;loop<aLine.length();loop++)
{
if(aLine.charAt(loop)==' ')
{
length1=loop;

break;
}
}
length2=aLine.length()-length1-2;
for(loop=0;loop<aLine.length();loop++)
{
array[loop]=0;
array[loop]+=aLine.charAt(loop);
}
String number1= new String (array,0,length1);
String number2= new String (array,length1+1,length2);


if(number1.length()>=number2.length())
{
num1=new byte[number1.length()];
num2=new byte[number2.length()];

for(loop=0;loop<number1.length();loop++)
{
num1[loop]=(byte)(number1.charAt(loop)-48);
}

for(loop=0;loop<number2.length();loop++)
{
num2[loop]=(byte)(number2.charAt(loop)-48);
}

for(loop=number2.length()-1;loop>=0;loop--)
{
if (add==false)
{
if(num1[loop+number1.length()-number2.length()]+num2[loop]>9)
{
add=true;
times+=1;
}
}
else
{
if(num1[loop+number1.length()-number2.length()]+num2[loop]>8)
{
add=true;
times+=1;
}
else add=false;
}
}

if(number1.length()!=number2.length())
{
if(add)
{
for(;loop+number1.length()-number2.length()>=0;loop--)
{
if (num1[loop+number1.length()-number2.length()]+1>9) times+=1;
else break;
}
}
}
}

else //when the first number is less than the second
{
num1=new byte[number2.length()];
num2=new byte[number1.length()];

for(loop=0;loop<number2.length();loop++)
{
num1[loop]=(byte)(number2.charAt(loop)-48);
}

for(loop=0;loop<number1.length();loop++)
{
num2[loop]=(byte)(number1.charAt(loop)-48);
}

for(loop=number1.length()-1;loop>=0;loop--)
{
if (add==false)
{
if(num1[loop+number2.length()-number1.length()]+num2[loop]>9)
{
add=true;
times+=1;
}
}
else
{
if(num1[loop+number2.length()-number1.length()]+num2[loop]>8)
{
add=true;
times+=1;
}
else add=false;
}
}
if(number1.length()!=number2.length())
{
if(add)
{
for(;loop+number2.length()-number1.length()>=0;loop--)
{
if (num1[loop+number2.length()-number1.length()]+1>9) times+=1;
else break;
}
}
}
}

if(times==0) System.out.println("No carry operation.");
else if(times==1) System.out.println("1 carry operation.");
else System.out.println(times+" carry operations.");

}

}
}
[/java]
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

having trouble (10035 problem) using java,pliz help

Post by Morning »

i'll be really appreciate if someone can some one give me some test numbers to let me know what's wrong with my code.
i've tried
1 999
999 1
0 123
123 123
etc...
[java]
import java.io.*;
import java.util.*;

class Main
{

static String ReadLn (int maxLg) // utility function to read from stdin
{
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); // eof
return (new String (lin, 0, lg));
}

public static void main (String args[]) // entry point from OS
{
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}

void Begin()
{
int loop;
long a,b;
int length1=0;
int length2=0;
int times=0;
boolean add=false;
byte [] num1;
byte [] num2;
StringTokenizer idata;
String aLine="";
byte [] array=new byte[255];
while ((aLine = Main.ReadLn (255)) != null)
{
times=0;
add=false;
length1=length2=0;
idata = new StringTokenizer (aLine);
a = Long.parseLong (idata.nextToken());
b = Long.parseLong (idata.nextToken());
if((a==b)&&(a==0)) break;
for(loop=0;loop<aLine.length();loop++)
{
if(aLine.charAt(loop)==' ')
{
length1=loop;

break;
}
}
length2=aLine.length()-length1-2;
for(loop=0;loop<aLine.length();loop++)
{
array[loop]=0;
array[loop]+=aLine.charAt(loop);
}
String number1= new String (array,0,length1);
String number2= new String (array,length1+1,length2);


if(number1.length()>=number2.length())
{
num1=new byte[number1.length()];
num2=new byte[number2.length()];

for(loop=0;loop<number1.length();loop++)
{
num1[loop]=(byte)(number1.charAt(loop)-48);
}

for(loop=0;loop<number2.length();loop++)
{
num2[loop]=(byte)(number2.charAt(loop)-48);
}

for(loop=number2.length()-1;loop>=0;loop--)
{
if (add==false)
{
if(num1[loop+number1.length()-number2.length()]+num2[loop]>9)
{
add=true;
times+=1;
}
}
else
{
if(num1[loop+number1.length()-number2.length()]+num2[loop]>8)
{
add=true;
times+=1;
}
else add=false;
}
}

if(number1.length()!=number2.length())
{
if(add)
{
for(;loop+number1.length()-number2.length()>=0;loop--)
{
if (num1[loop+number1.length()-number2.length()]+1>9) times+=1;
else break;
}
}
}
}

else //when the first number is less than the second
{
num1=new byte[number2.length()];
num2=new byte[number1.length()];

for(loop=0;loop<number2.length();loop++)
{
num1[loop]=(byte)(number2.charAt(loop)-48);
}

for(loop=0;loop<number1.length();loop++)
{
num2[loop]=(byte)(number1.charAt(loop)-48);
}

for(loop=number1.length()-1;loop>=0;loop--)
{
if (add==false)
{
if(num1[loop+number2.length()-number1.length()]+num2[loop]>9)
{
add=true;
times+=1;
}
}
else
{
if(num1[loop+number2.length()-number1.length()]+num2[loop]>8)
{
add=true;
times+=1;
}
else add=false;
}
}
if(number1.length()!=number2.length())
{
if(add)
{
for(;loop+number2.length()-number1.length()>=0;loop--)
{
if (num1[loop+number2.length()-number1.length()]+1>9) times+=1;
else break;
}
}
}
}

if(times==0) System.out.println("No carry operation.");
else if(times==1) System.out.println("1 carry operation.");
else System.out.println(times+" carry operations.");

}

}
}
[/java]
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning »

No one can help me? :cry:
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Sajid
Learning poster
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.
Contact:

Post by Sajid »

Input:

Code: Select all

9999 1
9992 8
9898 2
11111 0
1000 999
9 1
0 0
Output:

Code: Select all

4 carry operations.
4 carry operations.
2 carry operations.
0 carry operation.
0 carry operation.
1 carry operation.
I think there is no special input. Best of Luck
Sajid Online: www.sajidonline.com
Bug!
New poster
Posts: 24
Joined: Thu Oct 30, 2003 10:19 am

Post by Bug! »

input:
4556 9346
99999998888888999999 2222222222222
456 654
012332 123210
45654 456
5487 9321
0001 0009
0 0

output:
3 carry operations.
20 carry operations.
3 carry operations.
No carry operation.
3 carry operations.
2 carry operations.
1 carry operation.

hope this sample can help u... :wink:
regard..
Andre
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning »

I've tried all of ur numbers expect the"9999998888899999999 1"
cauz the problem says
"Each line of input contains two unsigned integers less than 10 digits. "
and i can't find any other problem. :cry:
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning »

may be the"999999999999999"is right the problem,i should change my variable to Double.let me test it.
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

10035 in JAVA.

Post by anupam »

have any one solved the problem in java.
if yes, snd it at abrbuet@yahoo.com plz.
"Everything should be made simple, but not always simpler"
Niaz Morshed
New poster
Posts: 12
Joined: Sun Nov 09, 2003 1:27 am
Location: East West University, Dhaka.
Contact:

Post by Niaz Morshed »

Shamim ! doing good job, carry on . . . :D :D :D
The Last Man Standing :-)
slavej
New poster
Posts: 2
Joined: Tue Nov 11, 2003 11:56 pm
Contact:

10035 Primary arithmetic

Post by slavej »

What is qrong with this code:::


[c]#include <stdio.h>

int main(void)
{
int i, n1, n2, temp, length;
int a1[20], a2[20];

while (1)
{
scanf("%d%d",&n1,&n2);

if ( (n1 == 0) && (n2 == 0) )
break;

for (i = 0; i <= 15; ++i)
{
a1 = 0;
a2 = 0;
}

i = 0;
temp = n1;

/* Put the digits of n1 into an array */
while (temp)
{
++i;
a1 = temp % 10;
temp /= 10;
}
/* Set the length to be equal to the length of n1 */
length = i;

i = 0;
temp = n2;

/* Put the digits of n2 into an array */
while (temp)
{
++i;
a2 = temp % 10;
temp /= 10;
}

/* Check the length */
if (i > length)
length = i;

temp = 0;

/* Calculate the nuber of operations */
for (i = 1; i <= length; ++i)
if ((a1 + a2) > 9)
{
++a1[i+1];
++temp;
}

if (temp)
{
if (temp == 1)
printf("%d carry operation.\n", temp);
else
printf("%d carry operations.\n", temp);
}
else
printf("No carry operation.\n");
}

return(0);
}
[/c]
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning »

no,that's not the reason.can someone help me? :cry:
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Post Reply

Return to “Volume 100 (10000-10099)”