147 - Dollars

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

Moderator: Board moderators

raphaeljpb
New poster
Posts: 1
Joined: Mon Apr 02, 2007 10:36 pm

I'm in trouble

Post by raphaeljpb »

Hi, this is my first thread. I can't recognize what's problem below. The code shows correct results at least $50, but I don't know the problem for bigger amounts.
Thanks in advance.

#include <stdio.h>
#include <stdlib.h>

#define SIZE 6001

int main() {

float quantidadeReal;
int quantidadeInteira = 0;
int tipos[] = { 1,2,4,10,20,40,100,200,400,1000, 2000 };
int i, j, numeroDeTipos = 11, c;
double * contagem;


while (1) {
scanf("%f", &quantidadeReal);
if (!quantidadeReal) break;
contagem = calloc(SIZE, sizeof(double));

quantidadeInteira = (int) (quantidadeReal * 100);
quantidadeInteira /= 5;
contagem[0] = 1.0;
for(i = 0; i < numeroDeTipos; i++) {
c = tipos;
for (j = c ; j <= quantidadeInteira; j++) {
contagem[j] += contagem[j-c];
}
}
printf("%7.2f %.0lf\n", quantidadeReal, contagem[quantidadeInteira]);

free(contagem);
}


return 0;
}
gradientcurl
New poster
Posts: 16
Joined: Mon Jun 26, 2006 9:33 am
Contact:

WA: Any test cases

Post by gradientcurl »

so far, never get to see any posted test cases. Here is a test output which I'm sure I've got it wrong somewhere:

Code: Select all

300.00  181490736388615
299.95  181000196059736
299.90  180510857366624
299.85  180510857366624
299.80  180510857366624
299.75  180022694940816
299.70  179535708782312
299.65  179535708782312
299.60  179535708782312
299.55  179049898891112
299.50  179049898891112
299.45  178565265267216
299.40  178081807910624
299.35  178081807910624
299.30  178081807910624
299.25  177599526821336
299.20  177118397097689
299.15  177118397097689
299.10  177118397097689
below is my code

Code: Select all

removed after AC
Last edited by gradientcurl on Thu Jun 07, 2007 1:24 pm, edited 1 time in total.
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Check the set.

Set:

Code: Select all

  0.11                0
  0.12                0
  0.13                0
Hope it helps.
Ami ekhono shopno dekhi...
HomePage
gradientcurl
New poster
Posts: 16
Joined: Mon Jun 26, 2006 9:33 am
Contact:

tks jan

Post by gradientcurl »

tks jan. didnt take the double to integer conversion seriously.
shankar
New poster
Posts: 2
Joined: Thu Jun 07, 2007 9:31 am
Contact:

help 147WA... i cant find the mistake

Post by shankar »

i dont no whether my algorithm is right. for higher numbers it goes wrong i guess. so can some one helpme.
thanks

Code: Select all


#include<iostream>
#include<cstdio>
using namespace std;
unsigned long long a[6001][11]={0};int limit;
void dollar()
{int i,j,d[11]={1,2,4,10,20,40,100,200,400,1000,2000};




	for(i=0;i<11;a[0][i]=a[1][i]=1,++i);
	
	for(i=2;i<=6000;i++)
	{ 	a[i][0]=1;
		for(j=1;j<11;j++)
		{
			if(i<d[j])
			{
				a[i][j]=a[i][j-1];			
			}
			else
			{unsigned long long l=0;
				while(l<=i)
				{	
					a[i][j]+=a[i-l][j-1];l+=d[j];
				}
			}
		}
	}

}
int main()	
{float g;int f,i,p,w;
	limit=1;
	for(i=0;i<6001;i++)
		for(f=0;f<11;a[i][f++]=0);
	dollar();

	while(1)
	{	scanf("%f",&g);
		f=(int)(g/0.05);
		
		
		printf("%d ",g);
		if(f==0)
			return 0;
		
		
		cout<<a[f][10]<<endl;
//		printf("%17ll\n",a[f][0]);
	}
}

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

Post by Jan »

Check the set.

Set:

Code: Select all

  0.70               24
  0.90               39
  0.11                0
  0.12                0
  0.13                0
  0.14                0
And your presentation is wrong, too.
Ami ekhono shopno dekhi...
HomePage
Itachi-Forsaken
New poster
Posts: 10
Joined: Mon May 07, 2007 4:45 am

Post by Itachi-Forsaken »

I have a question regarding the output
Since the output is tabbed, I assume you have to use printf (for C/C++)
So what is the symbol after % for long long? Like 'd' is for integer

