对一个十进制数的各位数字做一次平方和,称作一次迭代。如果一个十进制数能通过若干次迭代得到 1,就称该数为幸福数。1 是一个幸福数。此外,例如 19 经过 1 次迭代得到 82,2 次迭代后得到 68,3 次迭代后得到 100,最后得到 1。则 19 就是幸福数。显然,在一个幸福数迭代到 1 的过程中经过的数字都是幸福数,它们的幸福是依附于初始数字的。例如 82、68、100 的幸福是依附于 19 的。而一个特立独行的幸福数,是在一个有限的区间内不依附于任何其它数字的;其独立性就是依附于它的的幸福数的个数。如果这个数还是个素数,则其独立性加倍。例如 19 在区间[1, 100] 内就是一个特立独行的幸福数,其独立性为 2×4=8。
另一方面,如果一个大于1的数字经过数次迭代后进入了死循环,那这个数就不幸福。例如 29 迭代得到 85、89、145、42、20、4、16、37、58、89、…… 可见 89 到 58 形成了死循环,所以 29 就不幸福。
本题就要求你编写程序,列出给定区间内的所有特立独行的幸福数和它的独立性。
输入格式:
输入在第一行给出闭区间的两个端点:1<A<B≤10000。
输出格式:
按递增顺序列出给定闭区间 [A,B] 内的所有特立独行的幸福数和它的独立性。每对数字占一行,数字间以 1 个空格分隔。
如果区间内没有幸福数,则在一行中输出 SAD。
输入样例 1:
10 40
输出样例 1:
19 8
23 6
28 3
31 4
32 3
注意:样例中,10、13 也都是幸福数,但它们分别依附于其他数字(如 23、31 等等),所以不输出。其它数字虽然其实也依附于其它幸福数,但因为那些数字不在给定区间 [10, 40] 内,所以它们在给定区间内是特立独行的幸福数。
输入样例 2:
110 120
输出样例 2:
SAD
思路:
这个题用树来做是比较简单的,首先给出一个图,模拟了样例一的迭代,加上代码里写的注释应该很容易就能理解(图片是网上某位大佬的)
代码:
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
public class Main
{
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)1e4 + 10;
static math math_bag = new math();
static int fa[] = new int[N];
// 得到各位数字的平方和
static int get_sqrt_sum(int x)
{
int sum = 0;
while(x > 0)
{
sum += (x % 10) * (x % 10);
x /= 10;
}
return sum;
}
// 找祖先,找到最终指向的根,如果x是幸福数,那么最终一定会指到1,1的根就是自己
static int find_root(int x)
{
int ans = x;
int cnt = 0;
while(fa[ans] != ans)
{
ans = fa[ans];
cnt ++;
if(cnt >= N) return -1;
}
return ans;
}
// 检查[l,r]区间是否有数字指向x,即判断是否独立
static boolean check(int x,int l, int r)
{
for(int i = l ; i <= r ; i ++)
{
if(fa[i] == x) return false;
}
return true;
}
public static void main(String[] args ) throws IOException
{
int n = rd.nextInt();
int m = rd.nextInt();
for(int i = 1 ; i < N ; i ++) fa[i] = i; // 初始化,令所有的数字先指向自己
for(int i = 1 ; i < N ; i ++) fa[i] = get_sqrt_sum(i); // 令所有的数字都指向自己的各位平方和
int cnt_num = 0; // 记录出书了多少个特立独行的幸福数
for(int i = n ; i <= m ; i ++)
{
if(check(i,n,m) && find_root(i) == 1) // 如果找根能找到1,并且n到m区间也没有数字指向它,就是特立独行的幸福数
{
pw.print(i + " ");
// 计算迭代次数
int cnt_iteration = 1,x = i;
while(get_sqrt_sum(x) != 1)
{
x = get_sqrt_sum(x);
cnt_iteration ++;
}
if(math_bag.check_isPrime(i)) cnt_iteration *= 2; // 原数是素数,迭代次数要乘以2
pw.println(cnt_iteration);
cnt_num ++;
}
}
if(cnt_num == 0) pw.println("SAD");
pw.flush();
}
}
class rd
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String nextLine() throws IOException { return reader.readLine(); }
static String next() throws IOException
{
while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException { return Integer.parseInt(next()); }
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
static long nextLong() throws IOException { return Long.parseLong(next()); }
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(rd.nextLine());
return d;
}
}
class math
{
int gcd(int a,int b)
{
if(b == 0) return a;
else return gcd(b,a % b);
}
int lcm(int a,int b)
{
return a * b / gcd(a, b);
}
// 求n的所有约数
List get_factor(int n)
{
List<Long> a = new ArrayList<>();
for(long i = 1; i <= Math.sqrt(n) ; i ++)
{
if(n % i == 0)
{
a.add(i);
if(i != n / i) a.add(n / i); // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况 ^-^复杂度能减少多少是多少
}
}
// 相同因子去重,这个方法,完美
a = a.stream().distinct().collect(Collectors.toList());
// 对因子排序(升序)
Collections.sort(a);
return a;
}
// 判断是否是质数
boolean check_isPrime(int n)
{
if(n < 2) return false;
for(int i = 2 ; i <= n / i; i ++) if (n % i == 0) return false;
return true;
}
}
class PII implements Comparable<PII>
{
int x,y;
public PII(int x ,int y)
{
this.x = x;
this.y = y;
}
public int compareTo(PII a)
{
if(this.x-a.x != 0)
return this.x-a.x; //按x升序排序
else return this.y-a.y; //如果x相同,按y升序排序
}
}
class Edge
{
int a,b,c;
public Edge(int a ,int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
}
3608

被折叠的 条评论
为什么被折叠?



