练习打卡(C++)

本文涉及的编程主题包括分西瓜问题、偶数分块算法,以及对Bit++语言的理解和应用。
摘要由CSDN通过智能技术生成

目录

简介

A.Watermelon

A.Way Too Long Words

A.Team

A.Next Round

A.Bit++


简介

放弃目标,建立系统。

目前基础了好一些算法知识点,但是看着题还是一样的无解,脑袋真的是一片空白(这脑袋我看着它也不转呐),寻求了一下师兄们的看法,我决定组建一支冒险队攻打CF(说白了就是打codefoces哈哈)。以此,立下一个新年flag:

每天坚持打俩小时的CF并且周每周抽时间在CSDN打卡,来奖励勇敢面临寒冬的自己(^-^)~*

A.Watermelon

穿越到原题即可点击此处(^0^)

One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.

Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight.

一个炎热的夏天,皮特和他的朋友比利决定买一个西瓜。他们选了一个在他们看来最大、最成熟的西瓜。之后,他们称了称西瓜,天平显示 w 公斤。他们渴得要死,急忙赶回家,决定把西瓜分掉,但是他们遇到了一个难题。

皮特和比利非常喜欢偶数,所以他们想把西瓜分成两部分,每部分的重量都是偶数,但同时又不一定要相等。孩子们已经非常累了,想尽快开始吃饭,所以你应该帮助他们,看看他们能不能按照自己的方式分西瓜。可以肯定的是,他们每个人都应该分到正重的部分。

Input

The first (and the only) input line contains integer number w (1 ≤ w ≤ 100) — the weight of the watermelon bought by the boys.

第一行(也是唯一一行)输入整数 w ( 1 ≤ w ≤ 100 ) - 男孩们买的西瓜的重量。

Output

Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case.

Examples

input

8

output

YES

如果男生能把西瓜分成两份,每份的重量是偶数,则打印是;反之,则打印否。

#include<iostream>
#include<cstdio>
using namespace std;

int w;
int main()
{
	scanf("%d",&w);
	if(w==2)//特判
		printf("NO");
	else if(w%2==0){
		printf("YES");
	}
	else printf("NO");
}

这道分西瓜的题真的超水,只需要判断是否是偶数就行咯(除2以外的偶数都能被分为两个偶数),记得特判一下2就行(因为2只能分成1和1嘛,一开始我就是没想起来要特判=.=)。

A.Way Too Long Words

(^-^)

Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.

Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.

This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.

Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".

You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.

有时,有些词,如"本地化"或"国际化"太长,在一篇文章中多次书写会很累。

太长,如果其长度严格超过 10 个字符,我们就可以将其视为太长。所有过长的单词都应使用特殊缩写替换。

这种缩写是这样的:我们写下单词的第一个字母和最后一个字母,然后在它们之间写下第一个字母和最后一个字母之间的字母数。该数字采用十进制,不包含任何前导零。

因此,"本地化"将拼写成"l10n",而"国际化"将拼写成"i18n"。

建议您自动更改缩写词。这样,所有太长的单词都应被缩写取代,而不太长的单词则不应进行任何更改。

Input

The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.

第一行包含一个整数 n ( 1 ≤ n ≤ 100 )。接下来的每 n 行包含一个单词。所有单词均由小写拉丁字母组成,长度为 1 至 100 个字符。

Output

Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.

打印 n 行。第 i 行应包含替换输入数据中第 i 个字的结果。

Examples

input

4
word
localization
internationalization
pneumonoultramicroscopicsilicovolcanoconiosis

output

word
l10n
i18n
p43s
#include<iostream>
#include<string>
using namespace std;

int n;
string s;
int main()
{
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>s;
		if(s.length()<=10)//如果字符串长度没有超过10的长度,直接输出字符串
			cout<<s<<endl;
		else{
			cout<<s[0]<<s.length()-2<<s[s.length()-1]<<endl;//先输出字符串第一个字符,然后输出字符串除去头尾的长度,最后输出字符串最后一个字符
		}	
	}
	return 0;
}

这道题直接输入字符串,直接按顺序挨个输出就好咯,不需要其他的什么处理方式。

/*事实上对于输入量较大的题使用cin会出现超世的情况,在C++中scanf读入数据快于cin,以此更推荐加入头文件cstdio,使用scanf读入printf输出。(我用cin纯纯是习惯性,绝对不是想要偷懒)当然,也可以对cin进行优化(具体就不扯远咯,有兴趣可以自行查找^o^)*/。

A.Team

(^0^)

One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution.

This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.

一天,三个好朋友 Petya、Vasya 和 Tonya 决定组成一个团队,参加编程竞赛。在编程比赛中,参赛者通常会遇到几个问题。早在比赛开始前,朋友们就决定,如果至少有两个人对问题的解决方案有把握,他们就会去实现它。否则,朋友们不会写出问题的解决方案。

本次比赛为参赛者提供了 n 个问题。对于每个问题,我们都知道哪位朋友确定了解决方案。帮助朋友们找出他们会写出解决方案的问题数。

Input

The first input line contains a single integer n (1 ≤ n ≤ 1000) — the number of problems in the contest. Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem's solution, otherwise he isn't sure. The second number shows Vasya's view on the solution, the third number shows Tonya's view. The numbers on the lines are separated by spaces.

第一行包含一个整数 n ( 1 ≤ n ≤ 1000 )--竞赛中的问题数。然后 n 行分别包含三个整数,每个整数要么是 0 ,要么是 1 。如果行中的第一个数字等于 1 ,则表示 Petya 确定问题的答案,否则表示他不确定。第二个数字表示瓦夏对解法的看法,第三个数字表示冬妮娅的看法。各行数字之间用空格隔开。

Output

Print a single integer — the number of problems the friends will implement on the contest.

打印一个整数--朋友们在比赛中要解决的问题数量。

Examples

input

3
1 1 0
1 1 1
1 0 0

output

2

input

2
1 0 0
0 1 1

output

1

Note

In the first sample Petya and Vasya are sure that they know how to solve the first problem and all three of them know how to solve the second problem. That means that they will write solutions for these problems. Only Petya is sure about the solution for the third problem, but that isn't enough, so the friends won't take it.

In the second sample the friends will only implement the second problem, as Vasya and Tonya are sure about the solution.

在第一个示例中,Petya 和 Vasya 确信他们知道如何解决第一个问题,而且他们三个都知道如何解决第二个问题。这意味着他们将写出这些问题的解决方案。只有 Petya 对第三个问题的解法有把握,但这还不够,所以朋友们不会接受。

在第二个样本中,朋友们将只执行第二个问题,因为瓦夏和冬妮娅对解法很有把握。

#include<iostream>
using namespace std;

int ans,num=0,n,a[5];//ans用来统计一组数据中有几个1,num用来统计需要解决的问题数量
int main()
{
	cin>>n;
	for(int i=0;i<n;i++){
		ans=0;//每组数据分析完后清零
		for(int j=0;j<3;j++)
			cin>>a[j];
		for(int j=0;j<3;j++){
			if(a[j]==1)
			ans++;
		}
		if(ans>=2)
			num++;	//两个人都能解决,问题数+1
	}
	cout<<num;
}

这道题我使用了两个用来统计的自变量(ans、num),第一层循环是用来统计需要解决的问题个数的(也就是包含两个及以上1的数据组数),第二层循环分别是输入数据组和判断每组包含多少个1。

A.Next Round

哆啦a梦的任意门('0')(在探索颜文字的路上越走越远)

"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.

"得分等于或高于 k 名选手得分的选手将进入下一轮,只要该选手获得正分......"- 竞赛规则摘录。

共有 n 名参赛者参加了比赛( n ≥ k ),您已经知道了他们的分数。请计算有多少人将晋级下一轮。

Input

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).

输入的第一行包含两个整数 n 和 k ( 1 ≤ k ≤ n ≤ 50 ),中间用一个空格隔开。

第二行包含 n 个空格分隔的整数 a1, a2, ..., an ( 0 ≤ ai ≤ 100 ),其中 ai 是获得第 i 名的参赛者的得分。给定序列为非递增序列(即从 1 到 n - 1 的所有 i 都满足以下条件: ai ≥ ai + 1 ).

Output

Output the number of participants who advance to the next round.

输出晋级下一轮的参赛者人数。

Examples

input

8 5
10 9 8 7 7 7 5 5

output

6

input

4 2
0 0 0 0

output

0

Note

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.

在第一个例子中,第 5 名的参赛者获得了 7 分。由于第 6 名的参赛者也获得了 7 分,因此有 6 名晋级者。

在第二个示例中,没有人获得正分。

#include<iostream>
using namespace std;

int n,k,a[51],ans=0;
int main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i]; 
	}
	for(int i=1;i<=n;i++){
		if(a[i]>=a[k]&&a[i]>0)//第i个大于等于第k个就计一次
			ans++; 
	}
	cout<<ans;  
	return 0;
}

又成功水了一题,这道题只需要判断是否大于等于第k位选手的分数就行咯,输入量也不是很大。 ^0^)>*

A.Bit++

*\(^-^)/*

The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.

The language is that peculiar as it has exactly one variable, called x. Also, there are two operations:

  • Operation ++ increases the value of variable x by 1.
  • Operation -- decreases the value of variable x by 1.

A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.

A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.

You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).

比特大陆的经典编程语言是 Bit++。这种语言非常奇特和复杂。

这种语言之所以奇特,是因为它只有一个变量,叫做 x 。此外,还有两种操作:

  • 操作 ++ 使变量 x 的值增加 1。
  • 操作 -- 使变量 x 的值减少 1。

Bit++ 语言中的语句是一个序列,由一个操作和一个变量 x 组成。语句的书写没有空格,也就是说,它只能包含字符"+"、"-"、"X"。执行语句意味着应用其中包含的操作。

Bit++ 中的程序是一系列语句,每条语句都需要执行。执行一个程序意味着执行它所包含的所有语句。

给你一个 Bit++ 语言的程序。 x 的初始值是 0 。执行该程序并找出它的最终值(执行该程序时变量的值)。

Input

The first line contains a single integer n (1 ≤ n ≤ 150) — the number of statements in the programme.

Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter «X»). Thus, there are no empty statements. The operation and the variable can be written in any order.

第一行包含一个整数 n (1 ≤ n ≤ 150) - 程序中的语句数。 (1 ≤ n ≤ 150) - 程序中的语句数。

接下来的 n 行各包含一条语句。每个语句包含一个操作(++ 或 --)和一个变量 x (用字母"X"表示)。因此,不存在空语句。操作和变量可以按任意顺序书写。

Output

Print a single integer — the final value of x.

打印一个整数-- x 的最终值。

Examples

input

1
++X

output

1

input

2
X++
--X

output

0
#include<iostream>
#include<string>
using namespace std;

int n,x=0;
string s;
int main()
{
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>s;
		for(int j=0;j<s.length();j++){
			if(s[j]=='+'&&s[j+1]=='+')
				x++;
			if(s[j]=='-'&&s[j+1]=='-')
				x--;
		}
	}
	cout<<x;
}

这道题思路也很简单嘞,直接把一条语句看成一排字符串,然后依次判断n次字符串中是否出现了“++”和“--”就行。(出现了“++”就直接把变量x+1,反之则x-1)

总的来说目前做的这几个试水题都不是很难,思路都比较简单明了(当然还是因为我目前找的题都是Ratng在800左右的^-^)

打卡打卡打卡,如果有错误的话欢迎指正!第一时间就改掉问题哈哈。

  • 21
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值