EngTW / English-for-Programmers

《程式英文》:用英文提昇程式可讀性
971 stars 45 forks source link

1281. Subtract the Product and Sum of Digits of an Integer #79

Closed twy30 closed 3 years ago

twy30 commented 3 years ago

https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/

using System;

public class Solution
{
    public int SubtractProductAndSum(int n)
    {
        // 「輸入的數字」
        var inputNumber = n;

        // 「『位數(複數)』的積」
        var productOfDigits = 1;

        // 「『位數(複數)』的和」
        var sumOfDigits = 0;

        while (inputNumber > 0)
        {
            // 「個位數」
            int onesDigit;

            inputNumber = Math.DivRem(inputNumber, 10, out onesDigit);

            productOfDigits *= onesDigit;
            sumOfDigits += onesDigit;
        }

        return productOfDigits - sumOfDigits;
    }
}

參考資料

位數

https://en.wikipedia.org/wiki/Numerical_digit#Computation_of_place_values

例如:

底數(radix) 、基數(base)

https://en.wikipedia.org/wiki/Radix


請參考「刷 LeetCode 練習命名」 https://github.com/EngTW/English-for-Programmers/issues/69 😊

LPenny-github commented 3 years ago

@twy30 大大,再度麻煩你給予命名建議囉 orz 十分感謝。

(Linq 蠻長的,我不確定怎麼排版會比較好看 🤔 傷眼睛的部分,先說聲抱歉 orz)

using System;
using System.Linq;

public class Solution {
    public int SubtractProductAndSum(int n) {

       int input = n;

       int[] inputDigitsArray = input.ToString()
                                     .ToCharArray()
                                     .Select(digit => Convert.ToInt32(digit.ToString()))
                                     .ToArray();

       int productOfInputDigitsArray = inputDigitsArray.Aggregate(
                                                         (result, digit)=> result * digit);

       int sumOfInputDigitsArray = inputDigitsArray.Sum();

       return productOfInputDigitsArray - sumOfInputDigitsArray;
    }
}
twy30 commented 3 years ago

@LPenny-github 不客氣 😊

(Linq 蠻長的,我不確定怎麼排版會比較好看 🤔 傷眼睛的部分,先說聲抱歉 orz)

沒問題 😊 就練習命名來說,我覺得你的排版還好。


       int[] inputDigitsArray = input.ToString()
       int productOfInputDigitsArray = inputDigitsArray.Aggregate(
       int sumOfInputDigitsArray = inputDigitsArray.Sum();

這裡可以試試把 Array 拿掉,變成

以這個案例的情況來說, 如果 除了 inputDigitsArray 外還有 inputDigitsSet, inputDigitsDictionary; 那麼,我覺得在 inputDigits 後附上資料結構名稱會是好的。

反過來說,因為就只有一個 inputDigits ,我覺得可以選擇不加上 Array ,讓每個變數短一點、好讀一點。 😊

LPenny-github commented 3 years ago

@twy30 感謝大大 orz

LPenny-github commented 3 years ago

@twy30 大大

請問基數跟底數有什麼不一樣?

(出現於留言(https://github.com/EngTW/English-for-Programmers/issues/79#issue-735708521 )的參考資料) (已修正連結)

我查到的資料,多半是說 可通用 😅 <--沒告訴我 不可 通用的狀況 🤔

所以我還是無法辨別,麻煩大大解釋 orz 感激不盡

twy30 commented 3 years ago

@LPenny-github

好問題 😊 坦白說,我也不清楚 😅

從語源學的角度來看,

我一下能想到的只有這些 🤔


其它一些 base, radix 的例子

twy30 commented 3 years ago

@LPenny-github

題外話 😊

通常我在貼網址連結時會前後都留一個空白,例如以下是你的原文:

(出現於留言(https://github.com/EngTW/English-for-Programmers/issues/79#issue-735708521)的參考資料

GitHub 把後半的 )的參考資料) 也當成了網址的一部分。


在網址前後各插入一半形空白字元 (見以下例子)

(出現於留言( https://github.com/EngTW/English-for-Programmers/issues/79#issue-735708521 )的參考資料)

GitHub 就能正確判斷網址連結,自動把 https://github.com/EngTW/English-for-Programmers/issues/79#issue-735708521 轉換成#79 (comment) (見以下例子)

(出現於留言( https://github.com/EngTW/English-for-Programmers/issues/79#issue-735708521 )的參考資料)

LPenny-github commented 3 years ago

@twy30 感謝大大,我會努力學習。

此外,連結( https://github.com/EngTW/English-for-Programmers/issues/79#issuecomment-739892210 )已修正,是我太粗心了 orz