12-Topic 1156: Who is your potential friend

Source: Internet
Author: User
Tags sort
http://ac.jobdu.com/problem.php?pid=1156 Title Description:

"SquarePants Underwater"-This is the word we like to use when we describe our friends. Two people are friends usually means that they have a lot of common interests. As an otaku, however, you find that you don't have a lot of opportunities to get to know each other. Fortunately, you accidentally got a book borrowing records from the Peking University Library, so you midnight oil to stay up late and program to find potential friends from it.
First of all, you have a record of the borrowing records, the N readers sequentially numbered,..., N, the M book is numbered,..., m. At the same time, according to the "SquarePants underwater" principle, and the person you like to read the same book, is your potential friend. Your task now is to calculate from this borrowing record how many potential friends each person has.
I wrote the algorithm too troublesome, I made a structure, representing the number and preference, and then sort, and then count the same number of each paragraph, and then the corresponding number of the new array output the same number ... As a result, the code of others is very simple, the book made an array, the preference as the index of the book array, traverse over, the same +1 can, and finally traverse the output.

Topic 1156: Who is your potential friend #include <stdio.h> #include <iostream> #include <fstream> #include <algorithm

> Using namespace std;
	typedef struct READER {int ID;
int prefer;

}reader; BOOL CMP (reader A, reader B) {return A.prefer < B.prefer;} int main () {int n, m, i;//n is the number of people per group of data, a total of M-book//ifstream CI
	N ("Data.txt");
		while (CIN >> n >> m) {Reader *reader = new Reader[n];  int *friend = new Int[n];
		Cannot use friend because friend function friend int count = 0;
			for (i = 0; i < n; i++) {reader[i].id = i;
		Cin >> Reader[i].prefer;   } sort (reader, reader + N, CMP);

		Add readers to your favorite books, such as 2,2,2,2,3,3,4,5,5 ....
					for (i = 0; i < n; i++) {if (Reader[i].prefer = = Reader[i + 1].prefer) {if (i = = n-1)//Process The last set of identical data
				for (int j = i; j >= I-count; j--) friend[reader[j].id] = count;
			else count++;
				} else {for (int j = i; j >= I-count; j--) friend[reader[j].id] = count;
			Count = 0; }} For (i = 0; i < n; i++) {if (friend[i] = = 0) cout << "Beiju" << Endl;
		else cout << Friend[i] << Endl;
	}}//system ("pause");
return 0; }


Optimization algorithm:
Topic 1156: Who are your potential friends
#include <stdio.h>  
#include <iostream> 
#include <fstream>
using namespace std;

int main ()
{
	int n, m;
	Ifstream cin ("Data.txt");
	while (CIN >> n >> m)
	{
		int *reader = new Int[n];
		int book[200] = {0};
		for (int i = 0; i < n; i++)
		{
			cin >> reader[i];
			book[reader[i]]++;
		}
		for (int i = 0; i < n; i++)
		{
			if (book[reader[i]] = = 1)
				cout << "Beiju" << Endl;
			else
				cout << Book[reader[i]]-1 << endl;
		}
	}
	System ("pause");
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.