//求长方体数组
int main()
{
long a, b, d, k, x, y, z, w;
printf(" 请输入区间[a,b]的上下限a,b:");
scanf("%ld, %ld", &a, &b);
k = 0;
for (x = a; x <= sqrt(b*b / 3); x++) //设置枚举三重循环
for (y = x; y <= sqrt((b*b-x*x) /2);y++)
for (z = y; z <= sqrt(b*b - x * x - y * y); z++)
{
d = x * x + y * y + z * z;
w = (int)sqrt(d); //"为×、,、z的平方和开平方
if (w > b) break;
if (w * w == d) // 满足条件时比较最值
{
k++; //输出最值
printf("%3d: %1d,%1d, %ld, %1d \n", k, x, y, z, w);
}
}
if (k > 0)
printf("在指定区间【%ld,%ld】中共有以上%ld组长方体数",a,b,k);
else printf("在指定范围内没有长方体数\n");
return 0;
}
结果:
//指定区间内的完全数
int main()
{
int k;
long a, s, x, y;
printf(" 请输入整数 x,y:");
scanf ("%ld, %ld", &x, &y);
printf("[%1d,%1d]中的完全数有:\n", x, y);
for (a = x; a <= y; a++)
{
s = 1;
for (k = 2; k <= a / 2; k++) // 试商寻求a的因数k
if (a%k == 0) s = s + k; //k是a的因数,赋值求和
if (a == s)
{
printf("%ld = 1", a); //从小到大打印因数和式
for (k = 2; k <= a / 2; k++)
if (a%k == 0) printf("+%d", k);
if (a % 2 == 1) printf(" 找到奇完全数!");
printf("\n");
}
}
return 0;
}
结果:
//探求完全数与P-完全数
int main()
{
int k;
long a, b, s, x, y;
printf(" 求区间[x,y]中的完全数。");
printf(" 请输入整数x, y: ");
scanf("%ld",&x);
scanf("%ld",&y);
printf("[ %1d, %1d ]中的完全数有:\n",x,y);
for (a = x; a <= y; a++)
{
s = 1;
b = sqrt(a);
for (k = 2; k <= b; k++)
if (a%k == 0) s = s + k + a / k; // 试商寻求a的因数k
if (a == b * b)
s = s - b; //k与alk是a的因数 // 如果a=b^2,去掉一个重复因数b
if (a == s)
{
printf("%ld = 1", a); //打印a的因数和式
for (k = 2; k <= a / 2; k++)
if (a%k == 0) printf("+%d", k);
if (a % 2 == 1)printf("奇完全数!");
printf("\n");
}
}
return 0;
}
结果:
int main()
{
double a, a1, s, b, c, d, d1, k, t, t1, x, y, p[10], q[10], min;
int j, m = 0;
min = 1.0;
printf(" 请输入区间x,y:");
scanf ("%lf", &x) ;
scanf("%lf", &y);
//枚举区间内的所有整数a
for (a = x; a <= y; a++)
{
s = 1;
b = floor(sqrt(a)); // 试商寻求a的因数k
for (k = 2; k <= b; k++)
if (fmod(a, k) == 0)
s = s + k + a / k;
if (a == b * b)
s = s - b; //k与a/k是a的因数,求和 //如果a-b°2,去掉重复因数b
t = s / a;
d = floor(t);
c = t - d;
if (c == 0)
{
m++;
p[m] = a;
q[m] = t;
}
else if (c > 0.5)
{
c = 1 - c;
d = d + 1;
}
if (t > 0.5 && c < min)
{
min = c;
a1 = a;
t1 = t;
d1 = d;
}
}
if (m > 0)
for (j = 1; j <= m; j++)
printf(" p(%.0f) = %.0f", p[j], q[j]);
else
printf(" %.0f的因数比%.4f最接近正整数%.0f\n",a1,t1,d1);
return 0;
}
结果: