问题:设计一个函数集合器(设计一个菜单),包含以下各功能模块:
- 水数仙花
- 完数
- 素数
- 公约数与公倍数
- 哥德巴赫猜想
- 猴子吃桃
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int prime(int) { return 1; }
int Prime(int) { return 1; }
int hanshu1()
{ int a,b,c,number;
for (number=100;number<1000;number++)
{
a = number / 100;
b = number / 10 % 10;
c = number % 10;
if (a * a * a + b * b * b + c * c * c == number) cout << number << endl;
}
return 0;
}
int hanshu2()
{
int sum = 0;
int n; cout << "判断输入是否是完数" << endl;
cin >> n;
for (int i = 1;i < n;i++)
{ if (n % i == 0) { sum += i; }
if (sum == n)
{
cout << "是完数" << endl;
}
else cout << "不是完数" << endl;
}
return 0;
}
int hanshu3()
{
int m, k, i, n = 0;
bool prime;
for(m=101;m<=200;m+=2)
{
prime = true;k = int(sqrt(m));
for(i=2;i<=k;i++)
if(m%i==0)
{
prime = false;break;
}
if (prime) {
cout << setw(5) << m;
n += 1;
}
if (n % 10 == 0)cout << endl;
}
return 0;
}
int hanshu4()
{
int m, n, r;
cout << "输入俩个数";
cin >> m >> n;
int s = m * n;
r = m % n;
while(r!=0)
{
m = n;
n = r;
r = m % n;
}
cout << "最大公倍数" << n << endl;
cout << "最小公倍数" << s/n << endl;
return 0;
}
int hanshu5()
{
long i, j, k, flag1, flag2, n = 0;
int a, b;
int Prime(int);
cout << "请输入要验证的偶数范围,即输入两个正偶数:";
cin >> a >> b;
int min = a <= b ? a : b;
int max = a >= b ? a : b;
for(i=min;i<=max;i+=2)
{
for (i = min;i<=max;i+=2)
{
for (k = 2;k<=i/2;k++)
{
j = i - k;flag1 = Prime(k);
if(flag1)
{
flag2 = Prime(j);
if(flag2)
{
cout << i << "=" << j << "+" << k << " ";
n++;
if (n % 5 == 0)cout << endl;break;
}
}
}
}
}
cout << "哥德巴赫猜想正确!"<<endl;
return 0;
}
int hanshu6()
{
int a, b = 1;
for(int i=1;i<10;i++)
{
a = (b + 1) * 2;
b = a;
}
cout << "第一天摘的桃子有" << a << "个" << endl;
system("pause");
return 0;
}
int main()
{
int a;
cout << "1) 水数仙花" << endl;
cout << "2) 完数" << endl;
cout << "3) 素数" << endl;
cout << "4) 公约数与公倍数" << endl;
cout << "5) 哥德巴赫猜想" << endl;
cout << "6) 猴子吃桃" << endl;
cout << "请选择";
cin >> a;
switch(a)
{
case 1:hanshu1;break;
case 2:hanshu2;break;
case 3:hanshu3;break;
case 4:hanshu4;break;
case 5:hanshu5;break;
case 6:hanshu6;break;
default:cout << "错误!" << endl;
}
return 0;
}