Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
twy30 opened this issue Nov 3, 2020 · 7 comments · Fixed by #90
Closed

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

twy30 opened this issue Nov 3, 2020 · 7 comments · Fixed by #90
Labels

Comments

@twy30
Copy link
Contributor

twy30 commented Nov 3, 2020

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

例如:

  • 個位: ones place 或 units place
  • 個位數: ones digit 或 units digit
  • 十位: tens place
  • 十位數: tens digit
  • 十分位: tenths place
  • 十分位數: tenths digit

底數(radix) 、基數(base)

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


請參考「刷 LeetCode 練習命名」 #69 😊

@LPenny-github
Copy link

@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
Copy link
Contributor Author

twy30 commented Dec 7, 2020

@LPenny-github 不客氣 😊

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

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


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

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

  • inputDigits (輸入位數 (複數))
  • productOfInputDigits (輸入位數的積)
  • sumOfInputDigits (輸入位數的和)

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

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

@LPenny-github
Copy link

@twy30 感謝大大 orz

@LPenny-github
Copy link

LPenny-github commented Dec 7, 2020

@twy30 大大

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

(出現於留言(#79 (comment) )的參考資料)
(已修正連結)

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

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

@twy30
Copy link
Contributor Author

twy30 commented Dec 7, 2020

@LPenny-github

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

從語源學的角度來看,

  • Radix 來自拉丁文的 radix, 語源是 根 (root)
  • Base 來自拉丁文的 basis, 語源是 底座 (basis)

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


其它一些 base, radix 的例子

@twy30
Copy link
Contributor Author

twy30 commented Dec 7, 2020

@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) (見以下例子)

(出現於留言( #79 (comment) )的參考資料)

@LPenny-github
Copy link

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

此外,連結( #79 (comment) )已修正,是我太粗心了 orz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants