题目描述
完全数(Perfect number),又称完美数或完备数,是一些特殊的自然数。
它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身。
例如:28,它有约数1、2、4、7、14、28,除去它本身28外,其余5个数相加,1+2+4+7+14=28。
给定函数count(int n),用于计算n以内(含n)完全数的个数。计算范围, 0 < n <= 500000
返回n以内完全数的个数。 异常情况返回-1
/**
-
完全数(Perfect number),又称完美数或完备数,是一些特殊的自然数。
-
它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身。
-
例如:28,它有约数1、2、4、7、14、28,除去它本身28外,其余5个数相加,1+2+4+7+14=28。
-
给定函数count(int n),用于计算n以内(含n)完全数的个数
-
@param n 计算范围, 0 < n <= 500000
-
@return n 以内完全数的个数, 异常情况返回-1
*/
public static int count( int n)
输入描述:
输入一个数字
输出描述:
输出完全数的个数
示例1
输入
1000
输出
3
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int M;
while(cin >> M)
{
int count = 0;
//vector<int> ret;
for (int i = M; i >= 2; --i)
{
int sum = 1;
for (int j = i/2; j >= 2; --j)
{
if (i % j == 0)
{
//ret.push_back(i);
sum += j;
}
}
if (i == sum)
{
count++;
}
}
cout << count << endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string line;
while (getline(cin, line)){
//王炸最大,直接输出
if (line.find("joker JOKER") != -1)
cout << "joker JOKER" << endl;
else{
int dash = line.find('-');
//分开两手牌
string car1 = line.substr(0, dash);
string car2 = line.substr(dash + 1);
//获取空格的次数,牌数为c + 1
int c1 = count(car1.begin(), car1.end(), ' ');
int c2 = count(car2.begin(), car2.end(), ' ');
//拿到第一张牌
string first1 = car1.substr(0, car1.find(' '));
string first2 = car2.substr(0, car2.find(' '));
string str = "345678910JQKA2jokerJOKER";
if (c1 == c2){
//只要牌数相等,则第一张牌大的即为大
if (str.find(first1) > str.find(first2))
cout << car1 << endl;
else
cout << car2 << endl;
}
else
{
//牌数不相同,说明类型不同,只有炸弹可以和其它牌比较
//其它类型都是错误的
if (c1 == 3)
cout << car1 << endl;
else if (c2 == 3)
cout << car2 << endl;
else
cout << "ERROR" << endl;
}
}
}
}