算法作业_11(2017.3.29第六周)
2017-03-29 19:22
239 查看
507. Perfect Number
public:
bool checkPerfectNumber(int num) {
if(num==1)
return false;
int sum = 1;
int up = sqrt(num);
for(int i=2;i<=up;i++){
if(num%i==0){
sum = sum +i;
sum = sum + num/i;
}
}
return num==sum;
}
};
java:
public class Solution {
public boolean checkPerfectNumber(int num) {
if(num==1){
return false;
}
int sum =1;
int up = (int)Math.sqrt(num);
for(int i =2; i<=up; i++){
if(num%i==0){
sum = sum+i;
sum = sum+num/i;
}
}
return sum == num ;
}
}
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.
Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.
class Solution {public:
bool checkPerfectNumber(int num) {
if(num==1)
return false;
int sum = 1;
int up = sqrt(num);
for(int i=2;i<=up;i++){
if(num%i==0){
sum = sum +i;
sum = sum + num/i;
}
}
return num==sum;
}
};
java:
public class Solution {
public boolean checkPerfectNumber(int num) {
if(num==1){
return false;
}
int sum =1;
int up = (int)Math.sqrt(num);
for(int i =2; i<=up; i++){
if(num%i==0){
sum = sum+i;
sum = sum+num/i;
}
}
return sum == num ;
}
}
相关文章推荐
- 算法设计与结构基础作业第六周
- 计科1111-1114班第六周讲义、课外作业(贪心算法之最小生成树、哈夫曼编码,截止日期:2014年4月18日23点-周五晚,学委飞信通知同学)
- 算法课程第六周Leetcode作业
- 算法课第六周作业 | Jump Game
- 算法分析与设计课程作业第六周#1
- 第六周作业_Chapter 11课后练习
- 【算法作业11】LeetCode 70. Climbing Stairs
- 数值作业:龙贝格算法计算积分C语言实现
- 算法结构与设计基础作业第十一周
- 第六周作业1——利用哈夫曼编码英文字母表
- 第四周算法概论作业——无向图的DFS算法
- 算法第三周作业02
- 第六周作业——利用哈夫曼编码英文字母表
- 算法第六周Majority Element II[medium]
- 动态规划;多边形游戏;类似圈型石头合并;算法设计分析作业;
- 11、12章(异常处理和范型算法)
- 【算法作业12】LeetCode 198. House Robber
- java_实现先来先服务(FCFS)短作业优先算法(SJF)
- 算法与数据结构第七次作业 太阳帝国
- 短作业优先算法c++版