24h購物| | PChome| 登入
2013-06-28 09:22:36| 人氣987| 回應0 | 上一篇 | 下一篇

[UVA] 394 - Mapmaker

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


 Mapmaker 

The Cybersoft Computer Company (a leader in programming languages) has hired you to work on a new programming language named A--. Your task is to work on the array mapping tasks of the language. You will take an array reference such as x[5,6] and map it to an actual physical address. In preparation for doing this, you will write a program that will read in several array declarations and references and give the physical address of each reference. The physical address output by the program should be an integer number in base 10.

The physical address of an array reference tex2html_wrap_inline34 is calculated from the formula tex2html_wrap_inline36 , where the constants tex2html_wrap_inline38 are calculated as specified below.

tabular20

Input

The first line of the input file contains two positive integers. The first integer specifies N, the number of arrays defined in the data file, and the second integer specifies R, the number of array references for which addresses should be calculated. The next N lines each define an array, one per line, and the following R lines contain one array reference per line for which an address should be calculated.

Each line which defines an array contains, in the following order, the name of the array (which is limited to 10 characters), a positive integer which specifies the base address of the array, a positive integer which specifies the size in bytes of each array element, and D, the number of dimensions in the array (no array will have fewer than 1 or more than 10 dimensions). This is followed on the same line by D pairs of integers which represent the lower and upper bounds, respectively, of dimensions tex2html_wrap_inline90 of the array.

Each line which specifies an array reference contains the name of the array followed by the integer indexes tex2html_wrap_inline92 where D is the dimension of the array.

Output

The output file should contain the array references and the physical addresses. There should be one array reference and physical address per line. The formatting guidelines below must be adhered to.

For each line of output:

  1. Output the name of the array
  2. Output a left square bracket
  3. Output each index value (each pair of indexes should have a single comma and space between them)
  4. Output a right square bracket, a space, an equal sign, and another space
  5. Output the physical address

Sample Input

3 4
ONE       1500 2 2 0 3 1 5
TWO       2000 4 3 1 4 0 5 5 10
THREE     3000 1 1 1 9
ONE       2 4
THREE     7
TWO       2 0 6
TWO       3 3 9

Sample Output

ONE[2, 4] = 1526
THREE[7] = 3006
TWO[2, 0, 6] = 2148
TWO[3, 3, 9] = 2376




題目描述:

事實上是希望算出記憶體的位址, 宣告一個多維陣列, 採用 row major 的方式去計算。
會告訴你每一維度的上下限, 每個元素的 bytes 數, 以及 base address

#include <stdio.h>
#include <map>
#include <iostream>
using namespace std;
struct info {
    int B, D, Cd;
    int L[15], U[15];
    int C[15];
};
int main() {
    int n, m, i, j;
    while(scanf("%d %d", &n, &m) == 2) {
        map<string, info> R;
        char s[50];
        info E;
        for(i = 0; i < n; i++) {
            scanf("%s", s);
            scanf("%d %d", &E.B, &E.Cd);
            scanf("%d", &E.D);
            for(j = 0; j < E.D; j++)
                scanf("%d %d", &E.L[j], &E.U[j]);

            E.C[E.D-1] = 1;
            for(j = E.D-2; j >= 0; j--)
                E.C[j] = E.C[j+1]*(E.U[j+1]-E.L[j+1]+1);
            R[s] = E;
        }
        while(m--) {
            scanf("%s", s);
            info &e = R[s];
            int dim = e.D, ret = e.B, x;
            printf("%s[", s);
            for(i = 0; i < dim; i++) {
                scanf("%d", &x);
                if(i)   printf(", ");
                printf("%d", x);
                ret += e.C[i]*(x-e.L[i])*e.Cd;
            }
            printf("] = %d\n", ret);

        }
    }
    return 0;
}
/*
3 4
ONE       1500 2 2 0 3 1 5
TWO       2000 4 3 1 4 0 5 5 10
THREE     3000 1 1 1 9
ONE       2 4
THREE     7
TWO       2 0 6
TWO       3 3 9
*/

台長: Morris
人氣(987) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][重心] 651 - Deck
此分類上一篇:[UVA] 10920 - Spiral Tap

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