Selected examples of ladder training (2)

L1-020 Too handsome to have any friends

When all living beings are busy posting photos in the circle of friends, there are always some people who have no friends because they are too handsome. This question asks you to find those handsome people who have no friends.

Input format:

Enter a positive integer N(≤100) in the first line of input, which is the number of known circles of friends; in the subsequent Nlines, each line first gives a positive integer K(≤1000), which is the number of people in the circle of friends, and then lists a Everyone in the circle of friends - for convenience, each person corresponds to an ID number, which is 5 digits (from 00000 to 99999), and the IDs are separated by spaces; after that, a positive integer M(≤10000) is given to be queried The number of people; the next line lists Mthe IDs to be queried, separated by spaces.

Note: A person who has no friends can be a person who has not installed "Moments" at all, or a person who is only in Moments by himself. Although some narcissists will repeatedly add themselves to the circle of friends, the title guarantees that there Kare at least 2 different people in all circles of friends with more than 1.

Output format:

Output those handsome people who have no friends in the order of input. IDs are separated by 1 space, and there must be no extra spaces at the beginning and end of the line. Output if no one is too handsome No one is handsome.

Note: The same person can be queried multiple times, but only output once.

Input sample 1:

3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
8
55555 44444 10000 88888 22222 11111 23333 88888

Output sample 1:

10000 88888 23333

Input sample 2:

3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
4
55555 44444 22222 11111

Output sample 2:

No one is handsome

error-prone

It may ignore the situation that only one person is in the circle of friends, and the constraint condition of 5-digit id may be ignored when outputting

the code

#include<iostream>
#include<unordered_set>
#include <iomanip>
using namespace std;
int main() {
    unordered_set<int>rank;
    int n, m;
    int check, sum = 0;
    int num;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> m;
        if(m>=2)
       {
        for (int j = 0; j < m; j++)
        {
            cin >> num;
            rank.emplace(num);
        }
       }
        else
        {
            cin>>num;
        }
    }
    cin >> check;
    for (int i = 0; i < check ; i++)
    {
        cin >> num;
        if (rank.find(num) == rank.end())
        {
            if (sum != 0)cout << " ";
            rank.emplace(num);
            sum++;
             cout << setw(5) << setfill('0') << num;
        }
    }
    if (sum == 0)
        cout << "No one is handsome";
}

 L1-025 positive integer A+B

The goal of the question is very simple, which is to find the sum of two positive integers A, Bwhere Aboth Bsums are in the interval [1,1000]. Slightly troublesome, the input is not guaranteed to be two positive integers.

Input format:

Input gives Aand on one line Bseparated by spaces. The problem is Athat Bthe sum is not necessarily a positive integer that meets the requirements. Sometimes it may be a number that is out of range, a negative number, a real number with a decimal point, or even a bunch of garbled characters.

Note: We regard the first space that appears in the input as the separation of Aand B. The title guarantees at least one space and Bnot an empty string.

Output format:

If the input is indeed two positive integers, the A + B = 和output is formatted. If an input does not meet the requirements, it is output at the corresponding position ?, obviously at this time and also ?.

Input sample 1:

123 456

Output sample 1:

123 + 456 = 579

Input sample 2:

22. 18

Output sample 2:

? + 18 = ?

Input sample 3:

-100 blabla bla...33

Output sample 3:

? + ? = ?

core ideas;

Separate array A and array B, and judge whether they meet the requirements

the code

#include<iostream>    
#include<iomanip>
#include<string>
using namespace std;
int main()
{
	string ab;
	getline(cin, ab);
	string a, b;
	for (int i = 0; i < ab.length(); i++)
	{
		if (ab[i] == ' ')
		{
			break;
		}
		a = a + ab[i];
	}
	for (int i = a.length()+1; i < ab.length(); i++)
	{
		b =b+ ab[i];
	}
	for (int i = 0; i < a.length(); i++)
	{
		if (!isdigit(a[i]) || stoi(a) <= 0 || stoi(a) > 1000)
		{
			a = "?";
			break;
		}
	}
	for (int i = 0; i < b.length(); i++)
	{
		if (!isdigit(b[i]) || stoi(b) <= 0 || stoi(b) > 1000)
		{
			b = "?";
			break;
		}
	}
	if (a == "?" || b == "?")
	{
		cout << a << " + " << b << " = ?";
	}
	else
		cout << a << " + " << b << " = "<<stoi(a)+stoi(b);
}

L1-030 Gang of One 

"One-to-one study group" is a common learning organization method in primary and secondary schools. Teachers arrange students with high academic performance and students with low academic performance in a group. For this question, please write a program to help the teacher complete the assignment automatically, that is, after getting the ranking of the students in the class, among the students who have not yet been grouped, divide the students with the highest rank and the students of the opposite sex with the lowest rank into one group Group.

Input format:

Enter the first line to give a positive even number N(≤50), the number of students in the class. In the next Nline, give each student's gender (0 for girls, 1 for boys) and name (a non-empty string of no more than 8 English letters) in order of ranking from high to low, separated by a space. It is guaranteed that the ratio of male to female in this class is 1:1, and there is no tie for ranking.

Output format:

Output a set of two student names per line, separated by 1 space. Students with higher rankings are at the front and students with lower rankings are at the back. The output order of the group is arranged according to the ranking of the previous students from high to low.

Input sample:

8
0 Amy
1 Tom
1 Bill
0 Cindy
0 Maya
1 John
1 Jack
0 Linda

Sample output:

Amy Jack
Tom Linda
Bill Maya
Cindy John

the code

#include<iostream>
#include<iomanip>
#include<vector>
using namespace std;
struct stu {
    int sex;
    string name;
};
int main() 
{
    vector<stu> rank;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        stu x;
        cin >> x.sex >> x.name;
        rank.emplace_back(x);
    }
    for (int i = 0; i < rank.size(); i++)
    {
        for (int j = rank.size() - 1; j> 0; j--)
        {
            if (rank[i].sex != rank[j].sex)
            {
                cout << rank[i].name << " " << rank[j].name<<endl;
                rank.erase(rank.begin() + j);
                break;
            }

        }
    }

}

 

Guess you like

Origin blog.csdn.net/m0_63024355/article/details/129885186
Recommended