去注释
Description
给你一段C++代码,将其中的注释去除后输出剩余的代码。
注释共有两种形式:
1. 行注视:以//开头,一直作用到行尾为止。
例子:
int n;//n表示数据规模
int a;
去注释后:
int n;
int a;
注意:保留行尾换行符
2. 段注视:以/*开头,到*//结尾,中间部分都是注释,可以跨行。
例子:
int main() {
/*
我是
一段
注释
*/
}
去注释后:
int main() {
}
注意:由于在线评测系统(Online Judge)对网页显示文本作了格式化,一些空行会被删去,导致上面显示的删除后的结果不正确。删除注释后,剩余的代码应该是三行,两行代码之间有一个空 行。这是因为:在段注释结尾符的后面有一个换行符,它不在注释内,需要保留。
Input
一段C++程序代码
Output
去掉注释部分后的程序
Sample Input
int main()
{
/* 我是
一段
注释 */
int n;//n表示数据规模
}
Sample Output
int main()
{
int n;
}
思路
主要是确定和判别‘/’‘/’和‘/*’ ‘*/’。
#include<stdio.h>
#include<string.h>
int main()
{
char a[188888];
char s;
char z;
int i;
while(z=getchar())
{
if(z==-1)
break;
a[i]=z;
i++;
}
int n;
n=strlen(a);
for(i=0;i<n;i++)
{
if(a[i]=='/')
{
if(a[i+1]=='/')
{
while(a[i]!='\n')
{
i++;
}
}
else if(a[i+1]=='*')
{
while(1)
{
i++;
if((a[i-2]=='*')&&(a[i-1]=='/'))
break;
}
}
}
printf("%c",a[i]);
}
return 0;
}
聪明的美食家
Description
如果有人认为吃东西只需要嘴巴,那就错了。
都知道舌头有这么一个特性,“由简入奢易,由奢如简难”(据好事者考究,此规律也适合许多其他情况)。具体而言,如果是甜食,当你吃的食物不如前面刚吃过的东西甜,就很不爽了。
大宝是一个聪明的美食家,当然深谙此道。一次他来到某小吃一条街,准备从街的一头吃到另一头。为了吃得爽,他大费周章,得到了各种食物的“美味度”。他拒绝不爽的经历,不走回头路而且还要爽歪歪(爽的次数尽量多)。
Input
两行数据。
第一行为一个整数n,表示小吃街上小吃的数量
第二行为n个整数,分别表示n种食物的“美味度”
Output
一个整数,表示吃得爽的次数
Sample Input
10 3 18 7 14 10 12 23 41 16 24
Sample Output
6
思路
这是一个最大上升子序列的问题,它与一般的最大上升子序列的问题在于,遇到相同的数字时需要跳过而不是继承下来,所以要用upper_bound。
#include<cstdio>
#include<cstring>
#include<algorithm>
const int MAXN=200001;
int a[MAXN];
int d[MAXN];
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
memset(a,0,sizeof(a));
memset(d,-1,sizeof(d));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
d[1]=a[1];
int len=1;
for(int i=2;i<=n;i++)
{
if(a[i]>=d[len])
d[++len]=a[i];
else
{
int j=std::upper_bound(d+1,d+len+1,a[i])-d;
d[j]=a[i];
}
}
printf("%d\n",len);
}
return 0;
}
开心的cc
题目描述
CC is a smart girl and she is curious about everything. One day she starts to analyze her lifestyle and finds out that she either feels happy or unhappy in any day, then she collects her mood data and makes numerical representation. She writes down 1 if she is happy, otherwise she writes down 0.
For example, if CC is happy, happy, unhappy, happy, happy, unhappy in the next 6 days, a numerical sequence which stands for it is 1,1,0,1,1,0
Meanwhile, smart CC finds her mood sequence is always periodic. She also finds the repeated segment like 1,1,0 in the aforementioned example and calls it “feeling repetend”. As a curious girl, CC wants to make another deep analysis. After she gets the length of “feeling repetend” n, she defines a rule: she tries to find a day whose index is d and considers the next d+n-1 days. For each day d’ in [d, d+n-1], CC guarantee that the number of happy days is more than unhappy days in [1, d’] days.
Now, CC wonder that how many days which satisfy the rule in n “feeling repetend” days.
输入描述:
Input contains multiple test cases.
The first line contains an integer T (1<=T<=20), which is the number of test cases.
Then the first line of each test case contains an integer n (1<=n<=100000), which is the length of “feeling repetend”.
The second line of each test case contains n integers, each integer is either 0 or 1, standing for unhappy or happy.
输出描述:
output a integer represents the answer.
示例1
输入
2
5
1 0 1 1 0
5
1 1 1 1 1
输出
1
5
HINT
For the sample,the days from the third day as a starting point are as follows:
Day1: 1 day happy : 0 days unhappy
Day2: 2 days happy : 0 days unhappy
Day3: 2 days happy : 1 day unhappy
Day4: 3 days happy : 1 day unhappy
Day5: 3 days happy : 2 days unhappy
思路
这是一道水题,但是我没有做出来,没做出来的时候发现很多人做出来了。对于题意的理解花了有一段时间,因为最开始看案例会觉得是简单的开心天数减去不开心的天数,但理解题目后发现并不是这样,是看一个循环内cc开心的天数一直比不开心的天数多的次数,就是一旦1和0的次数相等或0比1多就直接跳出。按照题目的思路我写了程序,就是暴力。之后发现TLE了,但一时想不到其他的方法就去做其他题目了,之后看蛮多同学都做出来了,还以为有什么其他的方法没想到,但到截止还是没写出来。截止之后讨论发现,这道题目其实可以A。因为TLE的时候只要多去几种可能性就会A,而且还有更简单的方法。。。就是开心的天数减去不开心的天数。。。
#include<stdio.h>
int main()
{
int T;
int k;
scanf("%d",&T);
for(k=0;k<T;k++)
{
int n;
scanf("%d",&n);
int i;
int a;
int zero=0,one=0;
for(i=0;i<n;i++)
{
scanf("%d",&a);
if(a==0)
zero++;
else
one++;
}
if(one-zero>0)
printf("%d\n",one-zero);
else
printf("0\n");
}
return 0;
}