Codeforces 61A Ultra-Fast Mathematician 非常快的數學家

難易度 : 800

英文原文如下

Shapur was an extremely gifted student. He was great at everything including Combinatorics, Algebra, Number Theory, Geometry, Calculus, etc. He was not only smart but extraordinarily fast! He could manage to sum 1018 numbers in a single second.

One day in 230 AD Shapur was trying to find out if any one can possibly do calculations faster than him. As a result he made a very great contest and asked every one to come and take part.

In his contest he gave the contestants many different pairs of numbers. Each number is made from digits 0 or 1. The contestants should write a new number corresponding to the given pair of numbers. The rule is simple: The i-th digit of the answer is 1 if and only if the i-th digit of the two given numbers differ. In the other case the i-th digit of the answer is 0.

Shapur made many numbers and first tried his own speed. He saw that he can perform these operations on numbers of length ∞ (length of a number is number of digits in it) in a glance! He always gives correct answers so he expects the contestants to give correct answers, too. He is a good fellow so he won’t give anyone very big numbers and he always gives one person numbers of same length.

Now you are going to take part in Shapur’s contest. See if you are faster and more accurate.

中文翻譯


Shapur是一位極具天賦的學生。他在各方面都非常優秀,包括組合數學、代數、數論、幾何、微積分等等。他不僅聰明,而且極為迅速!他能在一秒內完成將1018個數字相加的任務。

在公元230年的某一天,Shapur試圖找出是否有人可以在計算方面超越他。因此,他舉辦了一場盛大的比賽,邀請所有人參加。

在比賽中,他給予參賽者許多不同的數字對。每個數字由數字 0 或 1 組成。參賽者必須根據給定的數字對寫下一個新數字。規則很簡單:答案的第i位數字僅在兩個給定數字的第 i 位數字不同的情況下為 1。在其他情況下,答案的第 i 位數字為0。

Shapur創造了許多數字,首先測試了自己的速度。他發現他可以在一瞬間完成對無窮長度的數字進行這些操作!他總是給出正確的答案,因此他期望參賽者也能給出正確的答案。他是一個好人,所以他不會給任何人非常大的數字,而且他總是給一個人相同長度的數字。

現在你要去參加Shapur的比賽了。看看你是否更快且更準確。

輸入

There are two lines in each input. Each of them contains a single number. It is guaranteed that the numbers are made from 0 and 1 only and that their length is same. The numbers may start with 0. The length of each number doesn’t exceed 100.

每一個輸入都有兩行,每一行都包含了一個單獨的數字。數字保證由 0 跟 1 組成而且他們的長度是相同的。數字可能從0開始,而且長度不會超過 100。

輸出

Write one line — the corresponding answer. Do not omit the leading 0s.

印出一行 — 相對的答案。不要忽略任何前導的 0。

範例

輸入1010100
0100101
輸出1110001
輸入000
111
輸出111
輸入1110
1010
輸出0100

解題思路

不同的情況為 1 ,相同為 0,這不就是二進位中的XOR計算嗎~

C#解決方案

方案1

using System.Text;

char[] arr1 = Console.ReadLine().ToCharArray();
char[] arr2 = Console.ReadLine().ToCharArray();
var result = new StringBuilder();

for(int i=0; i<arr1.Length; i++){
    result.Append(arr1[i]==arr2[i] ? '0' : '1');
}

Console.WriteLine(result.ToString());

為了加快速度,這邊用了兩個小技巧

第一,是用 StringBuilder來加快速度以及減少記憶體消耗 (String重複黏貼會開新的物件)

第二,則是不轉型為 int 來判斷,直接判斷兩個 char 是否相等就好

Java解決方案

方案1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        char[] arr1 = scanner.nextLine().toCharArray();
        char[] arr2 = scanner.nextLine().toCharArray();
        StringBuilder result = new StringBuilder();
        
        for(int i=0; i<arr1.length; i++){
            result.append(arr1[i]==arr2[i]? '0' : '1');
        }
        
        System.out.println(result.toString());
    }
}

結論

這裡看二進位的進階應用 ➡ 從實務應用出發 – 如何透過二進位控管人員權限

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

🧡分享文章或是留一個言,那都是對我的支持

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

題目連結 : Problem – 61A – Codeforces

一些其他的Codeforces文章

發佈留言

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