概述
三道简单题
A. Even Odds
Being a nonconformist, Volodya is displeasedwith the current state of things, particularly with the order of naturalnumbers (natural number is positive integer number). He is determined torearrange them. But there are too many natural numbers, so Volodya decided tostart with the first n. He writes down the following sequence of numbers:firstly all odd integers from 1 to n (in ascending order), then all evenintegers from 1 to n (also in ascending order). Help our hero to find out whichnumber will stand at the position number k.
Input
The only line of input contains integers nand k (1≤k≤n≤1e14).
Output
Print the number that will stand at the position number kafter Volodya's manipulations.
Sample test(s)
input
10 3
7 7
output
5
6
Note
In the first sample Volodya's sequence willlook like this: {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}. The third place in thesequence is therefore occupied by the number 5.
题目是说把自然数重新排列,先排奇数,按照从小到大的顺序,排完奇数再排偶数,也是按照从小到大的顺序。输入一个数,输出对应位置上的自然数。
解题思路:
简单题,分两种情况,判断输入数对应位置上的自然数是奇数还是偶数,然后运用等差数列的通项公式求解。
注意:做除法时不能随意化简,比如定义int a;a=2*((b-c)/2-1),不能化简为a=b-c-2。因为会出现除2不能整除的情况。例如:b=6,c=1,按前一种算,a=2*((b-c)/2-1)=2*((6-1)/2-1)=2*(2-1)=2,其中(6-1)/2=2而不是=2.5;按化简后计算为a=3,该答案是错误的。
Code:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
long long n,k;
while(scanf("%lld%lld",&n,&k)!=EOF)
{
if(k>(n+1)/2)
printf("%lldn",2+2*(k-(n+1)/2-1));//带除法的不能随意化简
else
printf("%lldn",1+2*(k-1));
}
return 0;
}
B. Strings of Power
Problem Description
Volodya likes listening to heavy metal and(occasionally) reading. No wonder Volodya is especially interested in textsconcerning his favourite music style.
Volodya calls a string powerful if it startswith "heavy" and ends with "metal". Finding all powerfulsubstrings (by substring Volodya means a subsequence of consecutive charactersin a string) in a given text makes our hero especially joyful. Recently he feltan enormous fit of energy while reading a certain text. So Volodya decided tocount all powerful substrings in this text and brag about it all day long. Helphim in this difficult task. Two substrings are considered different if theyappear at the different positions in the text.
For simplicity, let us assume that Volodya'stext can be represented as a single string.
Input
Input contains a single non-empty stringconsisting of the lowercase Latin alphabet letters. Length of this string willnot be greater than 1e6 characters.
Output
Print exactly one number — thenumber of powerful substrings of the given string.
Sample test(s)
input
heavymetalisheavymetal
heavymetalismetal
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
output
3
2
3
Note
In the first sample the string"heavymetalisheavymetal" contains powerful substring "heavymetal"twice, also the whole string "heavymetalisheavymetal" is certainlypowerful.
In the second sample the string"heavymetalismetal" contains two powerful substrings:"heavymetal" and "heavymetalismetal".
题意为:在一串字符串中统计同时匹配'heavy'和'metal'的个数,必须'heavy'在前,'metal'在后,每个单词可以重复使用。
思路有两个:第一种是自己想出来的,比较繁琐,固定’heavy‘来寻找’metal‘的个数,即每寻找到一个’heavy‘,就查看其后有多少个’metal‘;第二种正好相反,固定’metal‘来寻找’heavy‘的个数,每遇到一个’metal‘,就查看其前面有多少个’heavy‘,相对来说比较简单。
方法一:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
int heavy[1000],metal[1000];
int i,j;
while(cin>>s)
{
int count1=0,count2=0;
int sum=0;
for(i=0;i<s.length();i++)
{
if(s[i]=='h'&&s[i+1]=='e'&&s[i+2]=='a'&&s[i+3]=='v'&&s[i+4]=='y')
{
heavy[count1++]=i;
i+=4;
}
if(s[i]=='m'&&s[i+1]=='e'&&s[i+2]=='t'&&s[i+3]=='a'&&s[i+4]=='l')
{
metal[count2++]=i;
i+=4;
}
}
for(i=0;i<count1;i++)
{
for(j=0;j<count2;j++)
if(heavy[i]<metal[j])
sum++;
}
cout<<sum<<endl;
}
return 0;
}
方法二:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
while(cin>>s)
{
int sum=0,count=0;
for(int i=0;i<s.length();i++)
{
if(s[i]=='h'&&s[i+1]=='e'&&s[i+2]=='a'&&s[i+3]=='v'&&s[i+4]=='y')
{
count++;
i+=4;
}
else if(s[i]=='m'&&s[i+1]=='e'&&s[i+2]=='t'&&s[i+3]=='a'&&s[i+4]=='l')
{
sum+=count;
i+=4;
}
}
cout<<sum<<endl;
}
return 0;
}
比较两种方法,第一种繁琐是因为在实现时需要记录’‘heavy’和metal‘的位置,避免’heavy‘的位置出现在’metal‘的后面;而第二种方法只需扫一遍,不需记录它们的位置,由此简便一些。
第二种方法,开阔了思路,值得总结。
C. 蜗牛!快爬!
Problem Description
很久以前有一只蚂蚁,某天在路上走着走着,突然看见了一只蜗牛,爬的很慢。心想自己虽然比它小,可是跑得比它快,于是蚂蚁想跟蜗牛进行一次马拉松比赛,想证明它的实力。蚂蚁跟蜗牛商量后决定在星期六下午进行马拉松比赛。
Theday is coming….比赛跑道为一条直线,Unlucky!!跑道上有很多坑。注意哦,坑!蜗牛想知道它掉下坑底后,爬上来需要多少时间,你的任务是帮忙蜗牛算出它掉入每个坑后,从坑底爬上来的时间,蜗牛每爬半个小时,就得休息半个小时,休息的时候,蜗牛会往下掉一定的深度。坑的深度,往上爬的高度以及休息时往下掉的深度给定。
Input
输入第一行包含一个整数N(0<=N<=1000),表示跑道上坑的数量。
接下来N行,代表N个坑的测试数据,每组测试数据占一行,包含三个整数H、i、j(用一个空格分开),代表坑的深度、往上爬的高度、休息时往下掉的深度。(0<=H、i、j<2^16)
Output
对于每一个坑,输出蜗牛爬上岸所需要的时间(采用进一法,单位:小时)。不能爬上来则输出“Neversee sun!”
Sample Input
2
10 10 0
10 0 0
Sample Output
1
Never see sun!
题意很简单,就是蜗牛爬坑,每往前爬一段距离,就往后退一段距离。问蜗牛能否爬出坑来,若能,用多长时间。
解题思路:
数据量很大,模拟会爆掉。仔细分析不难发现,蜗牛能否爬出坑与坑的深度以及上升距离、下降距离都有关系。若一次上升距离不小于坑的深度,则可以爬出(无论下降多少),且时间确定;若一次上升距离小于坑的深度且上升距离不大于下降距离,则永远爬不出坑;其他情况均能爬出坑,需要计算时间。
计算时间时注意陷阱,若最后一次爬出去了,则不再往下掉。
本题用到了一个向上取整函数ceil();包含在头文件<cmath>中,另外向下取整函数为floor()。
取整函数详解:http://blog.csdn.net/piaomiao_hongying/article/details/9261423
Code:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
float a,b,c;//注意定义为float型
cin>>a>>b>>c;
if(b>=a)
cout<<"1"<<endl;
else if(b<=c)
cout<<"Never see sun!"<<endl;
else
cout<<ceil((a-b)/(b-c))+1<<endl;//这样解决时间陷阱
}
return 0;
}
最后
以上就是平淡鸡翅为你收集整理的2013年暑假队内选拔赛题解1的全部内容,希望文章能够帮你解决2013年暑假队内选拔赛题解1所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复