南邮数据结构、算法实践周提高题2(Number Games)

A positive integer is considered prime if it is evenly divisible only by itself and 1. 
Also, by convention, 1 is not itself a prime.
Thus, the sequence of primes begins:2, 3, 5, 7, 11, 13, 17, ...
A positive integer is called perfect if it is the sum of its proper divisors.
For example, 28 is a perfect number because 28 = 1+2+4+7+14.
Your assignment is to write a program that will input a sequence of integers, one per line, 
and output the properties of each integer in the following format:
If the number is neither prime nor perfect, output "Dull".
If the number is prime but not perfect, output "Prime".
If the number is perfect but not prime, output "Perfect".
If the number is both prime and perfect, output "This program doesn't work".
All inputs will be positive integers. The end of input will be marked by the number 0 (no output should be generated for 0).

#include<iostream.h>
#include<math.h>
bool isPrime(int n){
	if(n==1){
		return false;
	}else{
			for(int i=2;i<=sqrt(n);i++){
				if(n%i==0)
					return false;
			}
			return true;
		}
}
bool isPerfect(int n){
	int sum=0;
	for(int i=1;i<=n/2;i++){
		if(n%i==0){
			sum+=i;
		}
	}
	if(sum==n)
		return true;
	else
		return false;	
}
void Judge(int n){
	if(isPrime(n)==1 && isPerfect(n)==1){
		cout<<"This program doesn't work."<<endl;
	}else if(isPrime(n)==1 && isPerfect(n)==0){
		cout<<"Prime."<<endl;
	}else if(isPrime(n)==0 && isPerfect(n)==1){
		cout<<"Perfect."<<endl;
	}else{
		cout<<"Dull."<<endl;
	}
}
int main(){
	int *x=new int[50];
	int i=0;
	cin>>x[i];
	while(x[i]!=0){
		cin>>x[++i];
	}
	for(int j=0;j<i;j++){
		Judge(x[j]);
	}
	return 0;
}

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值