请编写一个程序寻找一种特殊整数:一个 n 位的正整数等于其各位数字的n次方之和。
答案:2 悬赏:10
解决时间 2021-02-17 05:50
- 提问者网友:浪荡羁士
- 2021-02-16 09:47
例如:407=4×4×4+0×0×0+7×7×7。所以407就是一个特殊数。
输入:
正整数的位数n(n<=6)。
输出:
所有此n位特殊数。每个数占一行。若不存在符合条件的特殊数,则输出提示:“No output.”;若存在,则从小到大进行输出。
说明:
假设输入为4,在4位整数中,有3个4位的特殊数,则输出格式为(输出中的1111、2222和9999并不是4位特殊数,只是格式样例):
111122229999 测试输入 期待的输出 时间限制 内存限制 额外进程
测试用例 1 以文本方式显示 3↵
以文本方式显示 153↵
370↵
371↵
407↵
这个程序错在哪儿了?????
#include
#include
#include
int main(int argc, char *argv[])
{
int n,a,b,i,j=0,k,m[6],s=0,c,l=0;
scanf("%d",&n);
a=pow(10,n-1);
b=pow(10,n)-1;
for(i=a;i<=b;i++)
{k=i;
while(k)
{m[j++]=k%10;k=k/10;}
for(c=0;c s+=pow(m[c],n);
if(s==i) {l=1; printf("%d\n",i);}
}
if(l==0) printf("No output\n");
system("PAUSE");
return 0;
}
最佳答案
- 二级知识专家网友:瘾与深巷
- 2021-02-16 09:54
#include
int main(void)
{
unsigned long int max,min,sum,cache,product;
unsigned int temp[6];
unsigned int number;
int i,j,k,find = 0;
max = min = sum = product = 0;
printf("Input the number of digits (not bigger than 6):_\b");
scanf("%d",&number);
if(number <=0 || number >= 7){
printf("Input Error,the number must be less or equal to 6 and bigger than 0\n");
return 1;
}
for(i = 0;i <= number-1;i++){
min = (i==0?min*10+1:min*10+0); //最小n位数
max = max * 10 + 9; //最大n位数
}
while (min < max) {
cache = min;
sum = 0;
for(j=(number-1);j>=0;j--){
temp[j] = cache % 10; //n位数的每一位
cache = cache / 10;
for(k=0;k<=number-1;k++)
product=(k==0?temp[j]:product*temp[j]); //每一位的n次方
sum=sum + product; //每一位的n次方总和
}
if (min == sum)
{
find=1;
printf("%ld\n",min);
}
min ++ ;
}
if ( find == 0) printf("No Output\n");
return 0;
}
全部回答
- 1楼网友:随心随缘不随便
- 2021-02-16 10:12
以下代码 python 2/3 通用。
#!/usr/bin/env python3
# coding=utf-8
"""http://wenwen.sogou.com/z/q856104582.htm
编写一个程序寻找一种特殊整数:一个n位的正整数等于其各位数字的n次方之和。例如:
407=4^3+0^3+7^3,所以407就是一个符合要求的特殊整数。
输入:正整数的位数n(n<=6)。
输出:所有符合要求的n位特殊数。若不存在符合条件的特殊数,则输出提示:“no output.”;若存在,则从小到大进行输出,每个数占一行。
"""
from __future__ import division, print_function, unicode_literals
import itertools
def get_int(digits, n):
return sum(i ** n for i in digits)
def test_get_int():
assert get_int((4, 0, 7), 3) == 407
assert get_int((4, 2, 7), 3) == 407 + 8
def digits_match(this_int, digits):
return sorted(str(this_int)) == sorted(''.join(map(str, digits)))
def test_digits_match():
assert digits_match(123, (1, 2, 3))
assert digits_match(123, (2, 1, 3))
assert digits_match(102, (1, 0, 2))
assert digits_match(112, (1, 1, 2))
assert digits_match(112, (1, 2, 1))
assert not digits_match(120, (1, 2, 1))
assert not digits_match(120, (1, 2))
def find_ints(n):
result = []
for c in itertools.combinations_with_replacement(range(10), n):
this_int = get_int(c, n)
if digits_match(this_int, c):
result.append(this_int)
result.sort()
return result
def main():
n = int(input())
results = find_ints(n)
if results:
for i in results:
print(i)
else:
print("no output.")
if __name__ == '__main__':
main()执行效果:
$ python3 find_ints_power_of_all_digits.py
1
0
1
2
3
4
5
6
7
8
9
$ python3 find_ints_power_of_all_digits.py
2
no output.
$ python3 find_ints_power_of_all_digits.py
3
153
370
371
407
$ python3 find_ints_power_of_all_digits.py
4
1634
8208
9474
$ python3 find_ints_power_of_all_digits.py
5
54748
92727
93084
$ python3 find_ints_power_of_all_digits.py
6
548834
我要举报
大家都在看
推荐资讯