332 - Rational Numbers from Repeating Fractions

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

Moderator: Board moderators

roticv
Learning poster
Posts: 63
Joined: Sat Dec 11, 2004 9:28 am

Post by roticv »

Hi, I have tried that input and got the correct answer. However the judge still thinks that my code is wrong.

Code: Select all

#include <stdio.h>
#include <string.h>
#include <math.h>

int n,z,len;
long long num, deno, result;
long double f,f2;
char str[20];

long long GCD(long long a,long long b) {
	if (b>a){
		a ^= b;
		b ^= a;
		a ^= b;
	}
	while (b > 0) {
		a = a % b;
		a ^= b;
		b ^= a;
		a ^= b;
	}
	return a;
}

double pow10(double a){
	if (a==0.0)
		return 1.0;
	else
		return pow(10,a);
}

int main(){
	z = 0;
	while (scanf("%d",&n)!=EOF){
		if (n==-1)
			break;
		scanf("%s",str);
		sscanf(str,"%llf",&f);
		len = strlen(str) - 1;
		str[len+1-n] = 0;
		len -= n;
		sscanf(str,"%llf",&f2);
		f *= pow10(len+n);
		f2 *= pow10(len);
		if (n==0)
			num = (long long)f;
		else
			num = (long long)f-f2;
		if (n==0)
			deno = pow10(len);
		else
			deno = (long long) pow10(len+n) - pow10(len);
		result = GCD(num, deno);
		num /= result;
		deno /= result;
		z++;
		printf("Case %d: %lld/%lld\n",z,num,deno);
		
	}
	return 0;
}

narsys
New poster
Posts: 5
Joined: Sat Jun 04, 2005 6:34 am

Help with 332 (correct but ...)

Post by narsys »

Your program has died with signal 8 (SIGFPE). Meaning:

Floating point exception

In my computer it work
my code is:
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
string num;
int repeated;
int noRepeatedNum=0;
int i,length,dotPosition=0,counter=0;
int precisionNum=0;
int integerNum=0;
bool precision=false;
int digitCount;
int makhraj;
int temp;

while (cin>>repeated)
{
if(repeated==-1)
{
counter=0;
continue;
}
noRepeatedNum=0;
precisionNum=0;
integerNum=0;
cin>>num;
length = num.size();

for (i=0; i<length-repeated ; i++)
{

if (num=='.')
{
precision = true;
dotPosition=i;
continue;
}
if(precision==true)
{
noRepeatedNum = noRepeatedNum*10 + num - '0';
}
else
{
integerNum = integerNum*10 + num - '0';
}


}

for (i=dotPosition+1; i<length ; i++)
{
precisionNum = precisionNum*10 + num - '0';
}


digitCount = length - (dotPosition+1);
precisionNum=precisionNum - noRepeatedNum;

makhraj = int(pow(10,digitCount)) - int (pow(10,digitCount-repeated));

// cout<<makhraj;

int n=makhraj,m=precisionNum;

//GCD (find GCD)
while(n%m!=0 )
{
temp=n%m;
n=m;
m=temp;
}

makhraj = makhraj/ m;
precisionNum = (precisionNum / m )+ (integerNum * makhraj);
counter++;

cout <<"Case "<<counter<<": "<<precisionNum<<'/'<<makhraj<<endl;
}





}
narsys
New poster
Posts: 5
Joined: Sat Jun 04, 2005 6:34 am

I find Problem

Post by narsys »

It occurs when your program want this: x/0 (divide number by 0)
I write another code that work efficient and simpler and get AC

0 0.0
thinker_bd
New poster
Posts: 22
Joined: Thu Jun 09, 2005 1:44 am

332 - Rational Numbers from Repeating Fractions

Post by thinker_bd »

pleae say me why i am getting RTE

Code: Select all

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 1000

char val[MAX];

