您的位置:首页 > 其它

POJ 2247 解题报告

2015-04-16 07:45 253 查看
这道题和1338非常类似。这里用的是http://poj.org/showmessage?message_id=113667里的方法。 注意后缀:11, 113等用的是th,而不是st。12, 212等同理。

thestoryofsnow2247Accepted156K32MSC++1249B
/*
ID: thestor1
LANG: C++
TASK: poj2247
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;

const int MAXN = 5842;

int main()
{
int primes[4] = {2, 3, 5, 7};
int nums[MAXN];
nums[0] = 1;
int ptr[4] = {0, 0, 0, 0};

for (int i = 1; i < MAXN; ++i)
{
int num = nums[ptr[0]] * primes[0];
for (int j = 1; j < 4; ++j)
{
num = min(num, nums[ptr[j]] * primes[j]);
}

nums[i] = num;

for (int j = 0; j < 4; ++j)
{
if (nums[ptr[j]] * primes[j] == num)
{
ptr[j]++;
}
}
}

// for (int i = 0; i < 20; ++i)
// {
// 	printf("%d\t", nums[i]);
// }
// printf("\n");

int n;
char suffix[3][3] = {"st", "nd", "rd"};
while (scanf("%d", &n) && n != 0)
{
printf("The %d", n);
int d = n % 10;
if (d == 0 || d > 3 || (10 <= (n % 100) && (n % 100) <= 19))
{
printf("th");
}
else
{
printf("%s", suffix[d - 1]);
}
printf(" humble number is %d.\n", nums[n - 1]);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: