练习打卡(C++)

简介

       嘿嘿,大家好久不见呐*~(^-^)~*,是的没错,我又回来啦,前段时间学校专业课程实在是太多了(再加上学校因为要搞什么评分,课堂上还要收手机、平板,还不允许带电脑T_T),再加上还要准备蓝桥杯、天梯赛和数学建模,一直莫有来打卡嘞,现在,终于,回来啦~~~  

        另外另外,通过这次天梯赛,我发现我的字符串相关问题很是薄弱,这周主要是打卡练习字符串咯。

59A.Word

传送吧,皮卡丘!

Vasya is very upset that many people on the Net mix uppercase and lowercase letters in one word. That's why he decided to invent an extension for his favorite browser that would change the letters' register in every word so that it either only consisted of lowercase letters or, vice versa, only of uppercase ones. At that as little as possible letters should be changed in the word. For example, the word HoUse must be replaced with house, and the word ViP — with VIP. If a word contains an equal number of uppercase and lowercase letters, you should replace all the letters with lowercase ones. For example, maTRIx should be replaced by matrix. Your task is to use the given method on one given word.

瓦夏对网上许多人在一个单词中混用大小写字母感到非常不满。因此,他决定为他最喜欢的浏览器发明一个扩展程序,改变每个单词中字母的寄存器,使其要么只包含小写字母,要么只包含大写字母。在这种情况下,单词中应尽可能少地更改字母。例如,HoUse必须替换为house,而ViP则替换为VIP。如果一个单词包含相同数量的大写字母和小写字母,则应将所有字母替换为小写字母。例如,maTRIx 应替换为 matrix。您的任务是在一个给定的单词上使用给定的方法。

Input

The first line contains a word s — it consists of uppercase and lowercase Latin letters and possesses the length from 1 to 100.

第一行包含一个单词 s - 它由大写和小写拉丁字母组成,长度从 1 到 100 。

Output

Print the corrected word s. If the given word s has strictly more uppercase letters, make the word written in the uppercase register, otherwise - in the lowercase one.

打印更正后的单词 s 。如果给定的单词 s 有严格意义上更多的大写字母,则将该单词写入大写寄存器,否则写入小写寄存器。

Examples

input

HoUse

output

house

input

Vip

output

VIP

input

maTRIx

output

matrix
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int a=0,b=0,l;
string s;
int main() 
{
	cin>>s;
	l=s.length();
	for(int i=0;i<l;i++){
		if(s[i]>=97&&s[i]<=122)
			a++;
		else if(s[i]>=65&&s[i]<=90)
			b++;
	}
	if(a>=b)
		transform(s.begin(),s.end(),s.begin(),::tolower);//转化成小写
	else
		transform(s.begin(),s.end(),s.begin(),::toupper);//转换成大写
	cout<<s;
    return 0;
}

        感觉这道题主要是考察字符串的大小写相互转换,这里我主要是直接使用的algortihm里面的tsansform()函数(当然我们也可以使用char手写转化大小)

【  tsansform():transform(起始地址,终止地址,转换后存放容器起始地址,::tolower)

注:::tolower和::toupper是c++库中的函数,可以将一个字符转换成小写或大写,在代码中它们作为参数传递给transform函数,通过函数对每个字符进行操作  】

        手写转换大小:

	if(a>=b)
		for(int i=0;i<l;i++){
			if(s[i]>=65&&s[i]<=90)
				s[i]+=32;//大写转换成小写
		}
	else
		for(int i=0;i<l;i++){
			if(s[i]>=97&&s[i]<=122)
				s[i]-=32;//小转大
		}

734A.Anton and Danilk

摇摇欲坠的魔法传送阵一个~~~

Anton likes to play chess, and so does his friend Danik.

Once they have played n games in a row. For each game it's known who was the winner — Anton or Danik. None of the games ended with a tie.

Now Anton wonders, who won more games, he or Danik? Help him determine this.

安东喜欢下棋,他的朋友达尼克也是。

有一次,他们连续下了 n 盘棋。安东和达尼克谁是赢家,每盘棋都有答案。没有一盘是平局。

现在安东想知道,他和达尼克谁赢的更多呢?帮他确定一下。

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of games played.

The second line contains a string s, consisting of n uppercase English letters 'A' and 'D' — the outcome of each of the games. The i-th character of the string is equal to 'A' if the Anton won the i-th game and 'D' if Danik won the i-th game.

输入的第一行包含一个整数 n ( 1 ≤ n ≤ 100 000 ) --已玩游戏的数量。

第二行包含一个字符串 s ,由 n 个大写英文字母"A"和"D" --每局游戏的结果。如果安东赢了第 i 局,则字符串的第 i 个字符等于"A";如果达尼克赢了第 i 局,则字符串的第 i 个字符等于"D"。

Output

If Anton won more games than Danik, print "Anton" (without quotes) in the only line of the output.

If Danik won more games than Anton, print "Danik" (without quotes) in the only line of the output.

If Anton and Danik won the same number of games, print "Friendship" (without quotes).

如果安东赢得的比赛多于达尼克,则在输出的唯一一行打印"安东"(不带引号)。

如果达尼克赢得的比赛比安东多,则在输出的唯一一行打印"达尼克"(不带引号)。

如果安东和达尼克赢得的棋局数相同,则打印"友谊"(不带引号)。

Note

In the first sample, Anton won 6 games, while Danik — only 1. Hence, the answer is "Anton".

In the second sample, Anton won 3 games and Danik won 4 games, so the answer is "Danik".

In the third sample, both Anton and Danik won 3 games and the answer is "Friendship".

在第一个示例中,安东赢得了 6 场比赛,而达尼克 -只赢得了 1 场比赛。因此,答案是"安东"。

在第二个样本中,安东赢得了 3 局,而达尼克赢得了 4 局,因此答案是"达尼克"。

在第三个示例中,Anton 和 Danik 都赢得了 3 局,因此答案是"友谊"。

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

int n,a,d;
string s;
int main()
{
	cin>>n>>s;
	for(int i=0;i<s.length();i++){
		if(s[i]=='A')
			a++;
		else if(s[i]=='D')
			d++;
	}
	if(a==d)
		cout<<"Friendship";
	else if(a<d)
		cout<<"Danik";
	else cout<<"Anton";
	return 0;
}

        遍历字符串,遇到A就Anton的次数++,反之Danilk++,判断大小然后输出,秒了!

41A.Translation

传送魔法阵修复ing

The translation from the Berland language into the Birland language is not an easy task. Those languages are very similar: a berlandish word differs from a birlandish word with the same meaning a little: it is spelled (and pronounced) reversely. For example, a Berlandish word code corresponds to a Birlandish word edoc. However, it's easy to make a mistake during the «translation». Vasya translated word s from Berlandish into Birlandish as t. Help him: find out if he translated the word correctly.

将伯兰语翻译成比尔兰语并非易事。这两种语言非常相似:伯兰语单词与具有相同含义的比尔语单词略有不同:拼写(和发音)相反。例如,伯兰语单词 code 与伯兰语单词 edoc 相对应。然而,在 "翻译 "过程中很容易出错。瓦夏将单词 s 从伯兰语翻译成了比尔兰语,即 t 。帮助他:看看他是否翻译正确。

Input

The first line contains word s, the second line contains word t. The words consist of lowercase Latin letters. The input data do not consist unnecessary spaces. The words are not empty and their lengths do not exceed 100 symbols.

第一行包含单词 s ,第二行包含单词 t 。单词由小写拉丁字母组成。输入数据不包含多余空格。单词不为空,长度不超过 100 个符号。

Output

If the word t is a word s, written reversely, print YES, otherwise print NO.

如果单词 t 是反向书写的单词 s ,则打印 是,否则打印 否。