int array2intiger(char array[MAX],int count)
{
    int i,p=count,intiger=0;
	
	for(i=2;i<count;i++)
	{
		p=p-1;	
		intiger+=((val[i]-48)*((int)(pow(10,p))));
	}
	return intiger;
} 

int gcd(int a,int b)
{
	if(b==0)
		return a;
	 else 
		return gcd(b,a%b);
}



int main()
{
//	freopen("332.txt","r",stdin);	
	
	int t_case=0;
	
	while(1)
	{
		t_case++; 
		int ripid=0;
			scanf("%d%s",&ripid,&val);

		if(ripid==-1)
			break;
		
		printf("Case %d: ",t_case);
	
		int val_count=strlen(val); 
		int val_int=array2intiger(val,val_count);
		
		
		val_int=val_int/100;
		val_count=val_count-2;
		

		double div=pow(10,val_count);
		double val_main=val_int/div;

		int not_ripid=val_count-ripid;
    
		double a=((pow(10,(ripid+not_ripid)))*(val_main));
		double b=((pow(10,not_ripid))*(val_main));
		double res=ceil(a-b);
		int denom=(int)(res);
	
		double a2=(pow(10,(ripid+not_ripid)))-(pow(10,not_ripid));
		
		int nom=(int)(a2);
		int gcd_v=gcd(denom,nom);
		
		nom=nom/gcd_v;
		denom=denom/gcd_v;
	
		printf("%d/%d\n",denom,nom);
	}
	return 0;
}
Raj Ariyan
Learning poster
Posts: 70
Joined: Sat Feb 05, 2005 9:38 am
Location: Gurukul

?

Post by Raj Ariyan »

Hi Thinker_BD,
You did not tell which RTE u've got(SegV/SegPE). Are u sure that ur code handle j=0 and gcd_v is zero. I think u should check this case to avoid division by zero. And there is so much thread abt this topic where u can get I/O. So search for it. Good Luck :D
Some Love Stories Live Forever ....
thinker_bd
New poster
Posts: 22
Joined: Thu Jun 09, 2005 1:44 am

but still problem

Post by thinker_bd »

hi raj,
my modified code is given below i tried to avoid here divided by zero,
my RTE IS

Your program has died with signal 8 (SIGFPE). Meaning:

Floating point exception

Before crash, it ran during 0.000 seconds.

------

Code: Select all


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 1000

char val[MAX];

int array2intiger(char array[MAX],int count)
{
    int i,p=count,intiger=0;
	
	for(i=2;i<count;i++)
	{
		p=p-1;	
		intiger+=((val[i]-48)*((int)(pow(10,p))));
	}
	return intiger;
} 

int gcd(int a,int b)
{
	if(b==0)
		return a;
	 else 
		return gcd(b,a%b);
}



int main()
{
freopen("332.txt","r",stdin);	
	
	int t_case=0;
	
	while(1)
	{
		t_case++; 
		int ripid=0;
			scanf("%d%s",&ripid,&val);

		if(ripid==-1)
			break;
		
		printf("Case %d: ",t_case);
	
		int val_count=strlen(val); 
		int val_int=array2intiger(val,val_count);
		
		
		val_int=val_int/100;
		val_count=val_count-2;
		

		double div=pow(10,val_count);
		double val_main;
		
		if(div>0)
			val_main=val_int/div;
        else
			val_main=val_int;
		
		int not_ripid=val_count-ripid;
    
		double a=((pow(10,(ripid+not_ripid)))*(val_main));
		double b=((pow(10,not_ripid))*(val_main));
		double res=(double)ceil(a-b);
		int denom=(int)(res);
	
		double a2=(pow(10,(ripid+not_ripid)))-(pow(10,not_ripid));
		
		int nom=(int)(a2);
		int gcd_v=gcd(denom,nom);
		
		if(gcd>0)
		{
			nom=nom/gcd_v;
			denom=denom/gcd_v;
		}
		printf("%d/%d\n",denom,nom);
	}
	return 0;
}
Raj Ariyan
Learning poster
Posts: 70
Joined: Sat Feb 05, 2005 9:38 am
Location: Gurukul

Post by Raj Ariyan »

Hi Thinker,
In my previous thread i told u that r u sure that ur code handle j=0 ? Your code still not handle this case. Then what u modified ???? :o You'r code still get gcd_v=0. Thats why SIGFPE.

Try this case :-

Code: Select all

0 0.11
0 0.1234
0 0.9999999
Some Love Stories Live Forever ....
thinker_bd
New poster
Posts: 22
Joined: Thu Jun 09, 2005 1:44 am

Post by thinker_bd »

after some modification i avoid RTE but i am getting WA, the code given below. whats my wrong . if possible give me some sample input output.

Code: Select all

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 1000

char val[MAX];

int array2intiger(char array[MAX],int count)
{
    int i,p=count,intiger=0;
	
	for(i=2;i<count;i++)
	{
		p=p-1;	
		intiger+=((val[i]-48)*((int)(pow(10,p))));
	}
	return intiger;
} 

int gcd(int a,int b)
{
	if(b==0)
		return a;
	 else 
		return gcd(b,a%b);
}



int main()
{
//freopen("332.txt","r",stdin);	
	
	int t_case=0;
	
	while(1)
	{
		t_case++; 
		int ripid=0;
			scanf("%d%s",&ripid,&val);

		if(ripid==-1)
			break;
		
		printf("Case %d: ",t_case);
	
		int val_count=strlen(val); 
		int val_int=array2intiger(val,val_count);
		
		
		val_int=val_int/100;
		val_count=val_count-2;
		

		double div=pow(10,val_count);
		double val_main;
		
		if(div>0)
			val_main=val_int/div;
        else
			val_main=val_int;
		
		int not_ripid=val_count-ripid;
    
		double a=((pow(10,(ripid+not_ripid)))*(val_main));
		double b=((pow(10,not_ripid))*(val_main));
		double res=(double)ceil(a-b);
		int denom=(int)(res);
	
		double a2=(pow(10,(ripid+not_ripid)))-(pow(10,not_ripid));
		
		int nom=(int)(a2);
		int gcd_v=gcd(denom,nom);
		
		if(gcd_v>0)
		{
			nom=nom/gcd_v;
			denom=denom/gcd_v;
		}
		printf("%d/%d\n",denom,nom);
	}
	return 0;
}
Raj Ariyan
Learning poster
Posts: 70
Joined: Sat Feb 05, 2005 9:38 am
Location: Gurukul

Post by Raj Ariyan »

Hi THinker,
Try this input/output. Hope it helps.

Input:-

Code: Select all

2   0.318     
1 0.3
2 0.09
6 0.714285
6 0.714285000
1 0.9999
9 0.123456789
8 0.987654321  
9 0.574454131
5 0.83777471
1 0.22222222
9 0.111111111
9 0.222222222
9 0.333333333
9 0.444444444   
9 0.555555555
9 0.666666666
9 0.777777777
9 0.888888888
9 0.999999999
9 0.000000000
1 0.200
8 0.200000000
8 0.020000000
0 0.3
0 0.5
0 0.55
1 0.55  
6 0.142857
0    0.9
1 0.9
6 0.076923
0 0.678453453
0 0.1
2 0.31818
9 0.345678993
2   0.25
1 0.3
0 0.3
0 0.0
5 0.99999
6 0.714285
0 0.5
0 0.35
1 0.111111111
  2   0.111111111
