【LeetCode】1134. Armstrong Number 解题报告(C++)
2024-09-08 13:48:26
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/
题目描述
The k
-digit number N
is an Armstrong number if and only if the k
-th power of each digit sums to N
.
Given a positive integer N
, return true if and only if it is an Armstrong number.
Example 1:
Input: 153
Output: true
Explanation:
153 is a 3-digit number, and 153 = 1^3 + 5^3 + 3^3.
Example 2:
Input: 123
Output: false
Explanation:
123 is a 3-digit number, and 123 != 1^3 + 2^3 + 3^3 = 36.
Note:
1 <= N <= 10^8
题目大意
给你一个整数数组 A,请找出并返回在该数组中仅出现一次的最大整数。
如果不存在这个只出现一次的整数,则返回 -1。
解题方法
直接计算
先算k,然后判断即可。
C++代码如下:
class Solution {
public:
bool isArmstrong(int N) {
int k = 0;
int temp = N;
while (temp != 0) {
k++;
temp /= 10;
}
long long res = 0;
temp = N;
while (temp != 0) {
int mod = temp % 10;
res += pow(mod, k);
temp /= 10;
}
return res == N;
}
int pow(int N, int k) {
int res = 1;
while (k --) {
res *= N;
}
return res;
}
};
日期
2019 年 9 月 18 日 —— 今日又是九一八
最新文章
- 在MySQL的InnoDB存储引擎中count(*)函数的优化
- IOS应用内存释放机制
- mysql xml 参数
- Fragment的onResume
- nginx + Lua 实现自定义WAF
- 【转载】CMake 简介和 CMake 模板
- 基本API-StdIn.java
- OSI七层模型理解
- java中的移位运算符:<;<;,>;>;,>;>;>;总结(转)
- Flask知识点二
- java基本语法特殊点
- PAT1031:Hello World for U
- SAwUML – UML-based, contractual software architectures and their formal analysis using SPIN
- JAVA框架之Hibernate框架的学习步骤
- Servlet案例6:显示用户的上次访问时间
- 将音乐生成波浪图形,JavaScript Html5
- 22-Python3 输入和输出
- 动态BT跳转
- php date时间的获取
- Dataguard学习笔记
热门文章
- bcftools 提取vcf(snp/indel)文件子集
- Docker-Mysql-proxy Mysql Proxy实现读写分离
- 大数据学习day14-----第三阶段-----scala02------1. 元组 2.类、对象、继承、特质 3.函数(必须掌握)
- 零基础学习java------day9------多态,抽象类,接口
- 【Python】【Basic】【数据类型】运算符与深浅拷贝
- 搭建内网Yum源
- SpringAOP浅析
- Spring(1):Spring介绍
- Maven项目打包成war包并启动war包运行
- Spring 与 SpringBoot 的区别