| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 7185 人关注过本帖, 7 人收藏
标题:[原创]引导C++初学者走进ACM
只看楼主 加入收藏
一番宝瓶
Rank: 1
等 级:新手上路
帖 子:239
专家分:0
注 册:2007-7-14
收藏(7)
 问题点数:0 回复次数:21 
[原创]引导C++初学者走进ACM

*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 http://www.bc-cn.net
*/ 作者: 一番宝瓶
*/ 时间: 2007-7-20 编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------



见于我们C++ 板块里的初学者越来越多, 感觉很多人不管是计算机专业的还算非计算机专业都对C++都很感兴趣,由于学习方式和背景不同,大家的能力也分开了层次,希望一些熟悉C++的朋友能多帮助初学者多多领悟C++的真谛.废话少说,这个帖子主要是给一些初学者介绍一下有关ACM的东东,希望能通过简单的入门,让一些新手也可以领率到正规的编程比赛的快乐。

简介:

ACM/ICPC(ACM International Collegiate Programming Contest, 国际大学生程序设计竞赛)是由国际计算机界历史悠久、颇具权威性的组织ACM(Association for Computing Machinery,美国计算机协会)主办的,世界上公认的规模最大、水平最高的国际大学生程序设计竞赛,其目的旨在使大学生运用计算机来充分展示自己分析问题和解决问题的能力。该项竞赛从1970年举办至今已历29届,一直受到国际各知名大学的重视,并受到全世界各著名计算机公司的高度关注,在过去十几年中,APPLE、AT&T、MICROSOFT和IBM等世界著名信息企业分别担任了竞赛的赞助商。可以说,ACM国际大学生程序设计竞赛已成为世界各国大学生最具影响力的国际级计算机类的赛事, 是广大爱好计算机编程的大学生展示才华的舞台,是著名大学计算机教育成果的直接体现,是信息企业与世界顶尖计算机人才对话的最好机会。

官方网站: http://icpc.baylor.edu/icpc/



当然不要让ACM的知名度让一些初学者不敢去尝试,我们也无需参加什么比赛,只是拿这种正规的比赛题来练练手,引导自己对C++的学习。下面介绍下一些在线的ACM OJ(ACM在线评测系统),做做在线ACM,感觉还是不错的 ^^

UVA :http://acm.uva.es/contest/
西班牙的,号称世界第一OJ

POJ :http://acm.pku.edu.cn/JudgeOnline/contests
北京大学OJ,中国最大OJ

JOJ :http://acm.jlu.edu.cn/joj/contests.php
吉林大学OJ,题目比较简单

ZOJ :http://acm.zju.edu.cn/
浙江大学OJ,题目比较多

URAL :http://acm.timus.ru/schedule.aspx
俄罗斯的,数学味浓

SGU :http://acm.sgu.ru/contests.php
俄罗斯的,题目很少,但数学味很浓

USACO :http://ace.delos.com/contestgate
美国的,很多oier都在上面训练

SPOJ :https://www.spoj.pl/
排名方式独特,而且支持很多语言


建议有时间的朋友去国内的ACM 在线评测系统,亲切感比较强吧。再说说有关在线答题的东东,题目一般分为几种类型:DP,搜索,图论,模拟,数学,字符串处理,几何及其他等趣味题。选择了题目后,可以点submit提交你的代码,测试系统一般会回应一下几个答复:

Accepted //正确

Compiler Error //编译错误

Wrong Answer //结果错误

Presentation Error //表达格式错误

Runtime Error //运行错误

Time Limit Exceeded //超时

Memory Limit Exceeded //分配内存上限

Others (Output Too Much) //其他错误

除此之外还会给出你的程序代码的大小,运行所用时间和内存使用情况等信息。

题的内容全部为英文,但没有什么复杂的英语语法,看懂很容易,在线评测系统对输入输出的格式要求很严格,所以要特别注意input和output的要求,写完程序后用自己的IDE把题里所给的sample input测试一下结果,和给的sample output是否一致,以确保格式是否正确。


好了,相关的东西先介绍到这里,初学C++的朋友可以先做一些简单的题来接触一下ACM,下面我给了几个自己以前做过的一些题目,因为题目非常简单,希望初学者自己也能感悟一下,因为自己水平一般,还是希望一些高手不要吝啬自己的才华,把自己做过的题目与大家分享,或者帮助初学者分析,愿我们共同学好C++


