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. 数据表与简单java类映射转换

    简单的Java类的定义来源于数据表的结构, 例如:雇员信息表.部门信息表描述的就是雇员或部门的信息, 在实际的开发之中,数据表和简单java类之间的映射关系如下: 1. 数据实体表设计 = 类的定义: ...

  2. 聚类-K-Means

    1.什么是K-Means? K均值算法聚类 关键词:K个种子,均值聚类的概念:一种无监督的学习,事先不知道类别,自动将相似的对象归到同一个簇中 K-Means算法是一种聚类分析(cluster ana ...

  3. Docker 构建私有仓库

    Docker Hub 目前Docker官方维护了一个公共仓库Docker Hub,其中已经包含了数量超过15000的镜像.大部分需求都可以通过在Docker Hub中直接下载镜像来实现. 可以在htt ...

  4. Docker数据挂载

    Docker数据管理 在容器中管理数据主要有两种方式: 数据卷(Volumes) 挂载主机目录(Bind mounts) 数据卷 数据卷是一个可供一个或多个容器使用的特殊目录,它绕过UFS,可以提供很 ...

  5. 阿里巴巴主导的“华山版《Java 开发手册》”简介

    1."83行代码计划"项目介绍(转自github): 2018年9月22日,在2018杭州云栖大会上,召开<码出高效:Java 开发手册>新书发布会,并宣布将图书所有收 ...

  6. 洛谷 CF402A Nuts 题解

    本蒟蒻又来发题解啦! 这题是个紫题? 好吧,恶意评分可海星? 回到正题 这题很明显是贪心啊: 有a个坚果,b个隔板,x个隔板,最多分成v个区间. 那么我们的贪心策略是: 如果一共使用的挡板小于x,且当 ...

  7. UIScrollViewDelegate相关方法

    UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView;//scrollview 滚动的时候调用该方法, ...

  8. [TimLinux] Python3.6 异常继承关系

    Python3.6 异常继承结构 object └── BaseException ├── Exception │   ├── ArithmeticError │   │   ├── Floating ...

  9. HDU5919 Sequence II(主席树)

    Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...

  10. CodeForces999A-Mishka and Contest

    A. Mishka and Contest time limit per test 1 second memory limit per test 256 megabytes input standar ...