Zerojudge 基礎題庫a009 解碼器


這題看起來好像很難,但它下面有兩個提示:
cin.getchar(char)
getline(cin,string)
一個是讀取一個字元,另一個則是一個字串
題目如下:
         1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5
>>>*CDC is the trademark of the Control Data Corporation.
         1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
>>>*IBM is a trademark of the International Business Machine Corporation.

可以發現到*變成1,i變p,等等...
去查一下ASCII,得知:
'*' => 42
'1' =>49

'i' =>105
'p' => 112
很明顯的,'*' + 7 = '1', 'i' + 7 = 'p'
找到規律了!但如何把字元變成數字,數字變回字元?

-------------------------------------------------------------------------------------

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()

{

    string question;

    getline(cin,question);

    cout << question;

}
-------------------------------------------------------------------------------------
這樣你輸入 How are you?
他會輸出 How are you?
-------------------------------------------------------------------------------------

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()

{

    char a;

    cin.get(a);

    cout << a;

}
-------------------------------------------------------------------------------------
這樣你輸入 How are you?
他會輸出 H (只有第一個字元)

認識工具後,來規劃一下吧。

1. 輸入字串
2. 字串切片
3. 字元轉換
4. 輸出

第一步:輸入字串

簡單,我們可以使用 getline.(cin , string); 來得到我們要的字串

1
2
string question;
getline(cin, question);

這樣就完成了

第二步:字串切片
這就有一個問題了,如何拿到字串裡的字元?
我們可以使用string.at(int)來得到字串索引值int的字元
像是:

1
2
string a = happy;
cout << a.at(2);
他會輸出 p

第三步:字元轉換

拿到了字元,如何得到他的ASCII碼?
可以使用 int(char) 來強制轉換字元成ASCII


1
cout << int(a);

他會輸出 97

接下來,依照前面的規律,把ASCII碼減7

最後使用 char(int) 來轉換數字成字元


1
cout << char(97);

得到的結果是 a

第四步:輸出
簡單,使用cout

全部統整在一起:
-------------------------------------------------------------------------------------

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main()
{
    string question;
    char notAns;
    int unAns;
    char Ans;

    getline(cin, question);
    notAns = question.at(0);
    unAns = int(notAns);
    Ans = char(unAns - 7);
    cout << Ans;
}
-------------------------------------------------------------------------------------
這樣我輸入 123時
他會輸出 *
但我要整句話
所以還要加for loop
-------------------------------------------------------------------------------------

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()
{
    string question;
    char notAns;
    int unAns;
    char Ans;

    getline(cin, question);

    for (int i = 0; i <= question.length()-1; i++)
    {
        notAns = question.at(i);
        unAns = int(notAns);
        Ans = char(unAns - 7);
        cout << Ans;
    }
}
-------------------------------------------------------------------------------------
這樣我輸入 1JKJ'
輸出 *CDC
別忘了加 while(){}
因為要一直不斷輸入
-------------------------------------------------------------------------------------

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
    string question;
    char notAns;
    int unAns;
    char Ans;

    while(getline(cin, question))
    {

        for (int i = 0; i <= question.length()-1; i++)
        {
            notAns = question.at(i);
            unAns = int(notAns);
            Ans = char(unAns - 7);
            cout << Ans;
        }
        cout << endl;
    }
}

-------------------------------------------------------------------------------------
完成啦,送進Zerojudge烤喔~
得到了不錯的回覆
a009. 解碼器 -- ACM 458 
AC (2ms, 328KB)

2019-11-30 16:06









留言

這個網誌中的熱門文章

Zerojudge 基礎題庫a004 文文的求婚 (Python)

紙蜻蜓的受風面積與紙蜻蜓落地時間的關係 #1 [實驗歷程與Python Matplotlib]

Zerojudge 基礎題庫a013 羅馬數字 (Python)