一番宝瓶

2007.7.20 晨

[此贴子已经被作者于2007-7-20 11:37:45编辑过]

搜索更多相关主题的帖子: ACM 引导 
2007-07-20 11:16
一番宝瓶
Rank: 1
等 级:新手上路
帖 子:239
专家分:0
注 册:2007-7-14
收藏
得分:0 

Time Limit:3s Memory Limit:8192K In/Out:Stdin/stdout

Given a series of integer, find the maximum number.

Input
Multi-case. The first line gives count n (n < 100) of this array. n=0 means end of input. There are one integer in the next n lines.

Output
For each case, output the maximum number in one line.

Sample Input
3
1 4 5
0
Sample Output
5


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

int main()
{
int n;
cin>>n;
while (n != 0)
{
if (n >= 100) exit(-1);
int *a = new int[n];
for (int i=0; i<n; ++i)
cin>>a[i];
cout<<*max_element(a,a+n)<<endl;
delete []a;
cin>>n;
}
return 0;
}


Code:304B Time(s):0.00 Mem(k):804/1632


2.3.2.2.2.0
2007-07-20 11:18
一番宝瓶
Rank: 1
等 级:新手上路
帖 子:239
专家分:0
注 册:2007-7-14
收藏
得分:0 

Time Limit:3s Memory Limit:8192K In/Out:Stdin/stdout


In this problem you are to generate a triangular wave form according to a specified pair of Amplitude and Frequency.

Input and Output
The input will contain two integers, each on a separate line. The first integer is the Amplitude; the second integer is the Frequency.


For the output of your program, you will be printing wave forms each separated by a blank line. The total number of wave forms equals the Frequency, and the horizontal ``height'' of each wave equals the Amplitude. The Amplitude will never be greater than nine.


The waveform itself should be filled with integers on each line which indicate the ``height'' of that line.

NOTE: There is a blank line after each separate waveform, excluding the last one.

Sample Input

3
2

Sample Output

1
22
333
22
1

1
22
333
22
1


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

int main()
{
int n, m;
cin>>n>>m;
while(m-->0)
{
for(int i=-n+1; i<n; ++i, cout<<endl)
for(int k=0; k<n-abs(i); cout<<n-abs(i), k++);
if( m>0 ) cout<<endl;
}
return 0;
}

Code:276B Time(s):0.00 Mem(k):804/1004


2.3.2.2.2.0
2007-07-20 11:19
一番宝瓶
Rank: 1
等 级:新手上路
帖 子:239
专家分:0
注 册:2007-7-14
收藏
得分:0 

Time Limit:3s Memory Limit:16384K In/Out:Stdin/stdout


Given a positive integer, tell whether it is equal to the sum of four consecutive integers.

Input
A positive integer(<231), each case will be on a separate line. A zero (0) denotes the end of input.

Output
The phrase “ is not the sum of four consecutive integers.” or the phrase “ is the sum of four consecutive integers.”, where is the number in question.

Example
In this example, 2 = -1 + 0 + 1 + 2, and 82 = 19 + 20 + 21 + 22. 5 and 41 are not of the correct form.

Input
2
5
41
82
0

Output

2 is the sum of four consecutive integers.
5 is not the sum of four consecutive integers.
41 is not the sum of four consecutive integers.
82 is the sum of four consecutive integers.


程序代码:

#include <iostream>
using namespace std;

inline bool sure(int n);

int main()
{
int i, n, a[40];
for(i =0, n=0;; ++i, ++n)
{
cin>>a[i];
if( a[i] == 0) break;
}

for(i=0; i<n; ++i)
{ if(sure(a[i])) cout<<a[i]<<\" is the sum of four consecutive integers.\n\";
else cout<<a[i]<<\" is not the sum of four consecutive integers.\n\";
}

return 0;
}

inline bool sure(int n)
{
int sum = 0, num = 0;
for(int i=-n; i<n+4; ++i)
{
if( n != sum )
{
num++;
if(num-->4) sum+=4;
else sum+=i;
}
else
return true;
}

return false;
}

Code:636B Time(s):0.00 Mem(k):804/1020



Hjin 分析后的版本2

