设x为一个十进制正整数,定义s(x)为x的每一位上的数字之和,如s(123) = 1+2+3 = 6 对于如下方程:x = b*(s(x)^a)+c 给出a,b,c的值,要求找出[1,999999999]内所有符合条件的x,并从小到大输出。
输入数据
三个正整数,分别代表a,b,c 1<=a<=5 1<=b<=10000 -10000<=c<=10000
输出数据
第一行为一个正整数n,代表符合条件的x的个数 第二行为n个递增的数,代表所有符合条件的x
样例输入
3 2 8
样例输出
3
10 2008 13726
这道题做出来不难,难点在于不超过OJ的时间限制,如果是把x从1到999999999遍历,肯定会花很长时间。所以这题的key point在于是把s(x)在1到81遍历。s(x)最大值为81,所以要从1遍历到81。然后根据题给公式算出x(此时x的值肯定小于999999999),并且这个结果的各位之和为对应s(x)。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int x = 0;
int i = 0;
int[] array = new int[100];
for(int y=1;y<=81;y++)
{
x = (int)(b*Math.pow(y, a)+c);
if(s(x)==y)
{
array[i]=x;
i++;
}
}
System.out.println(i);
for(int q=0;q<i;q++)
{
System.out.print(array[q]+" ");
}
}
public static int s(int x) {
int sum = 0;
while(x!=0)
{
sum += x % 10;
x /= 10;
}
return sum;
}
}