《算法竞赛-训练指南》第一章-1.17_UVa 11462
2013-07-20 09:04
281 查看
我就说,这本书上都是些经典的题目,怎么可能出那么简单的题目,应该是有他目的的。
果然,这道题目,用系统函数的sort能过,但是572ms,用我写的hash也能过476ms。
但是如果用书本上的解题技巧,那就是快速的过了,162ms。这差距的时间还是蛮大的!
数据量一般达到百万之后用这个快速的输入输出函数就非常的快了。不错,还是长见识了。
这道题目的代码到是没有什么意思,我把我的输入输入函数贴出来吧,可以解决正负数的问题。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
using namespace std;
int hash[105];
int buf[10];
int readInt()
{
char c = getchar();
int flag = 0;
while (!isdigit(c))
{
if (c == '-')
{
flag = 1;
}
c = getchar();
}
int x = 0;
while (isdigit(c))
{
x = x * 10 + c - '0';
c = getchar();
}
if (flag == 0)
{
return x;
}
else
{
return -1 * x;
}
}
void writeInt(int x)
{
int p = 0;
int flag = 0;
if (x < 0)
{
flag = 1;
}
if (x == 0)
{
p++;
}
while (x)
{
buf[p++] = x % 10;
x /= 10;
}
if (flag == 0)
{
for (int i = p - 1; i >= 0; i--)
{
putchar('0' + buf[i]);
}
}
else
{
putschar('-');
for (int i = p - 1; i >= 0; i--)
{
putchar('0' + buf[i]);
}
}
}
int main()
{
int N;
while (N = readInt())
{
memset(hash, 0, sizeof(hash));
for (int i = 0; i < N; i++)
{
hash[readInt()]++;
}
int first = 1;
for (int i = 1; i <= 100; i++)
{
while (hash[i])
{
if (!first)
{
putchar(' ');
}
first = 0;
writeInt(i);
hash[i]--;
}
}
puts("");
}
// system("pause");
return 0;
}
果然,这道题目,用系统函数的sort能过,但是572ms,用我写的hash也能过476ms。
但是如果用书本上的解题技巧,那就是快速的过了,162ms。这差距的时间还是蛮大的!
数据量一般达到百万之后用这个快速的输入输出函数就非常的快了。不错,还是长见识了。
这道题目的代码到是没有什么意思,我把我的输入输入函数贴出来吧,可以解决正负数的问题。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
using namespace std;
int hash[105];
int buf[10];
int readInt()
{
char c = getchar();
int flag = 0;
while (!isdigit(c))
{
if (c == '-')
{
flag = 1;
}
c = getchar();
}
int x = 0;
while (isdigit(c))
{
x = x * 10 + c - '0';
c = getchar();
}
if (flag == 0)
{
return x;
}
else
{
return -1 * x;
}
}
void writeInt(int x)
{
int p = 0;
int flag = 0;
if (x < 0)
{
flag = 1;
}
if (x == 0)
{
p++;
}
while (x)
{
buf[p++] = x % 10;
x /= 10;
}
if (flag == 0)
{
for (int i = p - 1; i >= 0; i--)
{
putchar('0' + buf[i]);
}
}
else
{
putschar('-');
for (int i = p - 1; i >= 0; i--)
{
putchar('0' + buf[i]);
}
}
}
int main()
{
int N;
while (N = readInt())
{
memset(hash, 0, sizeof(hash));
for (int i = 0; i < N; i++)
{
hash[readInt()]++;
}
int first = 1;
for (int i = 1; i <= 100; i++)
{
while (hash[i])
{
if (!first)
{
putchar(' ');
}
first = 0;
writeInt(i);
hash[i]--;
}
}
puts("");
}
// system("pause");
return 0;
}
相关文章推荐
- 《算法竞赛-训练指南》第一章-1.11——Uva 10795
- 《算法竞赛-训练指南》第一章-1.5_UVa 10881
- 《算法竞赛-训练指南》第一章-1.28_UVa 10891
- 《算法竞赛-训练指南》第一章-1.9——UVa11210
- 《算法竞赛-训练指南》第一章-1.19_UVa 11549
- 《算法竞赛-训练指南》第一章-1.19_UVa 11549
- 《算法竞赛-训练指南》第一章-1.27-UVa 10635
- 《算法竞赛-训练指南》第一章-1.7——UVa 11464
- 《算法竞赛-训练指南》-第一章-1.10_UVa 11384
- 《算法竞赛-训练指南》第一章-1.24-UVa 10755
- 《算法竞赛-训练指南》-第二章-2.16_UVa 11427
- 《算法竞赛-训练指南》第一章-1.23_LA 3695
- UVa 11426--算法竞赛入门经典训练指南
- 《算法竞赛-训练指南》-第二章-2.18_UVa 10294
- 《算法竞赛-训练指南》第一章-1.22_LA 3209
- 《算法竞赛-训练指南》第二章-2.15_UVa 11722
- 《算法竞赛-训练指南》第一章-1.4_LA 3708
- 《算法竞赛-训练指南》第二章-2.5_UVa 11361
- 《算法竞赛-训练指南》第二章-2.2_UVa 11401
- 《算法竞赛-训练指南》第一章-1.16_LA 3177