2009 机试 ........................................................................................................................................... 2
计算和的数位 ............................................................................................................................ 2
大写改小写................................................................................................................................ 3
素数对........................................................................................................................................ 4
求最大公约数和最小公倍数 ....................................................................................................6
排序后求位置处的数................................................................................................................ 7
*路由器连接 .............................................................................................................................. 8
*编译原理 ................................................................................................................................ 10
*分开连接 ................................................................................................................................ 13
2010 机试 ......................................................................................................................................... 17
ECNU 的含义 ............................................................................................................................17
空瓶换啤酒.............................................................................................................................. 18
统计字符 .................................................................................................................................. 20
2010 机试热身 ................................................................................................................................. 21
粽子买三送一,买五送二......................................................................................................21
工程流水线问题...................................................................................................................... 22
2011 机试 ......................................................................................................................................... 24
hello world ................................................................................................................................24
Special judge.............................................................................................................................26
查询成绩 .................................................................................................................................. 28
2011 机试热身 ................................................................................................................................. 30
贪吃蛇...................................................................................................................................... 30
仰望星空 .................................................................................................................................. 34
*编辑距离 ................................................................................................................................ 36
2012 机试 ......................................................................................................................................... 38
字母排序 .................................................................................................................................. 38
幸运数...................................................................................................................................... 39
十六进制的加法...................................................................................................................... 42
电话号码簿合并排序..............................................................................................................42
*五子棋 .................................................................................................................................... 43
*正则表达式匹配 .................................................................................................................... 45
2013 机试 ......................................................................................................................................... 46
斐波那契数列的素数个数......................................................................................................46
*将 a 字符变成 b 字符最少修改次数 ....................................................................................47
2013 机试热身 ................................................................................................................................. 49
去重排序 .................................................................................................................................. 49
蛇形图案 .................................................................................................................................. 51
数学手稿 .................................................................................................................................. 54
2009 机试
计算和的数位
Sum of digit
Description
Write a program which computes the digit number of sum of two integers a and b.
Input
The first line of input gives the number of cases, N(1 ≤ N ≤ 100). N test cases follow.
Each test case consists of two integers a and b which are separeted by a space in a line.
(0<=a,b<=100000000).
Output
For each test case, print the number of digits of a + b.
Sample Input
3
5 7
1 99
1000 999
Sample Output
2
3
4
#include
int main()
{
int n;
int a,b;
int sum;
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
int an=0;
scanf("%d%d",&a,&b);
sum=a+b;
while(sum)
{
an++;
sum/=10;
}
printf("%d\n",an++);
}
}
return 0;
}
大写改小写
Capitalize
Description
Write a program which replace all the lower-case letters of a given text with the corresponding
captital letters.
Input
A text including lower-case letters, periods, and space.
Output
Output The converted text.
Sample Input
welcome to east china normal university.
Sample Output
WELCOME TO EAST CHINA NORMAL UNIVERSITY.
#include
#include
char str[1000];
int main()
{
int l;
while(gets(str))
{
l=strlen(str);
int i;
for(i=0;i='a'&&str[i]<='z')
printf("%c",str[i]-32);
else
printf("%c",str[i]);
}
printf("\n");
}
return 0;
}
素数对
Primes Pair
Description
We arrange the numbers between 1 and N (1 <= N <= 10000) in increasing order and decreasing
order like this:
1 2 3 4 5 6 7 8 9 . . . N
N . . . 9 8 7 6 5 4 3 2 1
Two numbers faced each other form a pair. Your task is to compute the number of pairs P such
that both numbers in the pairs are prime.
Input
The first line of input gives the number of cases, C (1 ≤ C ≤ 100). C test cases follow.
Each test case consists of an integer N in one line.
Output
For each test case, output P .
Sample Input
4
1
4
7
51
Sample Output
0
2
2
6
#include
#include
bool prime[10005];
void init()
{
int i;
int j;
prime[0]=prime[1]=false;//不是素数
prime[2]=true;//是素数
for(i=3;i<=10005;i+=2)
{
prime[i]=true;//是素数
prime[i+1]=false;//不是素数 除 0 和 2 之外的偶数都不是素数
}
for(i=3;i<=10005;i+=2)
{
if(prime[i]==true)//是素数
{
j=i+i;
while(j<=10005)
{
prime[j]=false;//不是素数
j+=i;
}
}
}
}
int main()
{
int c;
int n;
init();//初始化
while(scanf("%d",&c)!=EOF)
{
while(c--)
{
scanf("%d",&n);
int sum=0;
int i;
for(i=2;i<=n/2;i++)
{
if(prime[i]==true&&prime[n+1-i]==true)
sum++;
}
sum*=2;
if(n%2==1)//n 为奇数
{
if(prime[n/2+1]==true)
sum+=1;
}
printf("%d\n",sum);
}
}
return 0;
}
求最大公约数和最小公倍数
GCD and LCM
Description
Write a program which computes the greatest common divisor (GCD) and the least common
multiple (LCM) of given a and b (0 < a, b ≤ 44000).
Input
The first line of input gives the number of cases, N(1 ≤ N ≤ 100). N test cases follow.
Each test case contains two interger a and b separated by a single space in a line.
Output
For each test case, print GCD and LCM separated by a single space in a line.
Sample Input
2
8 6
5000 3000
Sample Output
2 24
1000 15000
#include
int getgcd(int a,int b)
{
int gcd;
int t1,t2;
t1=a;
t2=b;
gcd=t1%t2;
while(gcd!=0)
{
t1=t2;
t2=gcd;
gcd=t1%t2;
}
return t2;
}
int main()
{
int n;
int a,b;
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
scanf("%d%d",&a,&b);
printf("%d %d\n",getgcd(a,b),a*b/(getgcd(a,b)));
}
}
return 0;
}
排序后求位置处的数
Sort it…
Description
There is a database,partychen want you to sort the database’s data in the order from the least up
to the greatest element,then do the query: "Which element is i-th by its value?"- with i being a
natural number in a range from 1 to N.
It should be able to process quickly queries like this.
Input
The standard input of the problem consists of two parts. At first, a database is written, and then
there's a sequence of queries. The format of database is very simple: in the first line there's a
number N (1<=N<=100000), in the next N lines there are numbers of the database one in each
line in an arbitrary order. A sequence of queries is written simply as well: in the first line of the
sequence a number of queries K (1 <= K <= 100) is written, and in the next K lines there are
queries one in each line. The query "Which element is i-th by its value?" is coded by the number
i.
Output
The output should consist of K lines. In each line there should be an answer to the corresponding
query. The answer to the query "i" is an element from the database, which is i-th by its value (in
the order from the least up to the greatest element).
Sample Input
5
7
121
123
7
121
3
3
2
5
Sample Output
121
7
123
#include
#include
using namespace std;
int num[100010];
int pos[105];
int main()
{
int n;
int i;
int k;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
scanf("%d",&k);
for(i=1;i<=k;i++)
scanf("%d",&pos[i]);
sort(num+1,num+1+n);
for(i=1;i<=k;i++)
printf("%d\n",num[pos[i]]);
}
return 0;
}
*路由器连接
Hub Connection plan
Description
Partychen is working as system administrator and is planning to establish a new network in his
company. There will be N hubs in the company, they can be connected to each other using cables.