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
Blitz
New poster
Posts: 2
Joined: Tue Jul 20, 2004 7:58 am
Location: Texas

Post by Blitz »

Could you provide an example of taking the input in as such?
Zoe
New poster
Posts: 9
Joined: Tue Jul 20, 2004 5:46 am
Location: Taiwen

10035 WA in C

Post by Zoe »

Hi
I don't know why I got WA.
Please give me some tips.
Thanks!!

This is my code.
[c]
#include <stdio.h>

#define max 1000

main()
{
long N1[max], N2[max];
long fator1[max], fator2[max], tmp;
long aux1[max], aux2[max];
int i, j;
int amount, min[max], d1, d2;

for ( i = 0 ; i < max ; i++ )
{
scanf("%ld %ld", &N1, &N2);

if ( N1 == 0 && N2 == 0 )
{
amount = i;
break;
}
}

for ( i = 0 ; i < amount ; i++ )
{
d1 = 0;
fator1 = 1;
tmp = N1;
while ( tmp != 0 )
{
fator1 = fator1 * 10;
tmp = tmp / 10;
d1++;
}
fator1 = fator1 / 10;
d1--;
for ( j = d1 ; j >= 0 ; j-- )
{
aux1[j] = N1[i] / fator1[i];
N1[i] = N1[i] - aux1[j] * fator1[i];
fator1[i] = fator1[i] / 10;
}

d2 = 0;
fator2[i] = 1;
tmp = N2[i];
while ( tmp != 0 )
{
fator2[i] = fator2[i] * 10;
tmp = tmp / 10;
d2++;
}
fator2[i] = fator2[i] / 10;
d2--;
for ( j = d2 ; j >= 0 ; j-- )
{
aux2[j] = N2[i] / fator2[i];
N2[i] = N2[i] - aux2[j] * fator2[i];
fator2[i] = fator2[i] / 10;
}

tmp = 0;
for ( j = 0 ; j < 10 ; j++ )
{
tmp = tmp + aux1[j] + aux2[j];
if ( tmp >= 10 )
{
min[i]++;
tmp = tmp / 10;
}
else
tmp = 0;

aux1[j] = 0;
aux2[j] = 0;
}
}

for ( i = 0 ; i < amount ; i++ )
{
if ( min[i] == 0 )
printf("No carry operation.\n");
else if ( min[i] == 1 )
printf("1 carry operation.\n");
else
printf("%d carry operations.\n", min[i]);
}
}
[/c]
Zoe
New poster
Posts: 9
Joined: Tue Jul 20, 2004 5:46 am
Location: Taiwen

Post by Zoe »

I fixed some wrong and I got AC.
Thanks!!
koodeGuru
New poster
Posts: 23
Joined: Mon Apr 19, 2004 6:24 am

java rules

Post by koodeGuru »

I have solved it in java. I havent submitted yet. Can some one post the special cases I have to check for? My code is around 104 lines. I am just reading everything as a String and then converting back to integer and then again to string. For a moment I felt this problem as text processing problem. Definitely there is some arithematic involved offcourse.I will let you know people after submitting the problem.
koodeGuru
New poster
Posts: 23
Joined: Mon Apr 19, 2004 6:24 am

java rules

Post by koodeGuru »

I have solved it in java. I havent submitted yet. Can some one post the special cases I have to check for? My code is around 104 lines. I am just reading everything as a String and then converting back to integer and then again to string. For a moment I felt this problem as text processing problem. Definitely there is some arithematic involved offcourse.I will let you know people after submitting the problem.
branka
New poster
Posts: 4
Joined: Fri Sep 10, 2004 2:21 pm

10035

Post by branka »

I don't know what is wrong with my problem, I tested it with many numbers and it works fine, but i stil get wrong answer.

Program in JAVA:

Code: Select all

[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)
	{
		Main myWork = new Main();  // create a dinamic instance
	    myWork.Begin();           // the true entry point
	}

	void Begin(){

		String input;
		StringTokenizer idata;
		long st1, st2, prenesi, stej, a, b;

		while((input = Main.ReadLn (255)) != null)
		{
			idata = new StringTokenizer (input);
	    	a = Integer.parseInt(idata.nextToken());
        	b = Integer.parseInt(idata.nextToken());
			prenesi = 0;
			stej = 0;
        	if((a == 0) && (b == 0)){
				break;
			}
        	while((a > 0) || (b > 0)){
				st1 = a%10;
				st2 = b%10;
				a = a/10;
				b = b/10;
				if(st1+st2+prenesi >= 10){
					prenesi = 1;
					stej = stej + 1;
				}
				else{
					prenesi = 0;
				}
			}
			if(stej > 0){
				System.out.println(stej+" carry operations.");
			}
			else{
        		System.out.println("No carry operations.");
			}
		}
	}
}
[/java]
I allredy searchd past postings but i didn't find any helpfull information....
Please help :cry:

THANKS[/list]
Ghust_omega
Experienced poster
Posts: 115
Joined: Tue Apr 06, 2004 7:04 pm
Location: Venezuela

Post by Ghust_omega »

