POJ还没做过的,今天偶然看到网易有道有个topcoder编程比赛,过几天就是入围赛了,赶紧报了个名,拿不到名次拿件T恤也好。
正好也自己练练手~今天抽点时间做了两道POJ的弱题,慢慢恢复手感ing
发现用C++还是比以前在学校时用纯C要好,STL很强大。
以后将长期A题玩~
1003 枚举,典型的先生成表,然后根据数据查表。产表O(N) 查表O(1)
题目:
How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We're assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 + 1/3 = 5/6 card lengths. In general you can make n cards overhang by 1/2 + 1/3 + 1/4 + ... + 1/(n + 1) card lengths, where the top card overhangs the second by 1/2, the second overhangs tha third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangs the table by 1/(n + 1). This is illustrated in the figure below.
Sample Input
1.00 3.71 0.04 5.19 0.00
Sample Output
3 card(s) 61 card(s) 1 card(s) 273 card(s)
代码
#include <iostream>
using namespace std;
int main()
{
float r[1000] = {0};
r[1] = 0.5;
for(int i=2;i<1000;i++)
{
float s = i;
float tmp = 1/(s+1);
r[i] = r[i-1] + tmp;
}
float n;
while(cin>>n)
{
if( n == 0)
break;
for(int i=0;i<999;i++)
{
if( r[i] < n && r[i+1] >=n )
{
cout<<i+1<<" card(s)"<<endl;
break;
}
}
}
return 0;
}
1006 枚举 完全就是小学数学题,O(N)硬举就过了:
人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。
每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。
因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同一天。
对于每个周期,我们会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一次高峰出现的时间)。
你的任务是给定一个从当年第一天开始数的天数,输出从给定时间开始(不包括给定时间)下一次三个高峰落在同一天
的时间(距给定时间的天数)。例如:给定时间为10,下次出现三个高峰同天的时间是12,则输出2(注意这里不是3)。
Sample Input
0 0 0 0 0 0 0 100 5 20 34 325 4 5 6 7 283 102 23 320 203 301 203 40 -1 -1 -1 -1
Sample Output
Case 1: the next triple peak occurs in 21252 days. Case 2: the next triple peak occurs in 21152 days. Case 3: the next triple peak occurs in 19575 days. Case 4: the next triple peak occurs in 16994 days. Case 5: the next triple peak occurs in 8910 days. Case 6: the next triple peak occurs in 10789 days.
#include<iostream>
#include<stdio.h>
#define MAXN 21252
using namespace std;
int main()
{
int a,b,c,d;
int count=1;
while( cin>>a>>b>>c>>d )
{
if( a == -1)
break;
int r = 1;
while( r<=MAXN )
{
if( (r+d-c)%33==0 &&((r+d-b)%28==0) &&((r+d-a)%23==0))
{
cout<<"Case "<<count<<": the next triple peak occurs in "<<r<<" days."<<endl;
break;
}
r++;
}
count++;
}
return 0;
}
1002 哈希表,用静态的内存不够(百万级),我这用的STL的map,根据KEY自动有序,太爽了,连排序都省了。
瓶颈在产生哈希表O(nlogn)——map实际结构是二叉树,生成的时候自动做了logn的排序。
电话号码的标准格式是七位十进制数,并在第三、第四位数字之间有一个连接符。
A, B, 和C 映射到 2
D, E, 和F 映射到 3
G, H, 和I 映射到 4
J, K, 和L 映射到 5
M, N, 和O 映射到 6
P, R, 和S 映射到 7
T, U, 和V 映射到 8
W, X, 和Y 映射到 9
Q和Z没有映射到任何数字,连字符不需要拨号,可以任意添加和删除。
如果两个号码有相同的标准格式,那么他们就是等同的(相同的拨号)
你的公司正在为本地的公司编写一个电话号码薄。作为质量控制的一部分,你想要检查是否有两个和多个公司拥有相同的电话号码。
Input
Output
No duplicates.
Sample Input
12 4873279 ITS-EASY 888-4567 3-10-10-10 888-GLOP TUT-GLOP 967-11-11 310-GINO F101010 888-1200 -4-8-7-3-2-7-9- 487-3279
Sample Output
310-1010 2 487-3279 4 888-4567 3
#include<iostream>
#include<string>
#include<map>
using namespace std;
#define MAXN 1000000
long prase( string k )
{
long r = MAXN;
long result = 0;
for(long i=0;i<k.length();i++)
{
if( k[i]>='0' && k[i]<='9')
{
long s = k[i] - '0';
result += s*r;
r = r/10;
}
if( k[i]>='A' && k[i]<='Y' )
{
long s;
if(k[i]=='Q')
continue;
if( k[i] == 'A' || k[i] == 'B' || k[i] == 'C' )
s=2;
if( k[i] == 'D' || k[i] == 'E' || k[i] == 'F' )
s=3;
if( k[i] == 'G' || k[i] == 'H' || k[i] == 'I' )
s=4;
if( k[i] == 'J' || k[i] == 'K' || k[i] == 'L' )
s=5;
if( k[i] == 'M' || k[i] == 'N' || k[i] == 'O' )
s=6;
if( k[i] == 'P' || k[i] == 'R' || k[i] == 'S' )
s=7;
if( k[i] == 'T' || k[i] == 'U' || k[i] == 'V' )
s=8;
if( k[i] == 'W' || k[i] == 'X' || k[i] == 'Y' )
s=9;
result += s*r;
r = r/10;
}
}
return result;
}
int main()
{
map<long,long> hash;
long n;
cin>>n;
for(long i=0;i<n;i++)
{
string tmp;
cin>>tmp;
long code = prase(tmp);
map<long,long>::iterator found = hash.find( code );
if ( found != hash.end() )
{
hash[code]++;
}
else
{
hash[code]=1;
}
}
bool flag = false;
for(map<long,long>::iterator iter = hash.begin(); iter != hash.end(); ++iter )
{
if( iter->second > 1)
{
long head = iter->first/10000;
long cut = iter->first%10000;
printf("%.3ld-%.4ld %ld/n",head,cut,iter->second);
flag = true;
}
}
if(!flag)
cout<<"No duplicates."<<endl;
return 0;
}
本文分享了POJ平台上三道题目的解题思路与代码实现,涉及枚举算法、哈希表应用及字符串解析等内容,展示了C++ STL的强大功能。
3025

被折叠的 条评论
为什么被折叠?