Examples

input

code
edoc

output

YES

input

abb
aba

output

NO

input

code
code

output

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

string s,s1="",t; 
int main()
{
	cin>>s>>t;
	for(int i=s.length()-1;i>=0;i--)
		s1+=s[i];
	if(s1==t)
		cout<<"YES";
	else cout<<"NO";
	return 0;
}

       这道题的思路也还是很简单嘞,创建一个空字符串s1,然后将键盘输入的字符串s反着存入s1后将字符串s1和t做对比,如果一样则输出YES反之输出NO(我反转字符串时手打的,这里转换字符串也可以通过使用algorithm里面的reverse 函数来实现),这里需要注意是i=s.length()-1喔

^_~*。

        使用reverse函数后:

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

string s,t;
int main()
{
    cin>>s>>t;
    reverse(s.begin (),s.end ());//反转
    if(s==t)
		cout<<"YES";
	else cout<<"NO";
    return 0;
}

        当然,还有直接使用条件表达式+puts函数来输出的(看起来代码会简洁很多很多^0^)。

puts(s==t?"YES":"NO");//如果两字符串相等就输出YES,反之输出NO

96A.Football

芜湖,魔法传送阵好像修好了,不过居然不是rating800的?!

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.

Petya 非常喜欢足球。一天,他在观看一场足球比赛时,在一张纸上写下了球员目前的位置。为了简化,他将其描绘成一个由 0 和 1 组成的字符串。0 代表一支球队的球员,1 代表另一支球队的球员。如果至少有 7 名某队球员相继站在一起,那么这种情况就被认为是危险的。例如,情况 00100110111111101 是危险的,而 11110111011101 不是。给你当前的情况。请判断是否危险。

Input

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.

第一行输入由字符 "0 "和 "1 "组成的非空字符串,代表玩家。字符串长度不超过 100 个字符。每队至少有一名球员出场。

Output

Print "YES" if the situation is dangerous. Otherwise, print "NO".

如果情况危险,则打印 "是"。否则打印 "否"。

Examples

input

001001

output

NO

input

1000000001

output

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

int ans=0;
string s;
int main()
{
	cin>>s;
	for(int i=0;i<s.length();i++){
		if(s[i+1]==s[i]){
			ans++;
			if(ans==6){
				cout<<"YES";
				return 0;
			}
		}
		else ans=0;
	}
	if(ans==0)
		cout<<"NO";
	return 0;
}

        有7个一样的数字就判断为危险(居然不用区分敌我),很明显这道题咱们只需要判断有没有7个连再一起的相同数字就行啦,遍历一遍输入的字符串,过程中如果挨着一个一模一样的数就ans++,如果还莫有到7个一样的就挨着一个不一样的数字咱们直接ans清0就行嘿嘿,等到有ans==6(有7个队友都挨着的)的,直接果断输出YES !

208A.Dubstep

魔法阵,启动!

Vasya works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them.

Let's assume that a song consists of some number of words. To make the dubstep remix of this song, Vasya inserts a certain number of words "WUB" before the first word of the song (the number may be zero), after the last word (the number may be zero), and between words (at least one between any pair of neighbouring words), and then the boy glues together all the words, including "WUB", in one string and plays the song at the club.

For example, a song with words "I AM X" can transform into a dubstep remix as "WUBWUBIWUBAMWUBWUBX" and cannot transform into "WUBWUBIAMWUBX".

Recently, Petya has heard Vasya's new dubstep track, but since he isn't into modern music, he decided to find out what was the initial song that Vasya remixed. Help Petya restore the original song.

瓦夏在最好的伯兰夜总会担任 DJ,他经常在表演中使用 dubstep 音乐。最近,他决定将几首老歌重新混编成 dubstep 音乐。