3 0.111111111
5 0.132456454
6   0.456467879
9 0.456467879
7 0.456467879
1 0.414141414
9 0.414141414
1 0.999999999
1 0.507462687
0 0.347826087
3 0.935483871
4 0.111111111
0 0.166666667
3 0.604166667
3 0.642857143
1 0.606060606
1 0.258064516
4 0.666666667
3 0.973684211
3 0.353658537
1 0.454545455
4 0.068965517
0 0.077922078
3 0.244186047
0 0.333333333
0 0.376623377
3 0.123711340
1 0.677777778
1 0.820895522
0 0.418918919
2 1.000000000
1 0.363636364
0 0.076923077
2 0.649122807
2 0.638554217
0 1.000000000
3 0.238636364
2 0.130434783
0 0.191176471
0 0.681318681
0 0.169491525
4 0.770833333
3 0.431578947
2 0.549450549
1 0.270270270
1 0.437500000
4 0.809523810
1 0.641509434
4 0.473684211
0 0.306818182
2 0.301075269
3 0.084337349
1 0.588235294
3 0.642857143
1 0.686274510
0 0.387755102
1 0.030612245
4 0.181818182
4 0.022471910
0 0.913978495
3 0.264367816
4 0.062500000
0 0.310344828
0 0.979591837
1 0.908163265
4 0.791666667
2 0.413043478
3 0.877777778
2 0.637362637
0 0.636363636
1 0.058823529
2 0.509090909
1 0.720930233
0 0.478260870
2 0.363636364
1 0.224489796
1 0.212121212
2 0.826086957
4 0.160000000
0 0.300000000
4 0.693181818
1 0.617977528
3 0.023529412
2 0.965909091
1 0.298245614
2 0.463768116
4 0.235955056
1 0.500000000
4 0.791666667
6 0.666666667
0 0.111111111
1 0.407407407
3 0.933333333
3 0.878048780
4 0.666666667
4 0.656250000
6 0.166666667
1 0.604166667
7 0.117647059
4 0.400000000
0 0.258064516
0 0.666666667
3 0.973684211
4 0.062500000
6 0.363636364
1 0.458333333
3 0.428571429
5 0.619047619
5 0.480000000
0 0.300000000
5 0.289473684
1 0.128205128
7 0.057142857
6 0.921052632
2 0.411764706
0 0.593750000
2 0.538461538
1 0.50000
0 0.79167
3 0.66667
4 0.11111
1 0.40741
1 0.93333
2 0.87805
4 0.66667
2 0.65625
1 0.40000
1 0.96552
1 0.87500
2 0.15789
0 0.33333
3 0.10870
0 0.68966
0 0.16667
3 0.60417
3 0.11765
1 0.40000
1 0.25806
0 0.3158
3 0.9444
2 0.2222
1 0.8667
3 0.2222
2 0.1000
0 0.4667
3 0.7857
0 1.0000
1 0.6667
2 0.6364
3 1.0000
3 0.2308
1 1.0000
2 0.1250
2 1.0000
2 0.6154
0 0.1818
3 0.5263
0 0.4706
3 0.0667
2 0.9091
0 0.0000
0 0.1250
3 0.5000
1 0.088
1 0.474
0 0.711
2 0.651
0 0.212
0 0.588
1 0.643
2 0.029
2 0.388
2 0.063
2 0.182
2 0.051
0 0.814
0 0.622
1 0.063
0 0.444
0 0.958
2 0.813
1 0.318
0 0.905
1 0.50
0 0.79
0 0.67
0 0.11
1 0.41
1 0.93
1 0.88
0 0.67
0 0.66
0 0.40
1 0.81
0 0.63
1 0.71
0 0.27
0 0.70
0 0.27
1 0.38
0 0.58
1 0.39
1 0.97
1 0.667
0 0.750
1 0.500
1 0.833
2 0.900
0 0.000
2 0.727
0 0.316
1 0.944
2 0.222
2 0.867
1 0.222
0 0.353
2 0.167
2 0.333
0 0.529
0 0.706
1 0.100
0 0.467
0 0.786
1 1.000
0 0.667
1 0.636
1 1.000
2 0.231
2 1.000
2 0.125
2 0.130435
4 0.722222
0 0.292683
5 0.900000
0 0.770833
1 0.642857
2 0.028571
2 0.387755
2 0.062500
-1
Output:-