Hi branka when the carry operation is equals to 1 you have to say 1 carry operation no operations
Hope it helps
Keep posting!!
jhonny_yang
New poster
Posts: 22
Joined: Fri Jan 17, 2003 8:24 am

Re: 10035 Error?

Post by jhonny_yang »

give space at your all output :

cout << count << " carry operations." << endl;

to

cout << count << " carry operations.<space>" << endl;

but it AC(P.E) my program is P.E too i don't know why ?
jhonny_yang
New poster
Posts: 22
Joined: Fri Jan 17, 2003 8:24 am

Re: 10035 Error?

Post by jhonny_yang »

jhonny_yang wrote:give space at your all output :

cout << count << " carry operations." << endl;

to

cout << count << " carry operations.<space>" << endl;

but it AC(P.E) my program is P.E too i don't know why ?
But i'm not change your program and accepted maybe cause of your email. please try at //@END_OF_SOURCE_CODE at your last program
A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

Post by A1 »

I think every body should always use this

http://acm.uva.es/problemset/submit.php

page for submitting their code.
jambon_vn
New poster
Posts: 15
Joined: Wed Sep 29, 2004 6:03 am

Post by jambon_vn »

Nothing is wrong with your code. I submit your code and get AC. :D
attel75
New poster
Posts: 1
Joined: Mon Oct 04, 2004 3:14 am

The code gets Presentation Error

Post by attel75 »

I submited the code at online-judge.uva.es and it gets AC, however it gets a Presentation Error at http://www.programming-challenges.com, can anyone think of why is this? I can't seem to figure it out.
I got a different implementation of my own and I am having the same problem, it gets AC at online-judge.uva.es but Presentation Error at http://www.programming-challenges.com. Any thoughts?
PerHagen
New poster
Posts: 23
Joined: Thu Oct 14, 2004 1:45 am
Location: lima

10035 wrong answer

Post by PerHagen »

hello !
mi code is :

#include <stdio.h>
#include <iostream.h>
int calcula(int arreglo[],int arreglo2[],int n);
int ubica(int num,int arreglo[],int cant);
void main(void)
{ int a[30],b[30],n,m,i,tec,temp;


cin >> n;
for (m=1;m<=n;m++)
{
scanf("%d",&tec);
a[tec-1]=m;
}


while(scanf("%d",&temp)==1)
{
b[temp-1]=1;
for(i=2;i<=n;i++)
{

scanf("%d",&tec) ;
if ((tec-1)!=(temp-1)) b[tec-1]=i;

}
int precio=calcula(a,b,n);
cout << precio <<endl;
}


}
int calcula(int arreglo[],int arreglo2[],int n)
{
int max=-1,i,pos,k,pos2;
for (i=0;i<n;i++)
{
pos=ubica(arreglo2,arreglo,n);

k=i;int consecutivos=0;

while ((0<k) && (0<pos))
{
pos2=ubica(arreglo[pos-1],arreglo2,n);
if (pos2<k)
{
k=pos2;

consecutivos++;
}

pos--;

}

k=i; pos=ubica(arreglo2,arreglo,n);

while ((k<n) && (pos<(n-1)))
{
pos2=ubica(arreglo[pos+1],arreglo2,n);
if (k<pos2)
{
k=pos2;

consecutivos++;
}
pos++;
}

consecutivos++;
if (consecutivos> max ) max=consecutivos;
}
return max;
}

int ubica(int num,int arreglo[],int cant)
{
int encontrado=0,pos=0;
while ((encontrado==0)&& (pos < cant))
{
if (num==arreglo[pos]) encontrado=1;
else pos++;
}

return pos;
}


but is answer wrong ...i don't understand help
hello !
gits
New poster
Posts: 19
Joined: Sun Oct 26, 2003 10:08 pm
Location: Aveiro, Portugal
Contact:

Post by gits »

I couldn't even compile your program. First, you must change main() return type to int, and finish main with "return 0;"

After making this change it compiled, but it immediately crashed with Segmentation Fault. That means it didn't even produce output and that you have some invalid memory reference in your code. Look carefully for places where you might be indexing arrays out of their bounds.
Jemerson
Learning poster
Posts: 59
Joined: Mon Feb 02, 2004 11:19 pm
Contact:

Post by Jemerson »

I got it in Java

Juz for some help try this algorithm:
1. read as int
2. transform the number in an array of digits (2 int arrays for 2 numbers)
3. pick the greatest number of digits between them.
4. for i =0 to greatest, sum the digits of the ith position if > 9 increase the i+1 in any of the arrays and increase the carry counter.
5. print carry information

My code got 60 lines including readLn() method

As someone asked, follow some of the inputs that caused me some trouble.

Input:

9999 1
1 9999
9989 1
9899 1
1 9989
999999999 999999999
000000001 999999999
0 0

Output

4 carry operations.
4 carry operations.
1 carry operation.
2 carry operations.
1 carry operation.
9 carry operations.
9 carry operations.

Good Luck
UFCG Brazil - Computer Science graduate student
http://acm.uva.es/problemset/usersnew.php?user=54806 ... and going up!
Post Reply

Return to “Volume 100 (10000-10099)”