551 - I2P2013 Exercise Scoreboard

Time

2015/04/30 22:00:26 2015/04/30 22:00:26

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

5700 - mid2_Exercise1_矩陣相乘   

Description

輸入兩個矩陣
假設矩陣大小都不超過 10-by-10
底下是輸入格式範例:
2 3
2.0 3.0 2.0
4.0 5.0 6.0
3 4
5.0 2.0 4.0 2.0
3.0 1.0 2.0 3.0
2.0 4.0 6.0 4.0
前兩個數字 2 3 是第一個矩陣的大小
分別代表 2 rows 和 3 columns
接下來六個 double 數值就是第一個矩陣的六個元素
然後接著是 3 4 兩個數字
代表第二個矩陣的大小
接下來則是第二個矩陣的內容
總共包含十二個 double 元素
 
輸出兩個矩陣相乘的結果
23.00 15.00 26.00 21.00
47.00 37.00 62.00 47.00
 

底下是部分程式碼

#include 

int main(void)
{
    double A[10][10] = {0};
    double B[10][10] = {0};
    double C[10][10] = {0};
    int A_r, A_c, B_r, B_c;
    int i, j, k;

    scanf("%d%d", &A_r, &A_c);
    for (i=0; i        for (j=0; j            scanf("%lf", &A[i][j]);
      }
   }

   scanf("%d%d", &B_r, &B_c);
   for (i=0; i        for (j=0; j           scanf("%lf", &B[i][j]);
       }
   }

    /* ??? */

   for (i=0; i       for (j=0; j           printf("%5.2f ", C[i][j]);
      }
      printf(" ");
   }
   return 0;
}

Input

矩陣A的row 矩陣A的col

矩陣A的內容

矩陣B的row 矩陣B的col

矩陣B的內容

Output

A x B的內容

Sample Input  Download

Sample Output  Download

Tags




Discuss




5701 - mid2_Exercise2_大數加法   

Description

 

輸入由數字構成的兩個字串 (假設每個字串的長度不超過 100)
例如
1234567890987654321234567890987654321
9876543210123456789876543210123456789
 
輸出兩數相加的結果
11111111101111111111111111101111111110
 
基本做法是用陣列來表示很大的整數
先把資料當作字串來讀取
然後轉成整數陣列做計算
底下是部分的程式碼
請把 add() 的內容寫出來
讓它能達到題目所要的計算結果
 

#include  
#include  
#include  
#define END_INT 999
void string_to_int(char *str, int num[]);
void show_big_int(int *num);
void add(int num1[], int num2[], int num3[]);

int main(void)
{
    char str[100];
    int A[100] = {0, END_INT};
    int B[100] = {0, END_INT};
    int C[101] = {0, END_INT};
    FILE *fin;
    fin = fopen("big_int.txt", "r");
    if (fin == NULL) {
        perror("big_int.txt");
        exit(EXIT_FAILURE);
    }
    fscanf(fin, "%99s", str);
    string_to_int(str, A);
    fscanf(fin, "%99s", str);
    string_to_int(str, B);

    show_big_int(A);
    printf(" ");

    show_big_int(B);
    printf(" ");
    add(A, B, C);
    show_big_int(C);
    printf(" ");
        
    fclose(fin);
    return 0;
}

void string_to_int(char *str, int num[])
{
    int i;
    for (i=0; i 

 

Input

兩組由數字所構成且長度不超過100的字串

Output

相加後的結果

Sample Input  Download

Sample Output  Download

Tags




Discuss




5702 - mid2_Exercise3_Knight's Move   

Description

已知西洋棋規則中

騎士一共有如下圖的八種走法

 

給定兩個 8 x 8 棋盤上的點 (X1, Y1)、(X2, Y2)

請算出騎士從 (X1, Y1) 最少需要走幾步可以到 (X2, Y2)

 

註:
1. (X1, Y1) 一定走得到 (X2, Y2)
2. 從 (X1, Y1) 走到下一個點才算第一步,(X1, Y1) 自己本身不是第一步

 

hint:

#include 

#define INF 2147483647
#define SIZE 8

/*
騎士的八種走法
每一列代表一種走法 x 和 y 的位移量
*/
int Move[8][2] = {
     1, -2,
     2, -1,
     2,  1,
     1,  2,
    -1,  2,
    -2,  1,
    -2, -1,
    -1, -2
};

int X1, Y1;
int X2, Y2;
int min;

int valid(int board[SIZE][SIZE], int x, int y, int path);
void knightMove(int board[SIZE][SIZE], int x, int y, int step);

int main()
{
    /* board 記錄每一格走過了沒 */
    int board[SIZE][SIZE] = {0};

    while (scanf("%d %d %d %d", &X1, &Y1, &X2, &Y2) != EOF) {
        min = INF;

        if (X1==X2 && Y1==Y2) {
            printf("0\n");
        }
        else {
             /* 注意!!! board內的順序為Y、X */
             board[Y1][X1] = 1;
             knightMove(board, X1, Y1, 1);
             board[Y1][X1] = 0;

             printf("%d\n", min);
        }
    }

    return 0;
}

/*
判斷是否超出棋盤、是否已經走過
path 代表是第幾種走法
*/
int valid(int board[SIZE][SIZE], int x, int y, int path)
{
    int next_x, next_y;

    next_x = x + Move[path][0];
    next_y = y + Move[path][1];

    /*
        ???
    */
}

/* 傳入騎士現在的位置以及要尋找的是第幾步 */
void knightMove(int board[SIZE][SIZE], int x, int y, int step)
{
    int next_x, next_y;
    int i;

    if (step >= min) {
        return;
    }
    for (i = 0; i < 8; ++i) {
        if (valid(board, x, y, i) == 1) {
            /*
                ???
            */
        }
    }
}

Input

X1(1)  Y1(1)  X2(1)  Y2(1)

X1(2)  Y1(2)  X2(2)  Y2(2)

...

X1(N)  Y1(N)  X2(N)  Y2(N)

 

註: 0 <= X1(i), Y1(i), X2(i), Y2(i) < 8 , for i=1, 2, ... , N

N為不超過20的正整數

Output

步數1

步數2

...

步數N

 

註: 最後須加上換行

Sample Input  Download

Sample Output  Download

Tags




Discuss




5703 - mid2_Exercise4_Numbers Tower   

Description

有一座五層的數字塔,第一層有 1 個數字,第二層有 2 個數字,以此類推
整座塔呈現三角形,每個數字只和下一個距離最近的兩個數字有樓梯連結
而且只能往下走,如下圖:

    1
   4 6
  6 9 3
 6 3 7 1
2 6 3 1 8

第二層的 4 只可以走到第三層的 6、9
第二層的 6 只可以走到第三層的 9、3

從第一層開始往下走
要如何讓經過的數字加起來等於指定的值
並且經過的層數是最少的
請將層數印出

 

hint:

#include 

#define INF 2147483647
#define SIZE 5

int Value;    /* 指定的數 */
int NoSolution;
int min;

/* 傳入即將進入第幾層、第幾個位置以及目前為止的加總 */
void goThroughTower(int tower[SIZE][SIZE], int level, int index, int sum)
{
    if (level + 1 > min || level + 1 > SIZE) {
        return;
    }
    else {
        /*
            ???
        */
    }
}

int main()
{
    int tower[SIZE][SIZE];
    int i, j;

    while (scanf("%d", &Value) != EOF) {
        NoSolution = 1;
        min = INF;

        for (i = 0; i < SIZE; ++i) {
            for (j = 0; j <= i; ++j)
                scanf("%d", &tower[i][j]);
        }

        goThroughTower(tower, 0, 0, 0);

        if (NoSolution == 1)
            printf("No solution\n");
        else
            printf("%d\n", min);
    }

    return 0;
}

Input

指定的數1

1

...

指定的數N

N

 

註: 塔內的數以及指定的數均為 1~231-1 的正整數

N 為小於20的正整數

Output

層數1

...

層數N

 

註: 若無解則印 "No solution"

最後須加上換行

Sample Input  Download

Sample Output  Download

Tags




Discuss




5704 - mid2_Exercise5_Equivalent Sets   

Description

 

輸入兩個字串 (長度小於100 且只包含小寫英文字母)
判斷是否包含相同的字元集合
只是差別在排列不同
例如輸入是
abccb  和 bbcca
應該要輸出 Yes
因為只要經過重新排列之後兩個字串就會相等
 
如果輸入是
bbdef 和 defba
則輸出 No
因為無論如何排列
兩個字串還是不同

 

Input

輸入兩個字串 (長度小於100 且只包含小寫英文字母)

Output

當兩組字串相等印出Yes,
反之印出No。

Sample Input  Download

Sample Output  Download

Tags




Discuss




5705 - mid2_Exercise6_Sentence reversal   

Description

輸入一個英文句子字串
包含多個英文單字
每個英文單字之間用單個空白字元隔開
沒有其他標點符號
例如
this is a book
 
用程式將上面的句子以單字為單位反轉
輸出
book a is this
 
讀取輸入的字串可以用下面的方式
 
char a[100];
fgets(a, 99, stdin);
a[strlen(a)-1] = '\n';
 
不使用 scanf "%s" 讀取字串的原因是因為 scanf 讀到空白字元就會停止
用 fgets 則會讀到換行才停止
由於 fgets 會把換行字元也讀進來
所也要多一個步驟
a[strlen(a)-1] = '\n';
把換行字元去掉

Input

一串字串(由小寫字母組成且長度不超過100)

Output

反轉後結果

Sample Input  Download

Sample Output  Download

Tags




Discuss




5706 - mid2_Exercise7_多項式相加   

Description

假設有兩多項式
f(x) = A1xa1 + A2xa2 + A3xa3 + ...
g(x) = B1xb1 + B2xb2 + B3xb3 + ...
其中係數皆為整數, 次方皆為非負整數並且f的次方不重複且由小排到大, g的次方也不重複且由小排到大

則給定兩組係數序列{(A1,a1), (A2,a2), (A3,a3), ...}與{(B1,b1), (B2,b2), (B3,b3), ...}
和一整數n

求f(n) + g(n) = ?

例如

n = 12
{(3,0), (-4,3), (100,4), (-3000,65536)}與{(-12,2), (101,4), (3000,65536)}

會有

f(x) = 3x0 + (-4)x3 + 100x4 + (-3000)x65536
g(x) = (-12)x2 + 101x4 + 3000x65536

f(n) + g(n) = f(12) + g(12) = 4159299


底下是部分程式碼

#include 
#define MAX_TERM 100

int A[MAX_TERM], a[MAX_TERM];
int B[MAX_TERM], b[MAX_TERM];
int C[MAX_TERM], c[MAX_TERM];

int main()
{
    int i, j, k, n, result;
    int f_terms, g_terms, fg_terms;

    scanf("%d", &f_terms);
    for(i = 0; i < f_terms; i++){
        scanf("%d %d", &A[i], &a[i]);
    }
    scanf("%d", &g_terms);
    for(i = 0; i < g_terms; i++){
        scanf("%d %d", &B[i], &b[i]);
    }
    scanf("%d", &n);

    /* ??? */

    printf("%d", result);
    return 0;
}

 

Input

f(x)的項數

係數 次方

g(x)的項數

係數次方

n

Output

f(n) + g(n)

Sample Input  Download

Sample Output  Download

Tags




Discuss