[C ++] - Find the words brother of the solution to a problem, the problem of table tennis basket, camel nomenclature

1. Find the string Brothers

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Topic read it? So, now we can analyze, the first digital input is n string you are about to enter a number ; the second step is to enter n string ; enter the third step is that you want to match string ; n fourth step is to find your brother string m-th
codes offer:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
bool isBrother(string str, string s) {  //判断是否为兄弟字符串
	if (str.size() == s.size()) {
		if (str == s)
			return false;
		sort(str.begin(), str.end());  //分别对两个字符串进行字典序排序
		sort(s.begin(), s.end());
		if (str == s)
			return true;
	}
	return false;
}
int main() {
	int num;   //输入字符串数量
	while (cin >> num) {
		string str;  
		string word, s;
		int index;
		vector<string> vs;
		for (int i = 0; i < num; ++i) {
			cin >> str;   //输入字符串
			vs.push_back(str);
		}
		sort(vs.begin(), vs.end());  // 因为是字典,一定要排序!!
		cin >> word;   //输入要匹配的标志字符串
		cin >> index;   //输入要输出的第几个兄弟字符串
		int counts = 0;

		for (int i = 0; i < num; ++i) {
			if (isBrother(word, vs[i])) {
				counts++;
				if (counts == index)
					s = vs[i];
			}
		}
		if (!vs.empty())
			cout << counts << endl;
		if (counts >= index)
			cout << s << endl;

	}
	return 0;
}

2, table tennis basket

Topic Description:
nowcoder there are two boxes (A, B) table tennis, there are DHS, there are Yali Asia ...... now he needs to determine whether A box contains all the B-box type, and number of each lot of balls the number of B box, how to do it?

Thinking: This is a typical problem of map application, we first A, B two strings into the map, the map first parameter is the i-th bit of the string, the second argument is the i-th position appears number, then when the character i 'A' - 'Z' increases, if the A [i] <B [i ], returns No, out of the loop, if i has come to Z, then returns Yes
codes:

#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
	string str1,str2;
	while(cin>>str1>>str2)
	{
		map<char,int> m1;
		for(auto e1:str1)
		{
			m1[e1]++;
		}
		map<char,int> m2;
		for(auto e2:str2)
		{
			m2[e2]++;
		}
		for(int i='A';i<'Z';i++)
		{
			if(m1[i]<m2[i])
			{
				cout<<"No"<<endl;
				break;
			}
			if(i=='Z')
			{
				cout<<"Yes"<<endl;
			}
		}
	}
	sysem("pause");
	return 0;
}

3, camels nomenclature

Topic Description:
From the C / C ++ to Java programmers, most are not used to start a variable name is to change the way of. C-style use underscores to separate multiple words, such as "hello_world"; and apply the rules of Java is called camel nomenclature: In addition to the first word, the first letter of all words capitalized, for example, "helloWorld".
You help the poor programmers automatically convert variable name.
Title very simple, the code is also very simple, a for loop can handle, first find the '_' position, that is, the i-th bit, and then erase off, then the i-th bit string transferred to uppercase.
Code:

#include <iostream>
#include <stdlib.h>
#using namespace std;
int main()
{
	string str;
	while(cin>>str)
	{
		for(int i=0;i<str.size();i++)
		{
			if(str[i]=='_')
			{
				str.erase(i,1);
				str[i]-=32;
			}
		}
		cout<<str<<endl;
	}
}

Question welcome readers correct me! ! !

Published 33 original articles · won praise 13 · views 1045

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/104975868