寻找数组中回文数的各位数字之和。

huangapple go评论80阅读模式
英文:

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(&quot;Enter the number of array elements&quot;);
int n = sc.nextInt();
int[] inp = new int[n];
System.out.println(&quot;enter the elements&quot;);
for(int i = 0; i&lt; 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&lt;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&gt;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&gt;9)
{
sum1=singledigitsum(sum1);
}
return sum1;
}
}

答案1

得分: 1

  1. 输入数字
  2. 检查哪些数字是回文数。
  3. 如果该数字是回文数,则找出其各位数字的和。

代码:

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;
    }
}
英文:
  1. Enter numbers

  2. Check which numbers are palindromes.

  3. 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(&quot;Enter the number of array elements&quot;);
int n = sc.nextInt();
int[] inp = new int[n];
System.out.println(&quot;enter the elements&quot;);
for(int i = 0; i&lt; n ;i++)
{
inp[i] = sc.nextInt();
}
for(int i=0;i&lt;n;i++)
{
if(sumOfPalindrome.isPallindrone(inp[i]))
{
System.out.println(&quot;No -&gt; &quot;+inp[i]+&quot; Sum-&gt;&quot;+sumOfPalindrome.sumOfDigits(inp[i]));
}
}
}//
public boolean isPallindrone(int no)
{
int r,sum=0,temp;    
temp=no;    
while(no&gt;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(&quot;Enter the number of array elements&quot;);
int n = sc.nextInt();
int[] inp = new int[n];
System.out.println(&quot;enter the elements&quot;);
for(int i = 0; i&lt; 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&lt;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&gt;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&gt;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.

huangapple
  • 本文由 发表于 2020年10月27日 12:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64548498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定