275 - Expanding Fractions

All about problems in Volume 2. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

gonzo
New poster
Posts: 1
Joined: Tue Nov 16, 2004 10:12 am
Location: CR
Contact:

presentation error

Post by gonzo »

I'm still getting Presentation Error. Can someone check my source code?

[c]
#include <stdio.h>
#include <stdlib.h>

int find(int *pole, int max) {
int i;
for(i=0; i<max; i++)
if(pole[max]==pole)
return i;
return -1;
}


int main() {
int numerator, denominator, a, i, j, x;
int nula;
int *rest;
int *solution;
int first=0;

while((scanf("%d %d", &numerator, &denominator)==2) &&(numerator!=0 || denominator!=0)){

nula=0;
a=denominator;

solution=(int *)malloc(denominator*sizeof(int));
rest=(int *)malloc(denominator*sizeof(int));

for(i=0; i<denominator; i++){
if(numerator==0 && i==0)
break;
if(numerator < denominator)
numerator = numerator * 10;
rest = numerator;

x = find(rest, i);
if(x==-1){
if(numerator%denominator==0) {
nula=1;
solution = numerator / denominator;
i++;
break;
}
else {
solution = numerator / denominator;
a = solution * denominator;
numerator = numerator - a;
}
}
else{
break;
}
}


if (first==0){
printf(".");
first=1;
}
else
printf("\n.");

if(numerator==0 && i==0)
printf("\nThis expansion terminates.\n");
else {

for(j=0; j<i; j++){
printf("%d", solution[j]);
if(j==48)
printf("\n");
if(j>48 && (((j-48)%50)==0))
printf("\n");
}

if(nula==1)
printf("\nThis expansion terminates.\n");
else
printf("\nThe last %d digits repeat forever.\n", i-x);
}
}


return 0;
}

[/c][/code]
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind »

I cant find out those inputs for which my program
doesn't give the correct answer

can anybody help me?

I give my code here
(Not for check the code
but only for check the critical input output
for which my program give incorrect output)

Code: Select all

#include <iostream>
#include <stdio.h>

using namespace std;

#define max 30010

int num[max+1], n;
char digit[100000000];

int main (void)
{
	int numerator, nominator;
	int i, j, st, en, signal, repeat;cout<<"";
	while(scanf("%d%d",&numerator,&nominator)==2){
		if(!(numerator || nominator))break;
		n=(numerator%nominator);
		printf(".");
		for(i=0;i<=nominator*10;i++)num[i]=0;
		i=1;
		signal=1;
		repeat=1;
		while(1){
			if(num[n]){
				st=num[n];
				en=i;
				if(n<nominator && num[n*10])st=num[n*10];
				if(num[n]==1 && n<nominator && num[n*10] && num[n*10]!=1){
						digit[i]='0';
						i++;
						en=i;
					}
				break;
			}
			if(!n){
				digit[i]='0';
				i++;
				st=i-1;
				en=i-1;
				repeat=0;
				break;
			}
			num[n]=i;
			if(n<nominator){
				n*=10;
				if(num[n]){
					st=num[n];
					i++;
					en=i;
					if(n<nominator && num[n/10]>num[st]){
						digit[i-1]='0';
					}
					if(num[n]==1 && n<nominator && num[n*10] && num[n*10]!=1){
							digit[i]='0';
							i++;
							en=i;
						}
					break;
				}
			}
			
			else if(num[n]){
				st=num[n];
				i++;
				en=i;
				if(n<nominator && num[n*10]){
					st=num[n*10];
				}
				if(num[n]==1 && n<nominator && num[n*10] && num[n*10]!=1){
						digit[i]='0';
						i++;
						en=i;
					}
				break;
			}
			num[n]=i;
			
			while(n<nominator){
				n*=10;
				digit[i]='0';
				i++;
				if(num[n]){
					st=num[n];
					en=i;
					if(n<nominator && num[n*10])st=num[n*10];
					if(num[n]==1 && n<nominator && num[n*10]){
						digit[i]='0';
						i++;
						en=i;
					}
					signal=0;
					break;
				}
				num[n]=i;
			}
			if(!signal)break;

			
			digit[i]=(n/nominator)+'0';
			n=n%nominator;
			i++;
		}
		for(i=1;i<st;i++){
			if((i+50)%50 == 0){
				cout<<endl;
			}
			cout<<digit[i];
		}
	
		for(i=st;i<en;i++){
			if((i+50)%50 == 0){
				cout<<endl;
			}
			cout<<digit[i];
		}
		if(repeat)
		printf("\nThe last %d digits repeat forever.\n\n",en-st);
		else
		printf("\nThis expansion terminates.\n\n");
	} 
	return 0;
}
thanks ..[/list]
guma
New poster
Posts: 5
Joined: Tue Mar 15, 2005 10:33 pm
Location: Joao Pessoa, Paraiba, Brasil

