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

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

Post by Morning » Mon Nov 24, 2003 1:33 pm

my god.i've tried it for about 1 month and get WA.do u guess the problem itself has problems?

[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;
double 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 = Double.parseDouble(idata.nextToken());
b = Double.parseDouble(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

gvcormac
Problemsetter & Reviewer
Posts: 194
Joined: Fri Mar 15, 2002 2:00 am
Contact:

Post by gvcormac » Mon Nov 24, 2003 3:52 pm

Morning wrote:no,that's not the reason.can someone help me? :cry:
Morning,

I suggest you try to prove to yourself that your code works for all cases. I also suggest that you discard your current solution and try to come up with something simpler. For example, rather than replicating the code depending on which is larger, why not swap the two numbers so that one is always bigger? Better still, add zeroes to the shorter so they are always the same length. Always try to reduce the number of cases and the amount of replicated code.

I also suggest that you try to be systematic in composing test examples. It isn't hard to find one for which your program fails. All I did was to compose the smallest example that has a carry, and your program gave the wrong answer.

3 7
0 0

It also got "88 88", "90 90" and many other two-digit examples wrong.

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

Post by Morning » Tue Nov 25, 2003 8:28 am

firstly,thanks for ur advices indeed.
but there're something strange.
3 7->1 carry operation.
88 88->2 carry operations.
90 90->1 carry operation.
0 0->the program end
that is the answer from my code,is there anything wrong?
pliz tell me which compiler u r using.
my compiler is javac 1.4.1 dowloaded from sun.com
wait for ur reply
"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

problem
New poster
Posts: 27
Joined: Mon Nov 10, 2003 12:40 am

10035 wa help me plz

Post by problem » Tue Nov 25, 2003 10:53 am

i think its very simle problem.but again and again i get wa.every input and output is ok.plz help me.


[cpp]
#include<stdio.h>
#include<math.h>

void main()
{
long int i,j,sum=0,p,q,carry=0;
while(scanf("%ld%ld",&i,&j)!=EOF)
{
if(i==0&&j==0)
break;
while(i>0&&j>0)
{
p=fmod(i,10); /*take last element by fmod*/
i=i/10;
q=fmod((j+carry),10); /*take last element by fmod of j*/
j=j/10;
if((p+q)>=10) /*check for carry*/
{
carry=(p+q)/10;
sum+=1;
}
}
if(i>0) /*if i is greater than j*/
{
while(i>0)
{
p=fmod(i,10);
i=i/10;
if((p+carry)>=10)
{
carry=(p+carry)/10;
sum+=1;
}
}
}

if(j>0) /*if j is greater than i*/
{
while(j>0)
{
q=fmod(j,10);
j/=10;
if((q+carry)>=10)
{
carry=(q+carry)/10;
sum+=1;
}
}
}



if(sum==0)
printf("No carry operation.\n");
if(sum==1)
printf("%.ld carry operation.\n",sum);
if(sum>1)
printf("%.ld carry operations.\n",sum);
sum=0;
carry=0;
}
}[/cpp]

anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post by anupam » Tue Nov 25, 2003 12:57 pm

Morning wrote:Programming, I love the fun, not game.
"Everything should be made simple, but not always simpler"

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

Post by Morning » Fri Nov 28, 2003 6:11 pm

hehe,i just quote the sentence"NBA---I love this game!"
"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

Eva
New poster
Posts: 14
Joined: Thu Apr 10, 2003 11:31 am
Location: Bombay
Contact:

10035 Error?

Post by Eva » Mon Dec 01, 2003 12:12 am

This is also not Ok! but why? can anyone help :(

[cpp]#include<iostream.h>

typedef unsigned long int ulong;

int main()
{
ulong a,b;

while(cin >> a >> b)
{
if(a==0 && b==0)
return 0;

int count = 0, cry = 0;

while(a || b)
{
int x = a%10, y = b%10;

a/=10;
b/=10;

if((x+y+cry)>9)
{
cry = 1;
count++;
}
else
cry = 0;
}

if(count>1)
cout << count << " carry operations." << endl;
else if(count == 1)
cout << "1 carry operation." << endl;
else
cout << "No carry operation." << endl;
}

return 0;
}[/cpp]
My own quote:

We are here as Adam and Eve were here!

gvcormac
Problemsetter & Reviewer
Posts: 194
Joined: Fri Mar 15, 2002 2:00 am
Contact:

Post by gvcormac » Mon Dec 01, 2003 2:47 am

I submitted your program and it gets AC.

problem
New poster
Posts: 27
Joined: Mon Nov 10, 2003 12:40 am

help need

Post by problem » Wed Dec 10, 2003 11:16 am

[c]
My program gives following output for the below input.but i dont understand
why wrong ans.plz check my solution.I think a little problem is arise.
help me.

input::
--------
123 456
555 555
123 594
0 1
1 0
999999999 1
1 999999999
111111111 888888888
111111112 888888888
900000000 100000000
1 1
1 999
0 0


output::
----------
No carry operation.
3 carry operations.
1 carry operation.
No carry operation.
No carry operation.
9 carry operations.
9 carry operations.
No carry operation.
9 carry operations.
1 carry operation.
No carry operation.
3 carry operations.
[\c]

osan
New poster
Posts: 47
Joined: Tue Jul 29, 2003 12:03 pm
Location: Bangladesh,Dhaka.
Contact:

10035

Post by osan » Wed Dec 17, 2003 5:16 pm

u use long int which range is 2^31-1= 2147483647

it is a 10 digits number. but 2147483647 <5000000000.

so u can use here unsigned long. u hav to %lu for unsigned long.

& one thing fmod is a function which use to mod a floating number.

OSAN

problem
New poster
Posts: 27
Joined: Mon Nov 10, 2003 12:40 am

Post by problem » Sun Dec 28, 2003 9:24 am

hi osan still now i got wrong ans.i change my source code what u tell me.if i give input 999999 & 999999 give output 1.but if i give input 555555&555555 || 666666&666666 || 777777&777777 || 888888&888888 give correct ans.

osan
New poster
Posts: 47
Joined: Tue Jul 29, 2003 12:03 pm
Location: Bangladesh,Dhaka.
Contact:

sorry problem i did a mistake

Post by osan » Sun Jan 04, 2004 8:57 am

sorry problem i did a mistake

unsigned long is 2^32=4294967296

it can't hold 10 digits & for this problem u have no need think abt this.coz Each line of input contains two unsigned integers less than 10 digits.

check ur algorithm. i think u hav problem there.

& sorry i did the problem long time ago. so i'm not sure abt this.

SORRY again :cry:

osan
New poster
Posts: 47
Joined: Tue Jul 29, 2003 12:03 pm
Location: Bangladesh,Dhaka.
Contact:

10035

Post by osan » Sun Jan 04, 2004 8:35 pm

dear problem
i think this simple algorithm is enough for this problem. actually ur code i couldn't understand ur algorithm. so try in this way.
while(a>0 || b>0)
{
p=a%10;
q=b%10;

if(p+q+n>=10)
carry++;
n=(p+q+n)/10;
a=a/10;
b=b/10;

}
& one thing for this problem data type won't be a fact. i was wrong :cry: about that. sorry again.......

another thing if u know where algorithm has lack. then try to solve it by debugging. it will help u to build ur skill.
:D GOOD LUCK :D
this time WA
what next...............?

Blitz
New poster
Posts: 2
Joined: Tue Jul 20, 2004 7:58 am
Location: Texas
Contact:

10035 - Runtime Error (SIGSEGV)

Post by Blitz » Tue Jul 20, 2004 8:06 am

I can't seem to replicate the runtime error that I'm recieving on this problem:

Your program has died with signal 11 (SIGSEGV). Meaning: Invalid memory reference

[cpp]
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <cctype>

using namespace std;
vector <string> tokenizer(string str, string delims);

int main() {
string input, n1, n2;
getline(cin,input);
input = input.substr(0,input.length()-1); // remove newline
while (1) {
vector<string>tokens = tokenizer(input," ");
n1 = tokens[0];
n2 = tokens[1];

if (n1 == "0" && n2 == "0") break;
int result = 0;
for (int i = n2.length()-1, j = n1.length()-1; i >= 0 || j >= 0; ) {
if ((i >= 0 && j >= 0) && ((n2-'0') + (n1[j]-'0') > 9)) {
result++;
if (i-1 >= 0) n2[i-1]++;
else if (j-1 >= 0) n1[j-1]++;
} else if (i < 0) {
if (n1[j]-'0' > 9) {
if (j-1 >= 0) n1[j-1]++;
result++;
}
} else if (j < 0) {
if (n2-'0' > 9) {
if (i-1 >= 0) n2[i-1]++;
result++;
}
}
i--;
j--;
}
// output
if (result == 0)
cout << "No carry operation." << endl;
else if (result == 1)
cout << "1 carry operation." << endl;
else
cout << result << " carry operations." << endl;
// get next line
getline(cin,input);
input = input.substr(0,input.length()-1); // remove newline
}
return 0;
}

vector<string> tokenizer(string str, string delims) {
vector<string> tokens;
int pos, pos2;
pos = str.find_first_not_of(delims, 0);
while (pos >= 0) {
pos2 = str.find_first_of(delims, pos);
if (pos2 < 0) pos2 = str.length();
tokens.push_back(str.substr(pos, pos2-pos));
pos = str.find_first_not_of(delims, pos2);
}
return tokens;
}
[/cpp]

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

Post by shamim » Tue Jul 20, 2004 4:06 pm

I compiled your code using VC++ 6.0 and the runtime behavior of your program is weird.

If you really want to get AC, I suggest you take the input into two character array and not into a string. This way there is no need for any parsing.

Post Reply

Return to “Volume 100 (10000-10099)”