判断自守数
来源:互联网 发布:手机淘宝为什么登不上 编辑:程序博客网 时间:2024/06/06 06:25
题目描述:
如果某个数的平方的末尾几位数等于这个数,那么就称这个数为自守数。
显然,5和6是一位自守数(5x5=25 6x6=36),25x25=625 76x76=5776,所以25和76是两位自守数。
输入一个数,判断该数是否为自守数。
解题思路:若采用“求出一个数的平方后再截取最后相应位数”的方法,显然是不可取的,因为计算机无法表示过大的整数。所以我们采取大数相乘的思想来存储这个数的平方。然后再去判断该平方项的后几位是否与原数相等。
import java.util.Scanner;public final class Demo { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); System.out.println("please num:"); int n = scanner.nextInt(); System.out.println(isAutoMorphicNum(n)); } public static boolean isAutoMorphicNum(int num) { if(num < 0){ return false; } if(num == 0){ return true; } String s = String.valueOf(num); int[] number = new int[s.length()]; for(int i=0;i<s.length();i++){ number[s.length()-i-1] = s.charAt(i) - '0'; // "12345"-> 5,4,3,2,1 } int[] result = multi(number, number); String str = ""; for(int i=result.length-1;i>=0;i--){ str += result[i]; } String str1 = num + ""; if (str1.equals(str.substring(str.length() - str1.length()))) { //判断是否为自守数 return true; } return false; } //下面的方法实现大数相乘,将结果每一位保存到result数组中 public static int[] multi(int num1[], int num2[]){ int len1 = num1.length; int len2 = num2.length; int[] result = new int[len1 + len2]; for(int i=0;i<len1;i++){ for(int j=0;j<len2;j++){ result[i+j] += num1[i]*num2[j]; } } for(int i=0;i<result.length-1;i++){ if(result[i] > 10){ result[i+1] += result[i]/10; result[i] %= 10; } } return result; }}
0 0
- 自守数判断
- 判断自守数
- 判断是否是自守数
- 华为机试题:判断自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数!
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- Set、List、HashMap优缺点比较,高性能集合
- textarea自动变换高度
- Android AdapterView View的复用机制分析
- leetcode.264. Ugly Number II
- AndroidStudio 断开SVN链接
- 判断自守数
- discuz mobile 接口加载过程
- 23种设计模式(3):抽象工厂模式
- iOS开发系列-动画绘图CALayer
- cocoapods管理私有库
- 函数重载总结
- “秒”转换成“时间”
- 【HUSTOJ】1074: 拐角VII
- [NOIP2004]虫食算 T4 简单搜索+剪枝