Problem 275, does anyone have any trick I/O?

Post by guma »

I' ve just done the 202 problem and I thought the solution of the 275 would be very easy. I guess I was wrong :P
All the i/o that i got, wich by the way were only the sample ones, are passing. But the judge keep saying me WA.

What should be the output for something like:
0 5
My program shows:
.0
This expansion terminates.
Is this write? Is there any [other] trick I/O?

Thanks for your time,

and sorry for the bad english.
joy
New poster
Posts: 48
Joined: Wed Oct 18, 2006 1:00 pm
Location: Dhaka, Bangladesh
Contact:

Post by joy »

I got PE several times, but do not understand why?
Please help

here is my code

Code: Select all

remove after acc...
Last edited by joy on Sun Dec 24, 2006 4:42 pm, edited 1 time in total.
form kisui na ... class tai asol....
iF U hv d class u get the form....
rio
A great helper
Posts: 385
Joined: Thu Sep 21, 2006 5:01 pm
Location: Kyoto, Japan

Post by rio »

Try this input and you will know why.

Code: Select all

1 998
0 0
joy
New poster
Posts: 48
Joined: Wed Oct 18, 2006 1:00 pm
Location: Dhaka, Bangladesh
Contact:

Post by joy »

Thanks rio...

It is really a cool input!!
form kisui na ... class tai asol....
iF U hv d class u get the form....
algoJo
New poster
Posts: 37
Joined: Sun Dec 17, 2006 9:02 am

Post by algoJo »

can somebody tell me why I got WA in this problem?
I've tried all the I/O set and it gave the same result?
any hints?

Code: Select all

#include<stdio.h>
#include<string.h>
#define MAX 30001

char arr[MAX];
int start,end;

int compute(void){
	int i,j,k,len,I,flag=0;
	len=strlen(arr);
	for(i=0;i<len;i++)
	{
		for(j=i+1;j<len;j++)
		{
			if(arr[i]==arr[j])
			{
				start=i;end=j;
				if(j-i==1)
				{
					for(k=j;k<len;k++)
						if(arr[k]!=arr[j]) {flag=0;break;}
						else flag=1;

				}
				else
				{
				I=j;
				for(k=i;k<j;k++,I++)
					if(arr[k]==arr[I]) flag=1;
					else {flag=0;break;}
				}
			}
			if(flag) return j-i;
		}
		if(flag) break;
	}
	return 0;
}

void main(){
	int a,b,k,I=0,save,count=1,cas=0;
	char kar;
	while(scanf("%d %d",&a,&b)==2)
	{
	if(!a&&!b) break;
	if(cas) printf("\n");
	else cas=1;
	I=0;count=1;
	memset(arr,0,sizeof(arr));
	save=a/b;
	a%=b;
	while(1)
	{
		a*=10;
		k=a/b;
		a%=b;
		kar=k+'0';
		arr[I]=kar;I++;
		if(I==MAX-1) break;
	}
	k=compute();
	printf(".");
	if(save) printf("%d",save);
	if(k==1)
	{
		if(arr[start]=='0'){for(I=0;I<end-1;I++,count++) {printf("%c",arr[I]);if(count==49) {printf("\n");count=-1;}}}
		else for(I=0;I<end;I++,count++) {printf("%c",arr[I]);if(count==49) {printf("\n");count=-1;}}
	}
	else{
	for(I=0;I<end;I++,count++)
	{
		printf("%c",arr[I]);
		if(count==49){printf("\n");count=-1;}
	}
	}
	if(k==1)
	{
		if(arr[start]!='0')printf("\nThe last %d digits repeat forever.\n",k);
	else printf("\nThis expansion terminates.\n");
	}
	else printf("\nThe last %d digits repeat forever.\n",k);
	count=1;
}

}

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Try the cases...

Input:

Code: Select all

451 536
529 668
736 767
0 0
Output:

Code: Select all

.841417910447761194029850746268656716
The last 33 digits repeat forever.

.7919161676646706586826347305389221556886227544910
17964071856287425149700598802395209580838323353293
41317365269461077844311377245508982035928143712574
8502994011976047904
The last 166 digits repeat forever.

