PTA L1-020 handsome to no friends in C ++

Handsome to no friends

When blues busy send photos in my circle of friends, there are always some people because of handsome and no friends. This question requires you to identify those handsome to no friends.
Input format:
input of the first row is given a positive integer N (≦ 100), is the number of known friends circle; then N rows, each first row is given a positive integer K (≤1000), as a circle of friends number, then all of a circle of friends lists - for convenience, each corresponding to an ID number, digits 5, between the partition ID (from 00,000 to 99,999) by a space; after a given positive integer M ( ≤10000), as the number of people to be queried; then listed row of M ID to be queried, separated by a space.
Note: no friends who can simply not install the "circle of friends", it can be only one person in their own circle of friends. Although there are individual narcissists will own their own circle of friends repeatedly added, but the problem to ensure that all K more than one circle of friends has at least two different people.
Output formats:
sequentially input to the output of those who did not handsome friends. With a space between the partition ID, and last row may not have extra space. If no one is handsome, the output No one is handsome.
Note: The same person can be queried several times, but only once output.
Sample Input 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

Sample Input 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

Talk is cheap. Show me the code.

#include<iostream>
#include<set>

using namespace std;
int main()
{    
	int n=0;    
	cin>>n;    
	set<int> no_handsome;    
	for(int i=0;i<n;i++)    
	{        
		int k=0;        
		cin>>k;        
		for(int j=0;j<k;j++)        
		{            
			int temp=0;            
			cin>>temp;            
			if(k>1)            
			{                
				no_handsome.insert(temp);            
			}           
		}    
	}    
	int m=0;    
	cin>>m;     
	set<int> handsome;    
	bool flag=true;    
	for(int i=0;i<m;i++)    
	{        
		int temp;        
		cin>>temp;        
		if(no_handsome.find(temp)==no_handsome.end())        
		{            
			if(handsome.find(temp)==handsome.end())            
			{                
				if(!flag)                
				{                    
					cout<<" ";                
				}                
				printf("%05d",temp);                
				flag=false;            
			}            
			handsome.insert(temp);        
		}    
	}    
	if(handsome.size()==0)    
	{        
		cout<<"No one is handsome";    
	}
} 

Problem-solving ideas

Under what conditions is handsome to find no friends is very important, you can also use reverse thinking. Can know from the title, no friends circle or circle of friends only he is no friend of the handsome man, and input to ensure that the circle is larger than the number of friends who must have two different people at 1, so the number of friends is to have more than 1 circle friends or people say wonderful person, so we just put these people into no_handsome can be. Note: The circle is equal to the number of friends still want to receive this individual is not just down 1:00 into no_handsome in. Who then check the input which is not in no_handsome is handsome to no friends who are output every time, we should also handsome to no friends of the people to organize themselves into handsome, before each output to check whether output over. If no one inside it handsome Output: No one is handsome.

Part of the code interpretation

1.

if(handsome.find(temp)==handsome.end())            
{                
	if(!flag)                
	{                    
		cout<<" ";                
	}                
	printf("%05d",temp);                
	flag=false;            
}            

Before each output to check whether the output too, and pay attention to control the space I have here is not output spaces before the first output, all subsequent output.

Error-prone point

1.

Sure enough, use an int is not easy, and some numbers in front of 0 output, we must add 0. With a string would not have trouble. Printf format control personally feel that in terms of convenience and decimals little more than cout.

Test Results

Here Insert Picture Description

Published 21 original articles · won praise 1 · views 162

Guess you like

Origin blog.csdn.net/weixin_43646424/article/details/104450081