1.原题:

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

Given an integer number n, return the difference between the product of its digits and the sum of its digits.

翻译:给定一个数字n,返回乘积和总合的差。

理论上的输入输出:

Input: n = 234

Output: 15

Explanation:

Product of digits = 2 * 3 * 4 = 24

Sum of digits = 2 + 3 + 4 = 9

Result = 24 - 9 = 15

2.解题思路:

应该来说是最简单的问题了,这里我们用的是 n % 10的方法,%得出的结果是两个数相除后的余数,因此你对任何正整数用,结果都是其最小位的数字。

然后得到小数之后,使用 / 除法操作符,因为是int,所以不用担心小数。

class Solution {
public:
int subtractProductAndSum(int n)
{
int pd = 1;
int sd = 0;
for (;n > 0 ; n /= 10)
{
pd *= n%10;
sd += n%10;
}
return (pd-sd);
}
};


leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法的更多相关文章

  1. leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字

    1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...

  2. leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字

    1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...

  3. leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)

    Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...

  4. leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping

    1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...

  5. leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石

    1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...

  6. leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST

    1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...

  7. leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero

    1.原题: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/ Given an integer n, retur ...

  8. leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点

    1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...

  9. leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段

    1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...

随机推荐

  1. Chapter 05—Advanced data management(Part 1)

    一. R的数学函数,统计函数及字符处理函数 例01:一道实际应用题 一组学生其数学,科学和英语的成绩如下表: 任务:根据成绩,决定对每个学生的单独指导: 前20%的学生的成绩为A,次之为B,以此类推: ...

  2. Spring 读取资源

    Spring 读取资源 主要介绍3种方式(当然不止三种,但是这三种基本能应付大多需求)FileSystemResource:以文件的绝对路径方式进行访问ClassPathResourcee:以类路径的 ...

  3. 【HC资料合集】2019华为全联接大会主题资料一站式汇总,免费下载!

    HUAWEI CONNECT 2019 大会主题演讲.峰会演讲精彩资料速递,欢迎下载查阅. 主题 资料下载(登录后可下载附件) 演讲者 [主题演讲资料]2019华为全联接大会day   2 共筑高品质 ...

  4. 你知道,HTTPS用的是对称加密还是非对称加密?

    1.引言 随着互联网安全意识的普遍提高,对安全要求稍高的应用中,HTTPS的使用是很常见的,甚至在1年前,苹果公司就将使用HTTPS作为APP上架苹果应用市场的先决条件之一(详见<苹果即将强制实 ...

  5. jQuery 判断页面对象是否存在

    不能用 if($("#id")){}else{} 因为 $("#id") 不管对象是否存在都会返回 object.   正确使用判断对象是否存在应该用: if( ...

  6. react-native-splash-screen 隐藏statusbar

    目录 android ios android android/app/src/main/java/项目名/MainActivity.java @Override protected void onCr ...

  7. HDU1517 Multiply Game

    Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers 2 t ...

  8. BZOJ 3108: [cqoi2013]图的逆变换

    3108: [cqoi2013]图的逆变换 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 627  Solved: 415[Submit][Statu ...

  9. 前端flex布局学习笔记

    flex布局,即为弹性布局,其为盒模型提供最大的灵活性,任何一个容器都可以指定为flex布局. eg:.box{ display:flex: } 行内元素也可以使用flex布局. 注意:设置flex布 ...

  10. ARTS-S EN0001-In tech race with China, US universities may lose a vital edge

    原文 The U.S. is still out in front of global rivals when it comes to innovation, but American univers ...