英文:
To find the single digit sum of palindrome numbers in array
问题
我尝试检查回文数字并找到回文数字的个位数之和,但是我的代码没有返回正确的值(我发现很难从循环中返回总和的值)。有人可以帮我找出错误。任何帮助都将受到赞赏。
import java.util.Scanner;
public class SumOfPalindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of array elements");
int n = sc.nextInt();
int[] inp = new int[n];
System.out.println("enter the elements");
for(int i = 0; i < n; i++) {
inp[i] = sc.nextInt();
}
System.out.println(SumOfPalindromeNumber(inp));
}
private static int SumOfPalindromeNumber(int[] inp ) {
int sumpal = 0;
for(int i = 0; i < inp.length; i++) {
int rem = 0;
int sum = 0;
while(inp[i] != 0) {
rem = inp[i] % 10;
sum = (sum * 10) + rem;
inp[i] /= 10;
}
if(inp[i] == sum) {
sumpal += inp[i];
if(sumpal > 9) {
sumpal = singledigitsum(sumpal);
}
}
}
return sumpal;
}
private static int singledigitsum(int sumpal) {
int rem1 = 0;
int sum1 = 0;
while(sumpal != 0) {
rem1 = sumpal % 10;
sum1 += rem1;
sumpal /= 10;
}
if(sum1 > 9) {
sum1 = singledigitsum(sum1);
}
return sum1;
}
}
英文:
I tried to check the palindrome number and find the single digit sum of the palindrome numbers, but my code is not returning the proper value (I am finding it difficult to return the value of sum from the loops). Can anyone help me getting the mistake. Any help will be appriciated.
import java.util.Scanner;
public class SumOfPalindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of array elements");
int n = sc.nextInt();
int[] inp = new int[n];
System.out.println("enter the elements");
for(int i = 0; i< n ;i++)
{
inp[i] = sc.nextInt();
}
System.out.println(SumOfPalindromeNumber(inp));
}
private static int SumOfPalindromeNumber(int[] inp )
{
int sumpal =0;
for(int i = 0; i<inp.length;i++)
{
int rem =0;
int sum = 0;
while(inp[i]!=0)
{
rem = inp[i]%10;
sum=(sum*10)+rem;
inp[i]/=10;
}
if(inp[i]==sum)
{
sumpal+=inp[i];
if(sumpal>9)
{
sumpal=singledigitsum(sumpal);
}
}
}
return sumpal;
}
private static int singledigitsum(int sumpal)
{
int rem1 = 0;
int sum1 = 0;
while(sumpal!=0)
{
rem1=sumpal%10;
sum1+=rem1;
sumpal/=10;
}
if(sum1>9)
{
sum1=singledigitsum(sum1);
}
return sum1;
}
}
答案1
得分: 1
- 输入数字
- 检查哪些数字是回文数。
- 如果该数字是回文数,则找出其各位数字的和。
代码:
import java.util.Scanner;
public class SumOfPalindrome {
public static void main(String[] args) {
SumOfPalindrome sumOfPalindrome = new SumOfPalindrome();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of array elements");
int n = sc.nextInt();
int[] inp = new int[n];
System.out.println("enter the elements");
for (int i = 0; i < n; i++) {
inp[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
if (sumOfPalindrome.isPalindrome(inp[i])) {
System.out.println("No -> " + inp[i] + " Sum -> " + sumOfPalindrome.sumOfDigits(inp[i]));
}
}
}
public boolean isPalindrome(int no) {
int r, sum = 0, temp;
temp = no;
while (no > 0) {
r = no % 10; //获取余数
sum = (sum * 10) + r;
no = no / 10;
}
if (temp == sum) {
return true;
} else {
return false;
}
}
public int sumOfDigits(int no) {
int sum = 0;
while (no != 0) {
sum = sum + no % 10;
no = no / 10;
}
return sum;
}
}
我没有理解您代码中 sumpal
的目的。您想要一个返回数组中回文数之和的方法。我已经注释掉了错误的部分。
import java.util.Scanner;
public class SumOfPalindrome_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of array elements");
int n = sc.nextInt();
int[] inp = new int[n];
System.out.println("enter the elements");
for (int i = 0; i < n; i++) {
inp[i] = sc.nextInt();
}
//System.out.println(SumOfPalindromeNumber(inp));
SumOfPalindromeNumber(inp);
}
private static void SumOfPalindromeNumber(int[] inp) {
int sumpal = 0;
for (int i = 0; i < inp.length; i++) {
int rem = 0;
int sum = 0;
int num = inp[i]; // 修正
while (num != 0) { // 修正
rem = num % 10;
sum = (sum * 10) + rem;
num /= 10; // 修正
}
if (inp[i] == sum) {
System.out.println(singleDigitSum(inp[i]));
}
}
}
private static int singleDigitSum(int sumpal) {
int rem1 = 0;
int sum1 = 0;
while (sumpal != 0) {
rem1 = sumpal % 10;
sum1 += rem1;
sumpal /= 10;
}
if (sum1 > 9) {
sum1 = singleDigitSum(sum1);
}
return sum1;
}
}
英文:
-
Enter numbers
-
Check which numbers are palindromes.
-
If that number is a palindrome then find sum of its digits.
Code:
import java.util.Scanner;
public class SumOfPalindrome {
public static void main(String[] args) {
SumOfPalindrome sumOfPalindrome=new SumOfPalindrome();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of array elements");
int n = sc.nextInt();
int[] inp = new int[n];
System.out.println("enter the elements");
for(int i = 0; i< n ;i++)
{
inp[i] = sc.nextInt();
}
for(int i=0;i<n;i++)
{
if(sumOfPalindrome.isPallindrone(inp[i]))
{
System.out.println("No -> "+inp[i]+" Sum->"+sumOfPalindrome.sumOfDigits(inp[i]));
}
}
}//
public boolean isPallindrone(int no)
{
int r,sum=0,temp;
temp=no;
while(no>0){
r=no%10; //getting remainder
sum=(sum*10)+r;
no=no/10;
}
if(temp==sum)
{
return true;
}
else
{
return false;
}
}
public int sumOfDigits(int no)
{
int sum = 0;
while (no != 0)
{
sum = sum + no % 10;
no = no/10;
}
return sum;
}
}
I did not understand the purpose of sumpal in your code. You wanted a method which return sumOfPalindromes in an array. I commented the part which was wrong.
import java.util.Scanner;
public class SumOfPalindrome_1
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of array elements");
int n = sc.nextInt();
int[] inp = new int[n];
System.out.println("enter the elements");
for(int i = 0; i< n ;i++)
{
inp[i] = sc.nextInt();
}
//System.out.println(SumOfPalindromeNumber(inp));
SumOfPalindromeNumber(inp);
}
private static void SumOfPalindromeNumber(int[] inp )
{
int sumpal =0;
for(int i = 0; i<inp.length;i++)
{
int rem =0;
int sum = 0;
while(inp[i]!=0)
{
rem = inp[i]%10;
sum=(sum*10)+rem;
inp[i]/=10;
}
if(inp[i]==sum)
{
// sumpal+=inp[i];
/*if(sumpal>9)
{
sumpal=singledigitsum(sumpal);
}
*/
System.out.println(singleDigitSum(inp[i]));
}
}
}
private static int singleDigitSum(int sumpal)
{
int rem1 = 0;
int sum1 = 0;
while(sumpal!=0)
{
rem1=sumpal%10;
sum1+=rem1;
sumpal/=10;
}
if(sum1>9)
{
sum1=singleDigitSum(sum1);
}
return sum1;
}
}
答案2
得分: 0
抱歉,如果情况并非如此,但这似乎是您正在寻找编码测试答案的情况。
英文:
Apologies if this is not the case but this seems like you are looking for answers to a coding test.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论