假设一首歌由若干单词组成。为了制作这首歌的 dubstep 混音版,Vasya 在歌曲的第一个单词前(单词数可以为零)、最后一个单词后(单词数可以为零)和单词之间(单词数之间至少有一个单词)插入一定数量的单词"WUB"、和词与词之间(任何一对相邻的词之间至少有一个),然后男孩将包括"WUB"在内的所有词粘成一串,并在俱乐部演奏这首歌。

例如,一首歌的歌词是"I AM X",它可以转化为"WUBWUBIWUBAMWUBWUBX"的混音版,而不能转化为"WUBWUBIAMWUBX"。

最近,Petya 听到了 Vasya 的新配乐,但由于他并不喜欢现代音乐,他决定找出 Vasya 最初混音的歌曲。帮助 Petya 还原原始歌曲。

Input

The input consists of a single non-empty string, consisting only of uppercase English letters, the string's length doesn't exceed 200 characters. It is guaranteed that before Vasya remixed the song, no word contained substring "WUB" in it; Vasya didn't change the word order. It is also guaranteed that initially the song had at least one word.

输入由一个非空字符串组成,该字符串仅由大写英文字母组成,字符串长度不超过 200 个字符。保证在 Vasya 将歌曲重新混音之前,没有任何单词包含子串"WUB" ;Vasya 没有更改单词顺序。此外,还可以保证歌曲最初至少有一个单词。

Output

Print the words of the initial song that Vasya used to make a dubsteb remix. Separate the words with a space.

打印 Vasya 用于制作 dubsteb 混音版的初始歌曲的歌词。用空格分隔歌词

Examples

input

WUBWUBABCWUB

output

ABC 

input

WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB

output

WE ARE THE CHAMPIONS MY FRIEND 

Note

In the first sample: "WUBWUBABCWUB" = "WUB" + "WUB" + "ABC" + "WUB". That means that the song originally consisted of a single word "ABC", and all words "WUB" were added by Vasya.

In the second sample Vasya added a single word "WUB" between all neighbouring words, in the beginning and in the end, except for words "ARE" and "THE" — between them Vasya added two "WUB".

在第一个示例中:"WUBWUBABCWUB" = "WUB"+ "WUB"+ "ABC"+ "WUB" 。也就是说,这首歌最初只有一个词"ABC",而所有词"WUB"都是瓦夏加上去的。

在第二个示例中,Vasya 在所有相邻单词之间添加了单词"WUB",包括开头和结尾、除了单词"ARE"和"THE"--Vasya 在它们之间添加了两个"WUB"。

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

int ans=1,count;
string s,s1=" ";
int main()
{
	cin>>s;
	for(int i=0;i<s.length();i++){
		while(ans==1){
			if(s[i]=='W'&&s[i+1]=='U'&&s[i+2]=='B'){
				s.erase(i,3);
			}else ans-=1;//要保证第一个单词前面莫有空格
		}
		if(s[i]=='W'&&s[i+1]=='U'&&s[i+2]=='B'){
			if(count==0){
				s.erase(i,3);
				s.insert(i,s1);
			}
		}	
	}
	for(int i=0;i<s.length();i++){//删掉多余空格
		if(s[i]==' '&&s[i+1]==' ')
			s.erase(i,1);
	}
	cout<<s;
	return 0;
}

        一定一定要保证第一次遇到不需要删去的字母前没有空格!!!(这道题当时真的改了好久好久T_T)

        在这道题中我是先建立了一个字符串s1专门拿来放空格,以便于我后面裁剪掉“WUB”后可以直接使用insert()函数把空格(也就是s1)插入字符串s,我还定义了一个ans,是用来判断是否应该删掉“WUB”后添加空格的(ans!=1就说明已经有字母作为句子的开头了,后期删除“WUB”后需要添加“ ”),当然,到了后面过案例的时候我发现会出现多个空格的情况,所以后期我使用了一个for循环来删掉多余的空格^0^)/。

这一次记录就到此为止啦,后面关于字符串的问题我就不再记录rating为800的题目啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值