leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法
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)--- 单一数字的乘积和总合的减法的更多相关文章
- leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字
1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...
- leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字
1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...
- leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)
Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...
- leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping
1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...
- leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...
- 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 ...
- 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 ...
- leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点
1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...
- leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段
1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...
随机推荐
- 从spring源码汲取营养:模仿spring事件发布机制,解耦业务代码
前言 最近在项目中做了一项优化,对业务代码进行解耦.我们部门做的是警用系统,通俗的说,可理解为110报警.一条警情,会先后经过接警员.处警调度员.一线警员,警情是需要记录每一步的日志,是要可追溯的,比 ...
- Java 虚拟机结构
一 数据类型 与 Java 程序语言中的数据类型相似,Java 虚拟机可以操作的数据类型可分为两类:原始类型(Primitive Types,也经常翻译为原生类型或者基本类型)和引用类型(Refere ...
- Centos 7.x 系统基础优化
Centos 7.x 系统基础优化 1.更换国内yum源 删除系统带的centos官方yum源 rm -rf /etc/yum.repos.d/* 使用国内阿里云源 curl -o /etc/yum. ...
- PyCharm 2019.3发布,增加了哪些新功能呢?
Python的IDE(Integrated Development Environment 集成开发环境)非常多,如:VS Code.Sublime.NotePad.Python自带编辑器IDLE.J ...
- Ubuntu中git pull远程仓库时显示403错误
# 报错内容 fatal: unable to access 'https://git.dev.tencent.com/chendongnan/sfedu_wx.git/': The requeste ...
- 2019-2020-11 20199304 《Linux内核原理与分析》 第十二周作业
ShellShock攻击实验 一.实验简介 2014年9月24日,Bash中发现了一个严重漏洞shellshock,该漏洞可用于许多系统,并且既可以远程也可以在本地触发 二.预备知识 1.shells ...
- MySQL必知必会(Select, Where子句)
SELECT prod_name, prod_price FROM products WHERE prod_price = 2.5; SELECT prod_name, prod_price FROM ...
- asp.net core中间件工作原理
不少刚学习.net core朋友对中间件的概念一直分不清楚,到底StartUp下的Configure方法是在做什么? public void Configure(IApplicationBuilder ...
- luogu P3807 【模板】卢卡斯定理
求 C(n,n+m)%p C(m,n)%p=C(m%p,n%p)*C(m/p,n/p) #include<cstdio> #include<cstring> #include& ...
- react-native 相对项目路径导入组件 ___ babel-plugin-module-resolver
babel-plugin-module-resolver 是一个Babel模块解析插件, 在.babelrc中可以配置模块的导入搜索路径. 为模块添加一个新的解析器.这个插件允许你添加新的" ...