10035 - Primary Arithmetic
Moderator: Board moderators
-
- New poster
- Posts: 21
- Joined: Sun Sep 14, 2003 11:44 pm
- Location: USA
- Contact:
Thanks for your response! I've just run the code for:
I recieve:
not 4. I haven't changed my code at all.
GCS
Code: Select all
1 999
Code: Select all
3 carry operations.
GCS
-
- New poster
- Posts: 21
- Joined: Sun Sep 14, 2003 11:44 pm
- Location: USA
- Contact:
SOLVED
I fixed my error and got the problem solved!
Thanks everyone who replied!
GCS
Thanks everyone who replied!
GCS
10035WA(i've tried all possible numbers i can imagin!!)
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]
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
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
having trouble (10035 problem) using java,pliz help
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]
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
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
-
- Learning poster
- Posts: 94
- Joined: Sat Oct 05, 2002 5:34 pm
- Location: CS - AIUB, Dhaka, Bangladesh.
- Contact:
Input:
Output:
I think there is no special input. Best of Luck
Code: Select all
9999 1
9992 8
9898 2
11111 0
1000 999
9 1
0 0
Code: Select all
4 carry operations.
4 carry operations.
2 carry operations.
0 carry operation.
0 carry operation.
1 carry operation.
Sajid Online: www.sajidonline.com
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...
regard..
Andre
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:](./images/smilies/icon_wink.gif)
regard..
Andre
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:](./images/smilies/icon_cry.gif)
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:](./images/smilies/icon_cry.gif)
"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
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
10035 in JAVA.
have any one solved the problem in java.
if yes, snd it at abrbuet@yahoo.com plz.
if yes, snd it at abrbuet@yahoo.com plz.
"Everything should be made simple, but not always simpler"
-
- New poster
- Posts: 12
- Joined: Sun Nov 09, 2003 1:27 am
- Location: East West University, Dhaka.
- Contact:
10035 Primary arithmetic
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]
[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]