10035 - Primary Arithmetic
Moderator: Board moderators
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]
[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
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Morning,Morning wrote:no,that's not the reason.can someone help me?
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.
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
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
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
10035 wa help me plz
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]
[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]
10035 Error?
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]

[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!
We are here as Adam and Eve were here!
help need
[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]
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]
10035
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
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
sorry problem i did a mistake
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
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

10035
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
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.
GOOD LUCK 
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

another thing if u know where algorithm has lack. then try to solve it by debugging. it will help u to build ur skill.


this time WA
what next...............?
what next...............?
10035 - Runtime Error (SIGSEGV)
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]
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]