443 - Humble Numbers

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

Moderator: Board moderators

mosaick2
New poster
Posts: 21
Joined: Wed Mar 08, 2006 4:05 am

I can just give a tips.

Post by mosaick2 » Mon Aug 28, 2006 3:22 am

If your answer is correct, but you had given a TLE from judge.
You can use a kind of technic that store your humble numbers in memory. That means precalculate the humble numbers(it's just total 5842).

I hope it helps you.

ps. anyway, your answer printing is wrong. you have to consider about the numbers like 11th, 12th, 13th, 111th, ... and so on.

aswin34
New poster
Posts: 4
Joined: Tue Nov 07, 2006 8:32 am

443 WA

Post by aswin34 » Tue Nov 07, 2006 8:39 am

#include <stdio.h>

int main() {
int i, j, num;
long temp;
long humble[5483];
char suffix[2];
suffix[0] = 't';
suffix[1] = 'h';
humble[0] = 1;

for(i = 1; i < 5483; i++)
{
humble[i] = 2100000000;

for(j = 0; j < i; j++)
{
if(humble[j] * 2 > humble[i - 1])
{
if(humble[j] * 2 < humble[i])
{
humble[i] = humble[j] * 2;
}
}
else if(humble[j] * 3 > humble[i - 1])
{
if(humble[j] * 3 < humble[i])
{
humble[i] = humble[j] * 3;
}
}
else if(humble[j] * 5 > humble[i - 1])
{
if(humble[j] * 5 < humble[i])
{
humble[i] = humble[j] * 5;
}
}
else if(humble[j] * 7 > humble[i - 1])
{
if(humble[j] * 7 < humble[i])
{
humble[i] = humble[j] * 7;
}
}
}
}

scanf("%d", &num);

while( num!=0)
{
if( num%10 == 1 && num%100== 11) { suffix[0] = 's'; suffix[1] = 't';}
else if( num%10 == 2 && num%100 == 12) { suffix[0] = 'n'; suffix[1] = 'd';}
else if( num%10 == 3 && num%100 == 13) { suffix[0] = 'r'; suffix[1] = 'd';}
else {suffix[0]='t';suffix[1]='h';}
temp = humble[num - 1];

printf("The %d%s humble number is %ld.\n", num, suffix, humble[num-1]);
scanf("%d", &num);
}
return 0;
}

aswin34
New poster
Posts: 4
Joined: Tue Nov 07, 2006 8:32 am

Post by aswin34 » Tue Nov 07, 2006 8:40 am

can someone let me know whats wrong with this?

User avatar
rio
A great helper
Posts: 385
Joined: Thu Sep 21, 2006 5:01 pm
Location: Kyoto, Japan

Post by rio » Tue Nov 07, 2006 10:41 am

May be the suffix chosing code is wrong.

I got AC with this code. It may help you.

Code: Select all

if ((i%100 - i%10) == 10)
      suf = "th";
else
         switch ( i%10 ) {
         case 1: suf = "st"; break;
         case 2: suf = "nd"; break;
         case 3: suf = "rd"; break;
         default: suf = "th"; break;
         }

Nakar
New poster
Posts: 16
Joined: Tue Apr 17, 2007 6:37 pm

Post by Nakar » Tue May 01, 2007 2:36 am

Somebody can compile my code

I compile (in Windows, with Bloodshed Dev-C++, and option -ansi -pedantic -Wall) and I dont find none warning or error.

Thanks

My code (CE)

Code: Select all

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

...CODE REMOVE

             if (n==11 || n==12 || n==13)
                cout << "th";

...CODE REMOVE

Thanks to "Jan" and "mf".
Last edited by Nakar on Wed May 02, 2007 9:27 pm, edited 3 times in total.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf » Tue May 01, 2007 3:09 am

I think you should include <algorithm> header to use min().

Nakar
New poster
Posts: 16
Joined: Tue Apr 17, 2007 6:37 pm

Post by Nakar » Tue May 01, 2007 11:48 am

Thanks.

But, now is WA.

I think the output is ok.

Please, somebodey can check me code.

Thanks.

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

Post by Jan » Wed May 02, 2007 8:15 pm

If there are 1st, 2nd, 3rd, you can't say 1th, 2th... :D
Ami ekhono shopno dekhi...
HomePage

Nakar
New poster
Posts: 16
Joined: Tue Apr 17, 2007 6:37 pm

Post by Nakar » Wed May 02, 2007 8:36 pm

My INPUT:

Code: Select all

1
2
3
4
11
12
13
21
22
23
100
1000
2500
5842
0

My OUTPUT:

Code: Select all

The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 2500th humble number is 20321280.
The 5842nd humble number is 2000000000.
Where is the problem?

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

Post by Jan » Wed May 02, 2007 9:13 pm

Ok, let me be more clear. What about 111, 512, ....
Ami ekhono shopno dekhi...
HomePage

Nakar
New poster
Posts: 16
Joined: Tue Apr 17, 2007 6:37 pm

Post by Nakar » Wed May 02, 2007 9:25 pm

Thank you so much!

How can we do whitout you?

Thank you again.

mirage
New poster
Posts: 11
Joined: Sat Jan 19, 2008 9:37 pm

443 Humble Numbers ....Getting a Wrong Answer...

Post by mirage » Tue Jan 29, 2008 6:03 pm

Hi friends,
I seem to be getting a wrong answer....even though my code is workin for all the test cases i ever tried on it.....
Can somebody help me out n tell wat's the error..

here's the code
Last edited by mirage on Wed Jan 30, 2008 11:35 am, edited 1 time in total.

mpi
New poster
Posts: 46
Joined: Fri Nov 03, 2006 7:53 pm
Location: Madrid

Post by mpi » Wed Jan 30, 2008 10:14 am

You are not printing ordinals numbers in the right way. For instance, the correct output for 111 should be:
The 111th humble number is 576.
Your output:
The 111st humble number is 576.
Got the idea?

mirage
New poster
Posts: 11
Joined: Sat Jan 19, 2008 9:37 pm

Post by mirage » Wed Jan 30, 2008 10:38 am

Thanx....
i got accepted now....
that line was intended to be
(p%100<=20 && p%100>=10)...
but i missed it out in the final code.....
thanx again for findin it out.....

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

why this code is not working???

Post by abid_iut » Sun Nov 16, 2008 7:44 pm

i have done it as it is suggested
may be the array size is too large but i dont know what should be the size to store the humble number
please help
and if there is a better idea of getting humble number using array pls give me that idea
my code is:

Code: Select all

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

#define limit 5843
#define MAX 2000000000

long humblenum[MAX];

int main()
{
	int n;	
	int i,j,k,l,pos=0;
	for(i=0;i<limit;i++){
		for(j=0;j<limit;j++){
			for(k=0;k<limit;k++){
				for(l=0;l<limit;l++){
					humblenum[pos]=(pow(2,i))*(pow(3,j))*(pow(5,k))*(pow(7,l));
					pos++;
				}
			}
		}
	}
	sort(humblenum,humblenum+pos);
	while(scanf("%d",&n)){
		if(n==0)break;
		if((n%100)/10==1)printf("The %dth humble number is %ld.\n",n,humblenum[n]);
		else {
			switch(n%10){
			case 0:   case 4:   case 5:   case 6:   case 7:   case 8:   case 9:
				printf("The %dth humble number is %ld.\n",n,humblenum[n]);
				break;
			case 1:
				printf("The %dst humble number is %ld.\n",n,humblenum[n]);
				break;
			case 2:
				printf("The %dnd humble number is %ld.\n",n,humblenum[n]);
				break;
			case 3:
				printf("The %drd humble number is %ld.\n",n,humblenum[n]);
				break;
			}
		}
	}
	return 0;
}



please help somebody. :(
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

Post Reply

Return to “Volume 4 (400-499)”