Codeforces 734A Anton 跟 Danik 翻譯與解答

難易度 : 800

英文原文如下

Anton likes to play chess, and so does his friend Danik.

Once they have played n games in a row. For each game it’s known who was the winner — Anton or Danik. None of the games ended with a tie.

Now Anton wonders, who won more games, he or Danik? Help him determine this.

中文翻譯

Anton 喜歡玩西洋棋,他的朋友 Danik 也喜歡。

有一天他們連續對局了 n 場,每一場對局勝利者都是知道的 – Anton 或是 Danik其中一個,沒有一個對局是平手的。

現在 Anton 有點好奇誰贏了更多場對局,他還是 Danik? 幫助他確認這件事。

輸入

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of games played.

The second line contains a string s, consisting of n uppercase English letters ‘A’ and ‘D’ — the outcome of each of the games. The i-th character of the string is equal to ‘A’ if the Anton won the i-th game and ‘D’ if Danik won the i-th game.

輸入的第一行包含了一個整數 n (1 ≤ n ≤ 100 000) — 他們玩了幾局遊戲。

第二行包含了一個字串 s ,由 n 個大寫字母 ‘A’ 以及’D’所組成 — 代表著每一局的結果。  字串第 i 位的字元為 ‘A’ 代表 Anton 贏了, ‘D’ 則為 Danik 贏了。

輸出

If Anton won more games than Danik, print “Anton” (without quotes) in the only line of the output.

If Danik won more games than Anton, print “Danik” (without quotes) in the only line of the output.

If Anton and Danik won the same number of games, print “Friendship” (without quotes).

如果 Anton 比 Danik 贏了更多場,印出 “Anton” (不須雙引號)

Danik 贏更多場,則印出 “Danik” (不須雙引號)

如果兩個人贏的場數一樣,印出 “Friendship”

範例

輸入6
ADAAAA
輸出Anton
輸入7
DDDAADA
輸出Danik
輸入6
DADADA
輸出Friendship

解題思路

相信大家看到這一題,應該就會想到用一個簡單的迴圈(對字串)

然後兩個計數器來計算出現數量對吧,那讓我們來實作看看

C#解決方案

方案1

int n = int.Parse(Console.ReadLine());
string text = Console.ReadLine();

int aCnt = 0;
int dCnt = 0;

foreach(var c in text){
    if(c=='A')
    {
        aCnt+=1;
    }
    else
    {
        dCnt+=1;
    }
    
}

string res = "Friendship";

if(aCnt>dCnt){
    res = "Anton";
}
else if(dCnt>aCnt)
{
    res = "Danik";
}

Console.WriteLine(res);

方案2 – 使用一個變數(計數器)

int n = int.Parse(Console.ReadLine());
string text = Console.ReadLine();

int ans = 0;

foreach(var c in text){
    if(c=='A')
    {
        ans-=1;
    }
    else
    {
        ans+=1;
    }
    
}

string res = "Friendship";

if(ans<0){
    res = "Anton";
}
else if(ans>0)
{
    res = "Danik";
}

Console.WriteLine(res);

跟第一個解答比起來,減少了一個計數器,變成共用的

Java解決方案

方案1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String n = scanner.nextLine(); //no use    
        String text = scanner.nextLine();
        int ans = 0;
        
        for (char c : text.toCharArray()) {
            if(c=='A')
            {
                ans-=1;
            }
            else
            {
                ans+=1;
            }
        }
        
        String res = "Friendship";

        if(ans<0){
            res = "Anton";
        }
        else if(ans>0)
        {
            res = "Danik";
        }
        
        System.out.print(res);
        
    }
}

Python3 解決方案

方案1

n = int(input())
text = input()

ans = 0

for c in text:
    if(c=='A'):
        ans-=1
    else:
        ans+=1

res = "Friendship"

if(ans<0):
    res = "Anton"
    
elif(ans>0):
    res = "Danik"

print(res)

JavaScript 解決方案

方案1

var n = parseInt(readline());
var text = readline();

var ans = 0

for(var c of text) {
    if(c=='A'){
        ans-=1;
    }
    else{
        ans+=1;
    }
}

res = "Friendship";

if(ans<0){
    res = "Anton";
}
    
else if(ans>0){
    res = "Danik";
}

print(res);

結論

在這一題中,其實我的解法都沒用到第一個輸入,也就是對局數量

不知道是不是漏掉了甚麼地方…

另外,感覺用C#的Linq 應該也可以簡單的判斷數量呢,之後再補個好了!!

🧡如果文章有幫上你的忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Problem – 734A – Codeforces

一些其他的Codeforces文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *