清华大学机试 完数VS盈数 Easy *注意下质因子和因子分解问题

基本思想:

因子分解问题:

质因子:一直除质数即可;

因子分解:进行1-sqrt(x)的遍历,但是一定要注意,当sqrt为整数时,例如2*2=4,就算因子个数,因子为2只能算一个;

关键点:

注意特殊情况;

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;

vector<int>wnum;
vector<int>ynum;

void charge(int x) {
	int cnt = 1;
	int sqt = sqrt(x);
	for (int i = 2; i <= sqt; i++) {
		if (x%i == 0) {
			if (i == x / i)
				cnt++;
			else
				cnt +=i+x/i;
		}
	}
	if (cnt == x) {
		wnum.push_back(x);
	}
	else if (cnt > x) {
		ynum.push_back(x);
	}
}

int main() {
	for (int i = 2; i < 61; i++) {
		charge(i);
	}
	printf("E:");
	for (int i = 0; i < wnum.size(); i++) {
		cout << " " << wnum[i];
	}
	cout << endl;
	printf("G:");
	for (int i = 0; i < ynum.size(); i++) {
		cout << " " << ynum[i];
	}
	cout << endl;
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12405769.html