Codeforces 136A Presents 禮物

難易度 : 800

英文原文如下

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.

中文翻譯

小 Petya 非常喜歡禮物。最近他從他的母親那邊拿到了一個新的筆記型電腦作為他的新年禮物。他馬上就決定要把它送給別人,因為沒有什麼是比送別人禮物更快樂的。為此,他在自己的地方舉辦了一場新年派對,並邀請了 n 個朋友。

如果有一件事比收到禮物更讓小 Petya 高興的話,那就是看著別人給別人送禮物。因此,他安全地將筆記型電腦藏起來,直到下一個新年,並決定觀看他的朋友們交換禮物,而他自己不參與這個過程。他用整數從 1 到 n 為他的所有朋友編了號碼。小彼得記得朋友 i 給了朋友 pi一份禮物。他還記得他的每一個朋友都收到了一份禮物。

現在,小 Petya 想要知道對於每個朋友 i,誰給了他禮物。

輸入

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.

第一行包含一個整數 n(1 ≤ n ≤ 100) — Petya 邀請參加派對的朋友數量。第二行包含 n 個用空格分隔的整數:第 i 個數字是 pi — 給予朋友 i 禮物的朋友的編號。保證每個朋友都只收到一份禮物。可能有一些朋友不同意小 Petya 將禮物給別人的觀點。這些朋友把禮物送給了自己。

輸出

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

印出 n 個用空格隔開的整數,第 i 個數字代表了給第 i 個朋友禮物的朋友編號

範例

輸入4
2 3 4 1
輸出4 1 2 3
輸入3
1 3 2
輸出1 3 2
輸入2
1 2
輸出1 2

解題思路

C#解決方案

方案1

int n = int.Parse(Console.ReadLine());  //number of friends 

string[] resArr = new string[n];
string[]splitArr = Console.ReadLine().Split(' ');

for(int i=0; i<splitArr.Length;i++){
    var idx = int.Parse(splitArr[i]);
    
    resArr[idx-1] = (i+1).ToString();
}

string res = string.Join(" ", resArr);
Console.WriteLine(res);

這時候我們先將輸入的數字依照空白分開成陣列

並用一個長度相同的 resArr 來儲存結果

接著將輸入的陣列進行迴圈處理,輸入的是送給誰,所以反過來放就會變成是禮物是誰送的

也就是本題所需的,另外因為 index 是從0開始,但編號是從一開始的,故要 i+1

Java解決方案

方案1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());  //number of friends 
        
        String[] resArr = new String[n];
        String[]splitArr = scanner.nextLine().split(" ");

        for(int i=0; i<splitArr.length;i++){
            int idx = Integer.parseInt(splitArr[i]);
            
            resArr[idx-1] = Integer.toString(i+1);
        }
        
        String res = String.join(" ", resArr);
        System.out.println(res);
    }
}

結論

說起來,這次的主角也是挺奇怪的,邀請大家來交換禮物,自己卻不參加!!

還打算把筆記型電腦藏到下一年…

🧡如果文章有幫上你的忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Problem – 136A – Codeforces

一些其他的Codeforces文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *