codeforces 318 A.Even Odds B.Sereja and Array
【摘要】
A.Even Odds
给你n和k, 把从1到n先排奇数后排偶数排成一个新的序列,输出第k个位置的数。
比如 10 3 拍好后就是 1 3 5 7 9 2 4 6 8 10 第3个数是5。
//cf 318 A//2013-06-18-20.30#include <iostream...
给你n和k, 把从1到n先排奇数后排偶数排成一个新的序列,输出第k个位置的数。
比如 10 3 拍好后就是 1 3 5 7 9 2 4 6 8 10 第3个数是5。
//cf 318 A
//2013-06-18-20.30
#include <iostream>
using namespace std;
int main()
{
__int64 n, k;
while (cin >> n >> k)
{
if (k <= (n+1)/2)
cout << (k-1)*2 + 1 << endl;
else
{
k -= (n+1)/2;
cout << k*2 << endl;
}
}
return 0;
}
就是找到以“heavy” 开头和“metal”结尾的字符串有多少个。
我的思路是标记“heavy” 和“metal”的位置然后计算以每一个“metal”结尾的有多少个,然后相加。
//cf 318 B
//2013-06-18-21.01
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int maxn = 1000006;
char str[maxn];
int s[maxn];
int e[maxn];
int main()
{
while (scanf("%s", str) != EOF)
{
memset(s, 0, sizeof(s));
memset(e, 0, sizeof(e));
int l = strlen(str);
for (int i = 0; i < l-4; i++)
{
if (str[i] == 'h' && str[i+1] == 'e' && str[i+2] == 'a' && str[i+3] == 'v' && str[i+4] == 'y')
{
s[i] = 1;
i += 4;
continue;
}
if (str[i] == 'm' && str[i+1] == 'e' && str[i+2] == 't' && str[i+3] == 'a' && str[i+4] == 'l')
{
e[i] = 1;
i += 4;
continue;
}
}
__int64 ans = 0;
for (int i = 1; i < l; i++)
{
if (e[i])
ans += s[i-1];
s[i] += s[i-1];
}
cout << ans << endl;
}
return 0;
}
文章来源: xindoo.blog.csdn.net,作者:xindoo,版权归原作者所有,如需转载,请联系作者。
原文链接:xindoo.blog.csdn.net/article/details/9123909
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)