Code: Select all

Case 1: 7/22
Case 2: 1/3
Case 3: 1/11
Case 4: 5/7
Case 5: 119047381/166666500
Case 6: 1/1
Case 7: 13717421/111111111
Case 8: 54869684/55555555
Case 9: 574454131/999999999
Case 10: 41888317/49999500
Case 11: 2/9
Case 12: 1/9
Case 13: 2/9
Case 14: 1/3
Case 15: 4/9
Case 16: 5/9
Case 17: 2/3
Case 18: 7/9
Case 19: 8/9
Case 20: 1/1
Case 21: 0/1
Case 22: 1/5
Case 23: 1/5
Case 24: 2000000/99999999
Case 25: 3/10
Case 26: 1/2
Case 27: 11/20
Case 28: 5/9
Case 29: 1/7
Case 30: 9/10
Case 31: 1/1
Case 32: 1/13
Case 33: 678453453/1000000000
Case 34: 1/10
Case 35: 7/22
Case 36: 38408777/111111111
Case 37: 25/99
Case 38: 1/3
Case 39: 3/10
Case 40: 0/1
Case 41: 1/1
Case 42: 5/7
Case 43: 1/2
Case 44: 7/20
Case 45: 1/9
Case 46: 1/9
Case 47: 1/9
Case 48: 4415171/33333000
Case 49: 456467423/999999000
Case 50: 456467879/999999999
Case 51: 228233917/499999950
Case 52: 372727273/900000000
Case 53: 138047138/333333333
Case 54: 1/1
Case 55: 456716419/900000000
Case 56: 347826087/1000000000
Case 57: 233637097/249750000
Case 58: 1/9
Case 59: 166666667/1000000000
Case 60: 603562501/999000000
Case 61: 321107143/499500000
Case 62: 90909091/150000000
Case 63: 15483871/60000000
Case 64: 666600001/999900000
Case 65: 972710527/999000000
Case 66: 117768293/333000000
Case 67: 40909091/90000000
Case 68: 7662069/111100000
Case 69: 38961039/500000000
Case 70: 243941861/999000000
Case 71: 333333333/1000000000
Case 72: 376623377/1000000000
Case 73: 123587629/999000000
Case 74: 610000001/900000000
Case 75: 73880597/90000000
Case 76: 418918919/1000000000
Case 77: 0/1
Case 78: 40909091/112500000
Case 79: 76923077/1000000000
Case 80: 642631579/990000000
Case 81: 25286747/39600000
Case 82: 0/1
Case 83: 7449929/31218750
Case 84: 32282609/247500000
Case 85: 191176471/1000000000
Case 86: 681318681/1000000000
Case 87: 6779661/40000000
Case 88: 37/48
Case 89: 431147369/999000000
Case 90: 135989011/247500000
Case 91: 27027027/100000000
Case 92: 7/16
Case 93: 134907143/166650000
Case 94: 577358491/900000000
Case 95: 473636843/999900000
Case 96: 153409091/500000000
Case 97: 99354839/330000000
Case 98: 21063253/249750000
Case 99: 105882353/180000000
Case 100: 321107143/499500000
Case 101: 68627451/100000000
Case 102: 193877551/500000000
Case 103: 27551021/900000000
Case 104: 181800001/999900000
Case 105: 22469663/999900000
Case 106: 182795699/200000000
Case 107: 88034483/333000000
Case 108: 1/16
Case 109: 77586207/250000000
Case 110: 979591837/1000000000
Case 111: 817346939/900000000
Case 112: 791587501/999900000
Case 113: 34076087/82500000
Case 114: 876900001/999000000
Case 115: 630989011/990000000
Case 116: 159090909/250000000
Case 117: 5882353/100000000
Case 118: 28/55
Case 119: 21627907/30000000
Case 120: 47826087/100000000
Case 121: 360000001/990000000
Case 122: 67346939/300000000
Case 123: 190909091/900000000
Case 124: 34076087/41250000
Case 125: 4/25
Case 126: 3/10
Case 127: 61/88
Case 128: 8690309/14062500
Case 129: 23505883/999000000
Case 130: 956250001/990000000
Case 131: 268421053/900000000
Case 132: 91826087/198000000
Case 133: 235931461/999900000
Case 134: 1/2
Case 135: 791587501/999900000
Case 136: 666666001/999999000
Case 137: 111111111/1000000000
Case 138: 366666667/900000000
Case 139: 14/15
Case 140: 73097561/83250000
Case 141: 666600001/999900000
Case 142: 21/32
Case 143: 166666501/999999000
Case 144: 543750001/900000000
Case 145: 29411762/249999975
Case 146: 2/5
Case 147: 64516129/250000000
Case 148: 666666667/1000000000
Case 149: 972710527/999000000
Case 150: 1/16
Case 151: 363636001/999999000
Case 152: 11/24
Case 153: 71357143/166500000
Case 154: 68782381/111110000
Case 155: 12/25
Case 156: 3/10
Case 157: 28947079/99999000
Case 158: 14423077/112500000
Case 159: 14285713/249999975
Case 160: 102339079/111111000
Case 161: 135882353/330000000
Case 162: 19/32
Case 163: 533076923/990000000
Case 164: 1/2
Case 165: 79167/100000
Case 166: 66601/99900
Case 167: 1/9
Case 168: 36667/90000
Case 169: 14/15
Case 170: 86927/99000
Case 171: 66661/99990
Case 172: 64969/99000
Case 173: 2/5
Case 174: 86897/90000
Case 175: 7/8
Case 176: 1954/12375
Case 177: 33333/100000
Case 178: 181/1665
Case 179: 34483/50000
Case 180: 16667/100000
Case 181: 20119/33300
Case 182: 653/5550
Case 183: 2/5
Case 184: 3871/15000
Case 185: 1579/5000
Case 186: 17/18
Case 187: 2/9
Case 188: 7801/9000
Case 189: 2/9
Case 190: 1/10
Case 191: 4667/10000
Case 192: 785/999
Case 193: 0/1
Case 194: 6001/9000
Case 195: 6301/9900
Case 196: 0/1
Case 197: 1153/4995
Case 198: 0/1
Case 199: 619/4950
Case 200: 0/1
Case 201: 677/1100
Case 202: 909/5000
Case 203: 2629/4995
Case 204: 2353/5000
Case 205: 667/9990
Case 206: 9001/9900
Case 207: 0/1
Case 208: 1/8
Case 209: 1/2
Case 210: 4/45
Case 211: 427/900
Case 212: 711/1000
Case 213: 43/66
Case 214: 53/250
Case 215: 147/250
Case 216: 193/300
Case 217: 29/990
Case 218: 7/18
Case 219: 7/110
Case 220: 181/990
Case 221: 17/330
Case 222: 407/500
Case 223: 311/500
Case 224: 19/300
Case 225: 111/250
Case 226: 479/500
Case 227: 161/198
Case 228: 287/900
Case 229: 181/200
Case 230: 1/2
Case 231: 79/100
Case 232: 67/100
Case 233: 11/100
Case 234: 37/90
Case 235: 14/15
Case 236: 8/9
Case 237: 67/100
Case 238: 33/50
Case 239: 2/5
Case 240: 73/90
Case 241: 63/100
Case 242: 32/45
Case 243: 27/100
Case 244: 7/10
Case 245: 27/100
Case 246: 7/18
Case 247: 29/50
Case 248: 2/5
Case 249: 44/45
Case 250: 601/900
Case 251: 3/4
Case 252: 1/2
Case 253: 5/6
Case 254: 9/10
Case 255: 0/1
Case 256: 8/11
Case 257: 79/250
Case 258: 17/18
Case 259: 2/9
Case 260: 859/990
Case 261: 2/9
Case 262: 353/1000
Case 263: 83/495
Case 264: 1/3
Case 265: 529/1000
Case 266: 353/500
Case 267: 1/10
Case 268: 467/1000
Case 269: 393/500
Case 270: 0/1
Case 271: 667/1000
Case 272: 191/300
Case 273: 0/1
Case 274: 229/990
Case 275: 0/1
Case 276: 62/495
Case 277: 129131/990000
Case 278: 13/18
Case 279: 292683/1000000
Case 280: 9/10
Case 281: 770833/1000000
Case 282: 144643/225000
Case 283: 14143/495000
Case 284: 17449/45000
Case 285: 1/16
Some Love Stories Live Forever ....
tuman
New poster
Posts: 24
Joined: Sat Oct 22, 2005 7:30 pm
Location: CUET
Contact:

