24h購物| | PChome| 登入
2012-06-05 16:33:27| 人氣2,617| 回應0 | 上一篇 | 下一篇

[UVA][sstream使用] 482 - Permutation Arrays

推薦 0 收藏 0 轉貼0 訂閱站台

UVA


  Permutation Arrays 

In many computer problems, it is necessary to permute data arrays. That is, the data in an array must be re-arranged in some specified order. One way to permute arbitrary data arrays is to specify the permutations with an index array to point out the position of the elements in the new array. Let x be an array that is to be permuted and let x' be the permuted array. Then, we have the relationship between x and x' that x'pi = xi.

Input 

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

Each input set will contain two lines of numbers. The first line will be an index array p containing the integers 1...n, where n is the number of integers in the list. The numbers in the first line will have been permuted in some fashion. The second line will contain a list numbers in floating point format.

Output 

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

The output for this program will be the list of floating point numbers from the input set, ordered according to the permutation array from the input file. The output numbers must be printed one per line in the same format in which they each appeared in the input file.

Sample Input 

1

3 1 2
32.0 54.7 -2

Sample Output 

54.7
-2
32.0


原本使用 stringstream sin(line);
但是發現 sin.clear(); 會在下一次的 sin << line; 發生錯誤,
因此改成 兩次都是 sin << line;

#include <stdio.h>
#include <sstream>
#include <iostream>
using namespace std;

int main() {
    int t;
    string line;
    scanf("%d", &t);
    getchar();
    while(t--) {
        getchar();
        int idx[5000], n = 1, i = 1;
        string x[5000];
        getline(cin, line);
        stringstream sin;
        sin << line;
        while(sin >> idx[n])
            n++;
        getline(cin, line);
        sin.clear();
        sin << line;
        while(sin >> x[idx[i]])
            i++;
        for(i = 1; i < n; i++)
            cout << x[i] << endl;
        if(t)
            puts("");
    }
    return 0;
}

台長: Morris
人氣(2,617) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 146 - ID Codes
此分類上一篇:[UVA] 10260 - Soundex Time

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文