10096 - moocHW3c   

Description

延續第二題,假設輸入的對應關係,有可能會出錯的一定只會是前兩個對應。譬如輸入是
EF+DE=GH
D 2
E 3
F 6
G 4
H 7
#
我們知道代入編碼之後等式不會成立,而假設錯誤的對應,只可能是題目所提供的前兩個對應關係,在這個例子中就是 D和 E,重新嘗試之後,我們可以是出來 D 應該設為 3 而 E 應該設為 1,請將最後的對應關係印出來,以這個例子來說,要顯示
 -1 -1 -1  3  1  6  4  7 -1 -1
假設測試資料一定會有解,而且只有唯一解。
如果要使用我們提供的程式架構,要寫出 void try_try(void); 其中會利用到第二題的 convert_all()。
另外也要稍微修改 read_mapping()。
程式架構如下:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
char Num_Str1[10];
char Num_Str2[10];
char Num_Str3[10];
int Num1=0, Num2=0, Num3=0;
int Mapping[10];
/* Store the mapping of A at location 0;
Store the mapping of B at location 1; ...
Store the mapping of J at location 9
For example, if we have the following mapping:
A->5, B->6, C->7, D->4, E->3, F->0, G->2, H->1, I->9, J->8
the elements of Mapping[] will be
{5, 6, 7, 4, 3, 0, 2, 1, 9, 8}
*/
int First_Map, Second_Map, num_map=1;

void read_equation(void);
void read_mapping(void);
void convert_all(void);

void try_try(void);

void show(void);

int main(void)
{
    //freopen("input", "r",stdin);
    read_equation();
    read_mapping();
    try_try();
    show();

    return 0;
}
void read_equation(void)
{
    char cur_ch, str[10];
    int i, len=0;
    while(cur_ch=getchar()){
        if(cur_ch=='+'){
            for(i=0; i<len; i++){
                Num_Str1[i] = str[i];
            }
            Num_Str1[i] = '\0'; len=0;
        }
        else if(cur_ch=='='){
             for(i=0; i<len; i++){
                Num_Str2[i] = str[i];
            }
            Num_Str2[i] = '\0'; len=0;
        }
        else if(cur_ch=='\n'){
            for(i=0; i<len; i++){
                Num_Str3[i] = str[i];
            }
            Num_Str3[i] = '\0'; len=0;
            break;
        }
        else{
            str[len] = cur_ch;
            len++;
        }
    }
}
void read_mapping(void)
{
    // Hint: refer to HW3b and modify by yourself
}

/*
Convert the encrypted numbers into the corresponding values (decimal numbers).
Store the results in the global variables Num1, Num2, Num3.
For example, if the elements of Mapping[] are
  5  6  7  4 -1 -1 -1 -1 -1  3
and the elements of the three character arrays are
Num_Str1: 'A','B','C','\0'
Num_Str2: 'C','B','\0'
Num_Str3: 'B','D','J','\0'
then, the results of Num1, Num2, and Num3 should be 567, 76, and 643.
*/
void convert_all(void)
{
    /*
    Hint:
    Check the characters stored in Num_Str1.
    Use Mapping[] to find the corresponding values and store the results
    in Num1, Num2, and Num3.
    */
}

/*
Show
 */
void show(void)
{
    int i;
    for (i=0; i<10; i++) {
        printf("%3d", Mapping[i]);
    }
    printf("\n");
}

 

Input

輸入格式和第一題完全相同。

Output

總共要輸出一行,結尾要換行。顯示的則是 A 到 J 對應的數值,如果沒有指定對應數值則預設值是 -1。注意,輸出格式是 %3d,也就是說,如果是 0 到 9 的數字,前面會空兩個空格,如果是 -1 則前面只有一個空格。

Sample Input  Download

Sample Output  Download

Tags




Discuss