332 , compiler error : atof() !!!!!!!!!!!!!!!

Post by tuman »

Plz tell me whats wrong with the function atof() in this problem.
I can use atof to convert a string into floating or double type vaiable. And it is supported by unix. But i m getting Compiler Error for this. :evil:

error message : implicit declaration of atof();

plz help me to overcome that problem
We the dreamer of the dreamy dream...
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

You should include <stdlib.h> (in C) or <cstdlib> (in C++). Alternatively you can use sscanf().
tuman
New poster
Posts: 24
Joined: Sat Oct 22, 2005 7:30 pm
Location: CUET
Contact:

Post by tuman »

Thanks little joey,

But atof() also works with stdio.h in C,

Anyway i get accepted. :lol:
We the dreamer of the dreamy dream...
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

Hmm. Well my version of gcc compiles and runs atof() with doubles, but produces some weird results if I don't include <stdlib.h>. If I do, everything is ok. But maybe some C-guru (mf, krzysztof, misof) can explain that.
Ankur Handa
New poster
Posts: 6
Joined: Mon May 15, 2006 11:34 am

332 WA ??

Post by Ankur Handa »

hi i am getting wrong answer in this question , can somebody tell me
where am i going wrong ?


here is the code ..

Code: Select all

#include<cstdio>
#include<iostream>
#include<cmath>
#include<string.h>
#include<stdlib.h>
using namespace std;
long long  Gcd(long long a , long long b)
{
        if(b==0)
                return a;
        else
                return Gcd(b,a%b);
}
int main(void)
{
        long long  num=0,k=0,gcd=0;
        double x,numerator,denom;
        char str[1000];
        int Cnt=1;
        while(1)
        {
                scanf("%lld",&num);
                if(num==-1)
                        break;
                scanf(" %s",str);
                sscanf(str,"%lf",&x);
                k = strlen(str)-2-num;

                double j = (double)(k+num);
                double m = (double)k;

                if(num!=0)
                {
                        long long numd = (long long)(pow(10.0,m)*x);
                        double numx = (double)numd;
                        numerator  = pow(10.0,j)*x - numx;
                        denom      = pow(10.0,j) - pow(10.0,m);
                }
                else
                {
                        numerator = pow(10.0,j)*x;
                        denom     = pow(10.0,j);
                }

                long long y = (long long)numerator;
                long long x = (long long)denom;

                gcd  =  Gcd(x,y);
                numerator/=(double)gcd;
                denom/=(double)gcd;

                printf("Case %d: %.lf/%.lf\n",Cnt,numerator,denom);
                Cnt++;
        }
}
Ankur Handa
New poster
Posts: 6
Joined: Mon May 15, 2006 11:34 am

Post by Ankur Handa »

the code is working for all of the test cases given here ..
Post Reply

Return to “Volume 3 (300-399)”