1.23 A

A - 1

Time limit 1000 ms
Memory limit 262144 kB

Problem Description

Manao works on a sports TV. He’s spent much time watching the football games of some country. After a while he began to notice different patterns. For example, each team has two sets of uniforms: home uniform and guest uniform. When a team plays a game at home, the players put on the home uniform. When a team plays as a guest on somebody else’s stadium, the players put on the guest uniform. The only exception to that rule is: when the home uniform color of the host team matches the guests’ uniform, the host team puts on its guest uniform as well. For each team the color of the home and guest uniform is different.

There are n teams taking part in the national championship. The championship consists of n·(n - 1) games: each team invites each other team to its stadium. At this point Manao wondered: how many times during the championship is a host team going to put on the guest uniform? Note that the order of the games does not affect this number.

You know the colors of the home and guest uniform for each team. For simplicity, the colors are numbered by integers in such a way that no two distinct colors have the same number. Help Manao find the answer to his question.

Input

The first line contains an integer n (2 ≤ n ≤ 30). Each of the following n lines contains a pair of distinct space-separated integers hi, ai (1 ≤ hi, ai ≤ 100) — the colors of the i-th team’s home and guest uniforms, respectively.

Output

In a single line print the number of games where the host team is going to play in the guest uniform.

Sample Input

3
1 2
2 4
3 4

4
100 42
42 100
5 42
100 5

2
1 2
1 2

Sample Output

1

5

0

问题链接:A - 1

问题简述:

输入n代表有n只队伍,输入n行,每行输入一对数h[i]和a[i]分别代表主场和客场的衣服颜色,问在冠军赛期间(有n*(n-1)场比赛),东道主队要穿多少次宾客制服?

问题分析:

我们来看sample input
3
1 2
2 4
3 4
姑且当为甲乙丙三队,甲会邀请乙丙打比赛,同理乙丙也会,东道主队要穿多少次宾客制服指的是在主场作战的情况下穿宾客制服,也就是问甲乙丙在主场作战中穿宾客制服的总和,用两个数组a[]和b[]分别放主场制服和宾客制服,让a[]扫过b[],若相同则让计数器sum自增,最后输出sum即可。

程序说明:

两个for语句,外循环放a[],内循环放b[]。

AC通过的C语言程序如下:

#include <iostream>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int a[1000];
	int b[1000];
	int sum = 0;
	for (int i = 1;i <= n;i++)
	{
		cin >> a[i];
		cin >> b[i];
	}
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= n;j++)
		{
			if (a[i] == b[j])
			{
				sum++;
			}
		}
	}
	cout << sum;

}

猜你喜欢

转载自blog.csdn.net/weixin_44003969/article/details/86609847