## 11148 - Moliu Fractions

Moderator: Board moderators

Spykaj
New poster
Posts: 47
Joined: Sun May 21, 2006 12:13 pm

### 11148 - Moliu Fractions

Do you have any very hard input ? My program get WA, though i think it's correct :/
temper_3243
Experienced poster
Posts: 105
Joined: Wed May 25, 2005 7:23 am
even i am getting WA.
here is my code.

Code: Select all

``````
#include<stdio.h>
#include<ctype.h>
#include<string.h>

char arr[1001];
unsigned long long gnum, gden;

unsigned long long
gcd (unsigned long long m, unsigned long long n)
{
if (n == 1)
return 1;

if (m % n == 0)
return n;

return gcd (n, m % n);

}

void
init (void)
{
char *y, *s, *t;
unsigned long cnt, fl, p1, p2, p3, num, den;
fgets (arr, 1000, stdin);

gnum = 1ULL;
gden = 1ULL;

s = arr;
cnt = 0;
while ((y = strtok (s, " ")) != NULL)
{
s = NULL;
t = y;
fl = 0;
p1 = 0;
p2 = 0;
p3 = 0;
if (isdigit (*t))
{
while (*t && isdigit (*t))
{
fl = 1;
p1 *= 10;
p1 += *t - '0';
t++;
}
if (*t == '-')
{
fl = fl + 3;
p3 = 0;
t++;
while (*t && isdigit (*t))
{
p3 *= 10;
p3 += *t - '0';
t++;
}
}
if (*t == '/')
{
t++;
fl = fl + 5;
p2 = 0;
while (*t && isdigit (*t))
{
p2 *= 10;
p2 += *t - '0';
t++;
}
}

if (fl == 1)
{
num = p1;
den = 1;
}
if (fl == 6)
{
num = p1;
den = p2;
}
if (fl == 9)
{
den = p1 * p2;
num = den + p3;
den = p2;
}
gnum *= num;
gden *= den;

}

}

return;
}

int
main ()
{

int no;
unsigned long long k;
fgets (arr, 1000, stdin);
no = strtoul (arr, NULL, 10);
while (no--)
{
init ();
k = 1;
k = gcd (gnum, gden);
if (k >= 1)
{
gnum /= k;
gden /= k;
}

if (gden == 1)
{
printf ("%llu\n", gnum);
}
else if (gnum < gden)
{
printf ("%llu/%llu\n", gnum, gden);
}
else
{
printf ("%llu-%llu/%llu\n", gnum / gden, gnum % gden, gden);
}

}
return 0;
}

``````
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York
Aha..

I think you made the same mistake that I initially made. Had to debug my code for 30 mins to find it.

input

Code: Select all

``````1
this_is_critical1/2 1/2
``````
output

Code: Select all

``````1/4
``````
Spykaj
New poster
Posts: 47
Joined: Sun May 21, 2006 12:13 pm
My code:

Code: Select all

``````Cut after acc ;]
``````
Last edited by Spykaj on Sun Dec 31, 2006 1:11 pm, edited 2 times in total.
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York
Input

Code: Select all

``````1
-there_can_be_cases_like_this 1/2 1/3
``````
Output

Code: Select all

``````1/6
``````
indogila
New poster
Posts: 2
Joined: Sun Dec 31, 2006 11:19 am
Hi. I am new here.
I got the same problem. It's WA.
(at first my code is very short, but after reading that there can be many tricky cases I changed it much and it became this long )

Code: Select all

``removed``
sorry. I remove my code.
My code doing stupid things in cases like : "cases_like - 2 - 2 - 2 / 2 !"

I will repair it
Thank you very much.
Last edited by indogila on Mon Jan 01, 2007 6:05 am, edited 1 time in total.
Spykaj
New poster
Posts: 47
Joined: Sun May 21, 2006 12:13 pm
Thanks sohel ! ACC
artie
New poster
Posts: 8
Joined: Sun Dec 31, 2006 11:56 am
Location: Russia
In the contest I got AC, but in the oj I got restricted function. What should I do?

Code: Select all

``````got AC
``````
Last edited by artie on Fri Jan 05, 2007 7:29 am, edited 1 time in total.
FAQ
Learning poster
Posts: 84
Joined: Wed Jan 28, 2004 6:23 pm
artie, try to avoid all 'runerror(200)' in your code
artie
New poster
Posts: 8
Joined: Sun Dec 31, 2006 11:56 am
Location: Russia
FAQ wrote:artie, try to avoid all 'runerror(200)' in your code
runerror would never execute if input and code is correct, so why it cause restricted function?

BTW, I removed it and got AC
Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong
artie wrote:runerror would never execute if input and code is correct, so why it cause restricted function?
I guess Restrict Function verdict is given in the compile stage.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
kolpobilashi
Learning poster
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Contact:
i am getting RE in this problem.....can anyone tell me the correct output for the following input

Code: Select all

``````3
2-3/sdfsdf sefd 1/4
1-szdc/4 abc 3``````
Sanjana
mmonish
Experienced poster
Posts: 109
Joined: Sun Mar 11, 2007 2:55 pm
Location: SUST
output

Code: Select all

``````1-1/2
1/4
3
``````
Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong
kolpobilashi, I can tell you that there are NO cases like those...
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
algoJo
New poster
Posts: 37
Joined: Sun Dec 17, 2006 9:02 am
Hi what the output for:
INPUT

Code: Select all

``````8
What is the product of 2 , 6/55 and 3-1/3 ?
A cookie weighs exactly 18-2/5 g. What is the weight of a packet of 12 cookies?
What is the product of 2 , 6/55 and 3-0/3 ?
A cookie weighs exactly 0-2/5 g. What is the weight of a packet of 12 cookies?
-there_can_be_cases_like_this 3/2 1/345
What is the product of 2 , 6/0 and 3-1/0 5/6 ?
A cookie weighs exactly 18-2/5 g. What is the weight of a packet of 12 cookies?
mangga mangga 0/6 mangga 5 mangga
``````
Thanks before