CodeForces-136A-Presents

A. Presents
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what can be more pleasant than giving somebody gifts. And on this occasion he organized a New Year party at his place and invited n his friends there.

If there's one thing Petya likes more that receiving gifts, that's watching others giving gifts to somebody else. Thus, he safely hid the laptop until the next New Year and made up his mind to watch his friends exchanging gifts while he does not participate in the process. He numbered all his friends with integers from 1 to n. Petya remembered that a friend number i gave a gift to a friend number pi. He also remembered that each of his friends received exactly one gift.

Now Petya wants to know for each friend i the number of a friend who has given him a gift.

Input

The first line contains one integer n (1 ≤ n ≤ 100) — the quantity of friends Petya invited to the party. The second line contains n space-separated integers: the i-th number is pi — the number of a friend who gave a gift to friend number i. It is guaranteed that each friend received exactly one gift. It is possible that some friends do not share Petya's ideas of giving gifts to somebody else. Those friends gave the gifts to themselves.

Output

Print n space-separated integers: the i-th number should equal the number of the friend who gave a gift to friend number i.

Sample test(s)
input
4
2 3 4 1
output
4 1 2 3
input
3
1 3 2
output
1 3 2
input
2
1 2
output
1 2
初始代碼思路:
通過建立兩個數組,一個數組存儲輸入數據,另一個數組將下標與對應元素數值調換後再次存入其中,然後再次輸出值,需要三個循環。較爲浪費時間。
#include<iostream>

using namespace std;

int main()
{
	int n;
	int x[100], y[100];
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> x[i];
	}
	for (int i = 0; i < n; i++){
		y[x[i]-1] = i;
	}
	for (int i = 0; i < n; i++){
		cout << y[i]+1 << " ";
	}


	return 0;
}
改進:略過第一個輸入循環,在第一個循環內完成原本前兩個循環內需要去完成的工作,抽象爲兩排數據的指向問題,先讀入指向該數據的數據值,再將其標記好對應位置。
#include<iostream>

using namespace std;

int main()
{
	int a[100], n, k;
	cin >> n;
	for (int i = 1; i <= 4; i++){
		cin >> k;
		a[k] = i;
	}
	for (int i = 1; i <= 4; i++){
		cout << a[i] << " ";
	}
	system("pause");
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章