关于一些初级ACM竞赛题目的分析和题解(一)
故事发生在在2017年年底,受我的室友cy1999巨巨的影响下,第一次接触到ACM竞赛,也是作为一名编程小白,第一次感受到编程语言的魅力。用竞赛这种形式来提高自己的编程能力,虽说不上是曲径通幽,但也不失为一种好方法。废话少说,上题。Petya loves football very much. One day, as he was watching a football match, he was writing the players' current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is considered dangerous. For example, the situation 00100110111111101 is dangerous and 11110111011101 is not. You are given the current situation. Determine whether it is dangerous or not.
The first input line contains a non-empty string consisting of characters "0" and "1", which represents players. The length of the string does not exceed 100 characters. There's at least one player from each team present on the field.
Print "YES" if the situation is dangerous. Otherwise, print "NO".
001001
NO
1000000001
YES
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; //以上是打竞赛题的套路
int main()
{
char a[101]; //定义字符串
gets (a); //数组的输入
puts(strstr(a,"0000000")||strstr(a,"1111111")?"YES":"NO"); //数组的输出,且strstr(a,b)判断b是否为a的子集
}
"Contestant who earns a score equal to or greater than the k-th place finisher's score will advance to the next round, as long as the contestant earns a positive score..." — an excerpt from contest rules.
A total of n participants took part in the contest (n ≥ k), and you already know their scores. Calculate how many participants will advance to the next round.
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 50) separated by a single space.
The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 100), where ai is the score earned by the participant who got the i-th place. The given sequence is non-increasing (that is, for all i from 1 to n - 1 the following condition is fulfilled: ai ≥ ai + 1).
Output the number of participants who advance to the next round.
8 5
10 9 8 7 7 7 5 5
6
4 2
0 0 0 0
0
In the first example the participant on the 5th place earned 7 points. As the participant on the 6th place also earned 7 points, there are 6 advancers.
In the second example nobody got a positive score.
这个题目是输入n,k,(1 ≤ k ≤ n ≤ 50) 第二行输入n个数字(0 ≤ ai ≤ 100),且递减 输出有多少个数 大于等于第k个数,以下是代码
#include<bits/stdc++.h>
using namespace std;
int n,k,c,b,a[57];
int main()
{
cin>>n>>k;
while (n>b)
cin>>a[b++]; // 输入数组
while (a[c]&&a[c]>=a[k-1]) // a[c]不等于0且a[c]大于等于a[k-1]
c++;
cout<<c; //输出c
}