我正在尝试编写一个类来检查一个数字是否是阿姆斯壮的数字。我遇到以下代码块时遇到问题。
public boolean checkNum(long num) {
digits = (int) (Math.log10(num) + 1);
String number = String.valueOf(num);
numDigits = number.toCharArray();
for (int i = 0; i < numDigits.length; i++) {
digit = numDigits[i] * 1.0;
power = digits * 1.0;
sum = sum + (long) (Math.pow(digit, power));
}
if (sum == num) {
return true;
} else {
return false;
}
}
投射似乎不起作用,checkNum
每次都返回false
。这是一种正确的方法,还有更好的方法吗?
答案 0 :(得分:2)
尝试这一点,仅使用算术运算,它适用于具有任意位数的非负整数(只要它们适合long
)。
public boolean checkNum(long num) {
long n = num;
long sum = 0;
// find the number of digits
int power = (int) Math.floor(Math.log10(n == 0 ? 1 : n)) + 1;
while (n != 0) {
int digit = (int) n % 10;
sum += Math.pow(digit, power);
n /= 10;
}
return sum == num;
}
或者(虽然效率较低)您可以将数字转换为字符串并迭代每个字符将其转换为数字。以下是您的预期解决方案的固定版本,并对关键点进行了评论:
public boolean checkNum(long num) {
String number = String.valueOf(num);
char[] numDigits = number.toCharArray();
long sum = 0;
// a simple way to obtain the number of digits
int power = numDigits.length;
for (int i = 0; i < numDigits.length; i++) {
// this is how we transform a character into a digit
int digit = Character.digit(numDigits[i], 10);
// we need to rise digit to the value of power
sum = sum + (long) Math.pow(digit, power);
}
if (sum == num) {
return true;
} else {
return false;
}
}
例如,使用任一实现来验证以下是Armstrong数字:
checkNum(6)
=> true
checkNum(371)
=> true
checkNum(1634)
=> true
答案 1 :(得分:1)
您也可以使用此简单逻辑
public class Armstrong {
public static void main(String[] args) {
int number = 371, originalNumber, remainder, result = 0;
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
}
if(result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}
答案 2 :(得分:0)
猜猜这将起作用:
boolean isArmstrong(int x){
int s=0;
int u=x;
while(x!=0)
{
int y=x%10;
s=s+(y*y*y);
x=x/10;
}
if(u==s)
return true;
else
return false;
}
答案 3 :(得分:0)
null
答案 4 :(得分:0)
在Kotlin中,您可以使用:
fun main() {
println("---------------------------------------")
val userInputValues = Scanner(System.`in`)
//* Kotlin Program to Display Armstrong Numbers Between Intervals Using Function
println("* Kotlin Program to Display Armstrong Numbers Between Intervals Using Function\n")
println("Enter your number range")
println("Enter start number of your range \t ")
val startRange = userInputValues.nextInt()
println("Enter end number of your range \t ")
val endRange = userInputValues.nextInt()
println("\n\n------ Armstrong number between $startRange and $endRange ------ ")
for (number in startRange..endRange) {
var stringNumber : String = number.toString()
var numberArray = stringNumber.toCharArray()
var powerOfNumber:Int = numberArray.size;
var result = 0
for (digit in numberArray){
var intDigit:Int = digit.toString().toInt()
result += intDigit.toDouble().pow(powerOfNumber.toDouble()).toInt()
}
if(result == number){
println( "$number is Armstrong number")
}
}
println("---------------------------------------")
}
答案 5 :(得分:0)
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the number: ");
int number=scanner.nextInt();
scanner.close();
int lastDigit=0;
int reverseNum=0;
int originalNumber=number;
while(originalNumber!=0) {
lastDigit=originalNumber%10;
reverseNum +=(lastDigit*lastDigit*lastDigit);
originalNumber /=10;
}
if(reverseNum==number) {
System.out.println("Number is Armstrong");
}
else {
System.out.println("Number is not Armstrong");
}
}
答案 6 :(得分:-2)
在这里,我已经完成了一段代码以动态查找阿姆斯壮编号:
import java.util.Scanner;
public class Armstrong {
public static void main(String[] args) {
if(isArmstrongNumber(input())) {
System.out.println("armstrong number");
} else {
System.out.println("Not armstrong number");
}
}
private static int input() {
try(Scanner reader = new Scanner(System.in)) {
return reader.nextInt();
}
}
private static int digitCount(int num) {
int count = 0;
while(num > 0) {
num = num / 10;
count++;
}
System.out.println("No of digit : " + count);
return count;
}
private static int power(int num, int count) {
int sum = 0;
while(num > 0) {
int result = 1;
int r2 = num % 10;
num /= 10;
for(int digit = count; digit > 0; digit--) {
result *= r2;
}
sum += result;
}
System.out.println("Sum : " + sum);
return sum;
}
public static boolean isArmstrongNumber(int num) {
int count = digitCount(num);
int sum = power(num, count);
return sum == num;
}
}
这是结果:
371
No of digit : 3
Sum : 371
armstrong number
希望此代码对您有所帮助。