Also is there a way to properly convert a float to an integer? Or do you have to use a string and analyze it..

Thanks in advance
Last edited by Itachi-Forsaken on Thu Jun 28, 2007 2:22 pm, edited 1 time in total.
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

'%lld' for long long.
Ami ekhono shopno dekhi...
HomePage
andysoft
Experienced poster
Posts: 109
Joined: Sat Jun 23, 2007 9:53 pm
Location: Brest, BELARUS
Contact:

Post by andysoft »

Hello!
I am solving this prob and my algo works ok (at least i think so), but I cannot output large numbers. I used and use unsigned long long type, and printf("%lld\n",ans), but values for 300.00 input are either negative (-1600..00) or too small (like 2500000). Now I threw out the printf and used cout. I hope you may help me.

Code: Select all

#include <stdio.h>
#include <math.h>
#include <iostream>
int main(int argc, char* argv[]) {
	unsigned long long a[6001];
	int d[11] = {1,2,4,10,20,40,100,200,400,1000,2000};
	float x;
	int i,j,v,c;

	for (i=1; i<=v; i++)
		a[i] = 0;
	a[0] = (unsigned long long)1;

	for (i=0; i<11; i++) {
		c = d[i];
		for (j=c; j<=6000; j++)
			a[j] += a[j-c];
	}

	while (1) {
		scanf("%f",&x);
		if (x==0.0) break;
		v = (int)floor (x*20 + 0.1);
		printf ("%6.2f",x);
		std::cout.width(17);
		std::cout << a[v] << std::endl;
	}

	return 0;
}
EDIT: Debugger watches prove that my algo's right. Ans for 300.00 in watch is 181490736388615, but how to output it??
Now I lay me down to sleep...
my profile
andysoft
Experienced poster
Posts: 109
Joined: Sat Jun 23, 2007 9:53 pm
Location: Brest, BELARUS
Contact:

Post by andysoft »

Hi again!

1) Well, I re-wrote it in Pascal in just 2 minutes and got AC from the first time, BUT I still want to know the answer to my question about C/C++ output.
2) My prog ran during 0.346 seconds and I am 1383 / 1763 in the ranking. I would like to know how can I improve my code, if its structure is like this:

Code: Select all

1. Precompute all values from 1 to 6000 with usual for-loop: for each coin cost C do a[i] += a[i-C], a - 1..60000.
2. Read the input real number, multiply it by 25 and write the needed value from array a.
3. Types are: integer (int) everywhere except array a. It is int64 (long long)
Now I lay me down to sleep...
my profile
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind »

All coin coin value is multiple of 5c
so just divide all coins by 5
and coins will be {2000c,1000c,400c,200c,100c,40c,20c,10c,4c,2c,1c}

Also divide your input by 5c

so you will require array of size 6000 only

thanks
andysoft
Experienced poster
Posts: 109
Joined: Sat Jun 23, 2007 9:53 pm
Location: Brest, BELARUS
Contact:

Post by andysoft »

Yes, I have made it: divided everything needed by five.
Now I lay me down to sleep...
my profile
Kallol
Learning poster
Posts: 100
Joined: Sun Nov 13, 2005 8:56 am

Post by Kallol »

where am I wrong ??

here is my code :

Code: Select all

removed after AC

I checked almost all the input I got , I am getting WA . Is my output style wrong ??
Last edited by Kallol on Tue Aug 14, 2007 8:53 pm, edited 1 time in total.
Syed Ishtiaque Ahmed Kallol
CSE,BUET
Bangladesh
mmonish
Experienced poster
Posts: 109
Joined: Sun Mar 11, 2007 2:55 pm
Location: SUST

Post by mmonish »

>>Kallol
Try this case

Code: Select all

Input
5.10
Output
  5.10             6622
Try to avoid double calculation.It gives u WA.
Hope it helps.
Kallol
Learning poster
Posts: 100
Joined: Sun Nov 13, 2005 8:56 am

Post by Kallol »

I am really very much astonished with the behaviour of double in C.

double x = 5.10

x*=100;

printf("%0.lf",x);

and u will get 5.09 !!!


this is just horrible ...I thought , this floating point error is not there in double precision which was in the float data type. But it exists even in Long double .

Neway thanks for helping me with that tricky input. How could u guess and generate that input anyway ?? double gave me correcty answer in almost every othe cases :-?
Syed Ishtiaque Ahmed Kallol
CSE,BUET
Bangladesh
Post Reply

Return to “Volume 1 (100-199)”