ACM基础题(共23道)

题目目录:

  1. CodeForces - 486A
  2. CodeForces 208A
  3. CodeForces 451A
  4. CodeForces 472A
  5. CodeForces 144A
  6. CodeForces 82A
  7. CodeForces 469A
  8. CodeForces 460A
  9. CodeForces 318A
  10. CodeForces 379A
  11. HDU 1576
  12. CodeForces 617A
  13. CodeForces 61A
  14. CodeForces 344A
  15. CodeForces 268A
  16. CodeForces 119A
  17. CodeForces 500A
  18. CodeForces 443A
  19. HDU 2191
  20. CodeForces 510A
  21. CodeForces 268B
  22. CodeForces 509A
  23. CodeForces 432A

1. CodeForces - 486A

A. Calculating Function

For a positive integer n let’s define a function f:
f(n) =  - 1 + 2 - 3 + … + ( - 1)nn
Your task is to calculate f(n) for a given integer n.

Input
The single line contains the positive integer n (1 ≤ n ≤ 1015).
Output
Print f(n) in a single line.

Examples
Input
4
Output
2
Input
5
Output
-3

Note
f(4) =  - 1 + 2 - 3 + 4 = 2
f(5) =  - 1 + 2 - 3 + 4 - 5 =  - 3

问题链接:http://codeforces.com/problemset/problem/486/A
问题简述:输入n,计算f(n) =  - 1 + 2 - 3 + … + ( - 1)^n*n
问题分析:水题,可使用列表法用数组存放结果,获取n后通过数组直接输出。或者分n为正数与负数两种情况讨论。
AC通过代码:

#include<stdio.h> 
typedef long long LL;

int main()
{
	LL n,sum=0;
	scanf("%lld",&n);
	if(n%2==0)
	{
		sum=n/2;
	}else
	{
		sum=(n-1)/2-n;
	}
	printf("%lld",sum);
	return 0;
}

2. CodeForces 208A

A. Dubstep

Vasya works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them.
Let’s assume that a song consists of some number of words. To make the dubstep remix of this song, Vasya inserts a certain number of words “WUB” before the first word of the song (the number may be zero), after the last word (the number may be zero), and between words (at least one between any pair of neighbouring words), and then the boy glues together all the words, including “WUB”, in one string and plays the song at the club.
For example, a song with words “I AM X” can transform into a dubstep remix as “WUBWUBIWUBAMWUBWUBX” and cannot transform into “WUBWUBIAMWUBX”.
Recently, Petya has heard Vasya’s new dubstep track, but since he isn’t into modern music, he decided to find out what was the initial song that Vasya remixed. Help Petya restore the original song.

Input
The input consists of a single non-empty string, consisting only of uppercase English letters, the string’s length doesn’t exceed 200 characters. It is guaranteed that before Vasya remixed the song, no word contained substring “WUB” in it; Vasya didn’t change the word order. It is also guaranteed that initially the song had at least one word.
Output
Print the words of the initial song that Vasya used to make a dubsteb remix. Separate the words with a space.

Examples
Input
WUBWUBABCWUB
Output
ABC
Input
WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB
Output
WE ARE THE CHAMPIONS MY FRIEND

Note
In the first sample: “WUBWUBABCWUB” = “WUB” + “WUB” + “ABC” + “WUB”. That means that the song originally consisted of a single word “ABC”, and all words “WUB” were added by Vasya.
In the second sample Vasya added a single word “WUB” between all neighbouring words, in the beginning and in the end, except for words “ARE” and “THE” — between them Vasya added two “WUB”.

问题链接:http://codeforces.com/problemset/problem/208/A
问题简述(+分析):除去一串字符串中的“WUB”即可,若该WUB在字符串开头则直接去除,若在字符串中间则更改为一个空格。
程序说明:利用了gets获取了一整行输入的字符串便于处理。利用flag变量来表示WUB是否在开头位置。

AC通过代码:

#include<stdio.h>
#include<string.h>

int main()
{
	char s[205];
	gets(s);
	int flag=0,i=0;
	while(i<strlen(s))
	{
		if(s[i]=='W'&&s[i+1]=='U'&&s[i+2]=='B')
		{
			i+=3;
			if(flag==1)
			{
				printf(" ");
			}
		}else
		{
			printf("%c",s[i]);
			flag=1;
			i++;
		}
	}
	return 0;
}

3. CodeForces 451A

After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of n horizontal and m vertical sticks.
An intersection point is any point on the grid which is formed by the intersection of one horizontal stick and one vertical stick.
In the grid shown below, n = 3 and m = 3. There are n + m = 6 sticks in total (horizontal sticks are shown in red and vertical sticks are shown in green). There are n·m = 9 intersection points, numbered from 1 to 9.
The rules of the game are very simple. The players move in turns. Akshat won gold, so he makes the first move. During his/her move, a player must choose any remaining intersection point and remove from the grid all sticks which pass through this point. A player will lose the game if he/she cannot make a move (i.e. there are no intersection points remaining on the grid at his/her move).
Assume that both players play optimally. Who will win the game?

Input
The first line of input contains two space-separated integers, n and m (1 ≤ n, m ≤ 100).
Output
Print a single line containing “Akshat” or “Malvika” (without the quotes), depending on the winner of the game.

Examples
Input
2 2
Output
Malvika
Input
2 3
Output
Malvika
Input
3 3
Output
Akshat

Note
Explanation of the first sample:
The grid has four intersection points, numbered from 1 to 4.
If Akshat chooses intersection point 1, then he will remove two sticks (1 - 2 and 1 - 3). The resulting grid will look like this.
Now there is only one remaining intersection point (i.e. 4). Malvika must choose it and remove both remaining sticks. After her move the grid will be empty.
In the empty grid, Akshat cannot make any move, hence he will lose.
Since all 4 intersection points of the grid are equivalent, Akshat will lose no matter which one he picks.
在这里插入图片描述
问题链接:codeforces.com/problemset/problem/451/A
问题简述(+分析):结果由横向纵向两个方向中stick数量少的一方决定,若该值为奇数,则Akshat赢得胜利,若该值为偶数,获胜者是Malvika。
程序说明:自己写了个小小的比较大小的子函数,也可使用alorithm这个头文件。

AC通过程序:

#include<stdio.h>

int min(int a,int b)
{
	if(a<=b) return a;
	else return b;
}

int main()
{
	int n,m;
	scanf
  • 14
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值