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);
}
};


最新文章

  1. php实现文件上传与下载(中)
  2. 框架Maven笔记系列 一 基础
  3. Android中怎么用this
  4. js生成二维码实例(真实有效)
  5. ExecutorService常用方法和newFixedThreadPool创建固定大小的线程池
  6. 02-自定义CALayer
  7. python 使用__slots__
  8. VS2013 MVC Web项目使用内置的IISExpress支持局域网内部机器(手机、PC)访问、调试
  9. react中的坑
  10. install tool
  11. selenium自动化--(JAVA方法写的)第一章 源代码工程的导入
  12. js复制内容到剪切板,兼容pc和手机端,支持Safari浏览器
  13. Mac下快速搭建PHP开发环境
  14. 浅谈iOS内存管理机制
  15. R语言学习笔记—组合数
  16. Jmeter如何连接数据库Mysql
  17. keras系列︱seq2seq系列相关实现与案例(feedback、peek、attention类型)
  18. python 面向对象基础和高级复习
  19. ASP.NET MVC 页面使用富文本控件的XSS漏洞问题
  20. fix for 12c profile

热门文章

  1. 小程序如何判断用户(后台使用Django)
  2. 【软件工具】easyExcel简明使用指南
  3. Ubuntu 16.04 安装最新稳定版Nginx 1.6.0
  4. 使用aop加解密http接口
  5. react可拖动的好用的树结构插件
  6. __getattribute__(self, obj) 这个方法中的obj这个参数
  7. 宝塔面板的数据库连接本地Navicat Premium
  8. luogu P1807 最长路_NOI导刊2010提高(07)
  9. play-with-docker搭配ffsend完成文件上传及下载(解决从docker hub拉取镜像慢问题)
  10. 洛谷 题解 P1736 【创意吃鱼法】