leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法
2024-09-01 20:58:34
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);
}
};
最新文章
- php实现文件上传与下载(中)
- 框架Maven笔记系列 一 基础
- Android中怎么用this
- js生成二维码实例(真实有效)
- ExecutorService常用方法和newFixedThreadPool创建固定大小的线程池
- 02-自定义CALayer
- python 使用__slots__
- VS2013 MVC Web项目使用内置的IISExpress支持局域网内部机器(手机、PC)访问、调试
- react中的坑
- install tool
- selenium自动化--(JAVA方法写的)第一章 源代码工程的导入
- js复制内容到剪切板,兼容pc和手机端,支持Safari浏览器
- Mac下快速搭建PHP开发环境
- 浅谈iOS内存管理机制
- R语言学习笔记—组合数
- Jmeter如何连接数据库Mysql
- keras系列︱seq2seq系列相关实现与案例(feedback、peek、attention类型)
- python 面向对象基础和高级复习
- ASP.NET MVC 页面使用富文本控件的XSS漏洞问题
- fix for 12c profile
热门文章
- 小程序如何判断用户(后台使用Django)
- 【软件工具】easyExcel简明使用指南
- Ubuntu 16.04 安装最新稳定版Nginx 1.6.0
- 使用aop加解密http接口
- react可拖动的好用的树结构插件
- __getattribute__(self, obj) 这个方法中的obj这个参数
- 宝塔面板的数据库连接本地Navicat Premium
- luogu P1807 最长路_NOI导刊2010提高(07)
- play-with-docker搭配ffsend完成文件上传及下载(解决从docker hub拉取镜像慢问题)
- 洛谷 题解 P1736 【创意吃鱼法】