#include <iostream>
#include <vector>
using namespace std;

int main()
{
int num;
vector<int> a;
vector<int>::iterator iter;
while(cin>>num)
{
if(num == 0) break;
a.push_back(num);
}
for(iter = a.begin(); iter != a.end(); ++iter)
{ if((*iter - 6) % 4 == 0) cout<<*iter<<" is the sum of four consecutive integers.\n";
else cout<<*iter<<" is not the sum of four consecutive integers.\n";
}

return 0;
}

[此贴子已经被作者于2007-7-21 17:31:28编辑过]


2.3.2.2.2.0
2007-07-20 11:20
一番宝瓶
Rank: 1
等 级:新手上路
帖 子:239
专家分:0
注 册:2007-7-14
收藏
得分:0 

Time Limit:3s Memory Limit:8192K In/Out:Stdin/stdout

Zorro is ready to modernize -- he is tired of hand drawing his giant "Z", and would like to add an educational element. So he wants you to write a program to draw a Z using the lower-case letters of the alphabet in order. If you run out of letters, just continue by following z with a.

Input
A positive integer(<=500) denoting the number of characters across the top of the Z. An input of 0 will indicate that Zorro is done.

Output
The Z, drawn in lowercase alphabetic characters. Each Z should be separated from the previous Z by at least one blank line.

Sample Input
3
30
0

Sample Output

abc
d //d在中间,格式没弄好
efg

abcdefghijklmnopqrstuvwxyzabcd
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
a
b
c
d
e
f
ghijklmnopqrstuvwxyzabcdefghij
Hint:
Blank line is used between two Zorros, so there are no extra blank line after last Zorro.

程序代码:

#include <iostream>
#include <string>