.9595827900912646675358539765319426336375488917861
79921773142112125162972620599739243807040417209908
73533246414602346805736636245110821382007822685788
7874837027379400260756192
The last 174 digits repeat forever.
Hope these help.
Ami ekhono shopno dekhi...
HomePage
JCU
New poster
Posts: 2
Joined: Mon Nov 26, 2007 4:35 pm

Post by JCU »

Code: Select all

#include<iostream>
using namespace std;

int main()
{
    long long a,b;
    long long q;
    int m[100000] = {0};
    int n[100000] = {0};
    int i;
    int jump;
    int counter = 1;
    while(cin >> a >> b)
    {
        if(a == 0 && b == 0)
            break;
            
        if(a%b == 0)
        {
            cout << ".";
            cout << "\nThis expansion terminates.\n";
            continue;       
        }
        
        i = 0;
        
        for(int j = 0;j < 100000;j++)
        {
            m[j] = 0;
            n[j] = 0;        
        }
        
        
        m[a%b] = 1;
        a*=10;
        counter = 1;
        while(1)
        {
            if(a%b == 0)
            {
                n[i++] = a/b;
                break;   
            }
            else
            {
                
                if(m[a%b] == 0)
                {
                    m[a%b] = ++counter;
                    n[i++] = a/b;
                    a%=b;
                    a*=10;
                }
                else
                {
                    ++counter;
                    n[i++] = a/b;
                    break;
                }    
            }
        }
        
        jump = 0;
        
        if(a%b == 0)
        {
            cout << ".";
            for(int j = 0;j < i;j++)
            {
                jump++;
                if(jump == 50)
                {
                    cout << endl;
                    jump = 0;        
                }
                cout << n[j];
            }   
            cout << "\nThis expansion terminates.\n";
        }
        else
        {
            cout << ".";
            for(int j = 0;j < i;j++)
            {
                jump++;
                if(jump == 50)
                {
                    cout << endl;
                    jump = 0;        
                }
                cout << n[j];
            }
            cout << "\nThe last " << counter - m[a%b] << " digits repeat forever.\n\n";
        }
              
    }

    return 0;
}
Can some one help me?
I test all case from this forum
but my answer is match all case
but i still get WA......
plz give me some advice
thanks a lot
I love coding ^^
abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

Re: 275 why WA

Post by abid_iut »

Why I am getting WA
I think I pass correct output of all existing input
please someone check
here is the code:

Code: Select all

Removed
pls help :(
Last edited by abid_iut on Sun Dec 07, 2008 9:09 pm, edited 1 time in total.
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/
Articuno
Learning poster
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm
Location: IUT-OIC, Dhaka, Bangladesh

Re: 275

Post by Articuno »

Your program is not getting correct answer for all test cases. Try this case:

Code: Select all

1 2
And there is another thing:
multiple line expansions should each contain exactly 50 characters on each line(except the last line, which, of course, may be shorter) - that includes the beginning decimal point.
You forgot about this.
I think that will help. Good luck :-?
Last edited by Articuno on Sun Dec 07, 2008 7:37 pm, edited 2 times in total.
May be tomorrow is a better day............ :)
abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

Re: 275

Post by abid_iut »

hey Articuno
I think I have solved those problems U mentioned but still WA
is there anymore thing to solve
and

Code: Select all

input:
1 2
output:
.5
This expansion terminates.

is this OK
pls help.
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/
Articuno
Learning poster
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm
Location: IUT-OIC, Dhaka, Bangladesh

Re: 275

Post by Articuno »

Yes, your this output is correct. What about this case:

Code: Select all

2 2
it should be:

Code: Select all

.0
This expansion terminates.
The rule about the newline is also applicable for the cases where the expansion terminates but there are more than 50 characters in a single line. Did you chechk this?
I have changed a little in your code and now it is AC. Try again.
Good luck. :)

if you are getting wrong answers after this, you can post your code. May be i can help
May be tomorrow is a better day............ :)
abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

Re: 275 WA

Post by abid_iut »

Hey Articuno my friend
I become very sad
You try to give me way
But I am getting WA
here is the modified code(need more modification :-? )

Code: Select all

removed
Last edited by abid_iut on Mon Dec 08, 2008 5:31 pm, edited 1 time in total.
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/
Articuno
Learning poster
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm
Location: IUT-OIC, Dhaka, Bangladesh

Re: 275

Post by Articuno »

Well i have edited your code. It should be OK now:

Code: Select all

Removed
Hope it will help.
:)
Last edited by Articuno on Mon Dec 08, 2008 6:03 pm, edited 1 time in total.
May be tomorrow is a better day............ :)
Post Reply

Return to “Volume 2 (200-299)”