using namespace std;
string alpha(\"abcdefghijklmnopqrstuvwxyz\");

void printZ(int n);

int main()
{
int i, n, a[10];
for(i=0, n=0;; ++i, ++n)
{
cin>>a[i];
if(a[i]==0) break;
if(a[i] > 500) exit(-1);
}
for(i=0; i<n; ++i)
{ if(i>0) cout<<endl;
printZ(a[i]);
}
return 0;
}

void printZ(int n)
{
int i, num=1;
for(i=0; num <= n; ++i)
{
if(i==26) i=0;
cout<<alpha[i];
num+=1;
}
cout<<endl;
for(int j=0; j<n-2; ++j, cout<<endl)
{
if(i==26) i=0;
for(int k=0; k<n-j-2; cout<<' ', ++k);
cout<<alpha[i++];
}
for(num=1; num <= n; num++)
{
if(i==26) i=0;
cout<<alpha[i++];
}
cout<<endl;

}

Code:709B Time(s):0.02 Mem(k):804/1356

[此贴子已经被作者于2007-7-20 11:35:19编辑过]


2.3.2.2.2.0
2007-07-20 11:21
一番宝瓶
Rank: 1
等 级:新手上路
帖 子:239
专家分:0
注 册:2007-7-14
收藏
得分:0 

Time Limit:3s Memory Limit:16384K In/Out:Stdin/stdout

Draw a diamond shape with character '*'.

Input
There are several lines in the input, each line is a integer N(N<100), N is zero means the end of input.

Output
For each line of input, draw a diamond whose width is N like sample. Notice there is no unnecessary spaces after '*' in the same line.

Output a blank line after each case.

Sample Input

3
0
Sample Output

  *
 ***
*****
 ***
  *

程序代码:
#include <iostream>
#include <cmath>
using namespace std;

inline void printch(int n);

int main()
{
int n, a[1000], i=0;
while(cin>>n)
{
if(n == 0) break;
a[i++] = n;
}
for(int j=0; j<i; cout<<endl, ++j) printch(a[j]);
return 0;

}

inline void printch(int n)
{
for(int i=-n+1; i<n; ++i, cout<<endl)
{
for(int j=0; j<abs(i); cout<<' ', ++j);
for(int k=0; k<2*n-2*abs(i)-1; cout<<'*', k++);
}
}

Code:441B Time(s):0.15 Mem(k):804/1936

[此贴子已经被野比于2007-7-21 0:23:26编辑过]


2.3.2.2.2.0
2007-07-20 11:22
一番宝瓶
Rank: 1
等 级:新手上路
帖 子:239
专家分:0
注 册:2007-7-14
收藏
得分:0 

Time Limit:3s Memory Limit:8192K In/Out:Stdin/stdout

Consider a scenario where you are asked to calculate a function Answer(x, y), with x and y both integers in the range [1, N], 1 <= N <= 50000. If you know Answer(x, y), then you can easily derive Answer(k*x, k*y) for any integer k. In this situation you want to know how many values of Answer(x, y) you need to precalculate. The function Answer is not symmetric.

For example, if N = 4, you need to precalculate 11 values: Answer(1, 1), Answer(1, 2), Answer(2, 1), Answer(1, 3), Answer(2, 3), Answer(3, 2), Answer(3, 1), Answer(1, 4), Answer(3, 4), Answer(4, 3) and Answer(4, 1).

Input
Each line will has a positive integer N.

Output
Only one integer that is how many values of Answer(x, y) you need to precalculate.

Sample Input
4
Sample Output
11

程序代码:

#include <iostream>
using namespace std;
#define min(x, y) ((x)>(y)?(x):(y))

bool gcm(int a, int b);

int main()
{
int N;
while (cin>>N)
{
int count = 0;
if (N<1 || N>50000) exit(-1);

for (int i=1; i<=N; ++i)
{
for (int j=1;j<=N; ++j)
{
if( gcm(i, j) ) continue;
else count++;
}
}

cout<<count++<<endl;
}
return 0;
}

bool gcm(int a, int b)
{
for (int i=2; i<=min(a,b); ++i)
if ( a%i==0 && b%i==0 ) return true;
return false;
}

Code:552B Time(s):0.00 Mem(k):804/1096


2.3.2.2.2.0
2007-07-20 11:26
一番宝瓶
Rank: 1
等 级:新手上路
帖 子:239
专家分:0
注 册:2007-7-14
收藏
得分:0 

Time Limit:3s Memory Limit:8192K In/Out:Stdin/stdout

Given a character string, determine if it is a palindrome. A palindrome is a word or phrase that reads the same forwards and backwards, like mom or noon. For our purposes, palindromes are case insensitive, so Bob and Anna are palindromes. Also, we are only concerned with alphabetic characters, so punctuation and other non-alphabetic characters may be ignored.

Input
A positive integer denoting the number of cases. Each case will consist of a string of characters, including blanks and non-alphabetic characters. Each case will be on a separate line.

Output
The sentence “<string> is a palindrome.” if the word is a palindrome, or the sentence “<string> is not a palindrome.” if it is not, where <string> is the input string.

Sample Input
5
Allen Alda
asdffdsa
Race car
Go hang a salami, I'm a lasagna hog!
Hanna

Sample Output

Allen Alda is not a palindrome.
asdffdsa is a palindrome.
Race car is a palindrome.
Go hang a salami, I'm a lasagna hog! is a palindrome.
Hanna is not a palindrome.


程序代码:
 
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
int n, count=0;
cin>>n;
cin.clear(ios::goodbit);
string *s = new string[n];
for(int i=0; i<n; ++i) getline(cin,s[i]);
for(int j=0; j<n; ++j, count=0)

{
string temp=\"\";
for(int k=0; k<s[j].size(); ++k)
{
if(isalpha(s[j].at(k)))
temp+=tolower(s[j].at(k));
}

for(int m=0; m<temp.size()/2; ++m)
( temp[m]==temp[temp.size()-m-1] ) ? count++ : count--;

if(count==temp.size()/2 || temp.size() == 1) cout<<s[j]<<\" is a palindrome\n\";
else cout<<s[j]<<\" is not a palindrome\n\";

}
delete []s;
return 0;
}

Code:488B Time(s):0.00 Mem(k):812/2316

[此贴子已经被作者于2007-7-20 12:38:19编辑过]


2.3.2.2.2.0
2007-07-20 11:27
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 

支持一个,继续发扬!


Fight  to win  or  die...
2007-07-20 12:35
Jackzdong
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2006-6-1
收藏
得分:0 

就是不知道为什么别人的CODE用的内存开销那么小, 我的有点大, 有时候把他们的程序COPY过去, 然后提交得到的内存开销也不是一样的

2007-07-20 15:40
快速回复:[原创]引导C++初学者走进ACM
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023042 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved