Java 数字回文
Java Number Palindrom
我想写一个回文程序,打印所有由两位数 (10-99) 相乘得到的回文数?
到目前为止,这是我的代码:
public class PrintPalindrom {
public int printPalindrom (int a, int b) {
int result = a*b;
int reverse = 0;
if (a >= 10 && a <= 99 && b >= 10 && b <= 99) {
while (result != 0) {
reverse = reverse * 10;
reverse = reverse + result % 10;
result = result/10;
System.out.println("palindrom is " + result);
}
} else {
System.out.println("Wrong numbers");
}
return result;
}
}
如何打印两位数(10-99)相乘产生的回文数?
我的方法暂时有效...
创建一个方法 (isPalindrome()
) returns 如果数字是回文数则为 true,否则为 false。您可以从 printPalindrom()
方法开始轻松创建它。然后,在你的 main()
:
int from = 10;
int to = 99;
for (int i = from; i <= to; i++) {
for (int j = from ; j <= to; j++) {
int mult = i*j;
if (isPalindrome(mult))
System.out.println(i + "x" + j + "=" + mult + " is plaindrome!");
}
}
请检查此代码:
public class PrintPalindrom {
public int printPalindrom (int a, int b) {
int result = a*b;
int temp = result;
int reverse = 0;
if (a >= 10 && a <= 99 && b >= 10 && b <= 99) {
while (temp != 0) {
reverse = reverse * 10;
reverse = reverse + result % 10;
temp = temp/10;
}
if(result == reverse) {
return result;
}
}
return 0;
}
int from = 10;
int to = 99;
for (int i = from; i < to; i++) {
for (int j = i + 1 ; j <= to; j++) {
int result = printPalindrom(i, j);
if(result > 0) {
System.out.println(result + ",");
}
}
}
}
你可以试试这个,如果允许使用堆栈来检查两个数字的乘积是否是回文。
import java.util.Stack;
public class PrintPalindrom {
public static void main(final String[] args) {
final int MIN = 10;
final int MAX = 99;
for (int i = MIN; i <= MAX; i++) {
for (int j = MIN ; j <= MAX; j++) {
new PrintPalindrom(i, j);
}
}
}
public PrintPalindrom(final int a, final int b) {
if (a >= 10 && a <= 99 && b >= 10 && b <= 99) {
final String result = String.valueOf(a * b); //Convert the product of the int to a String
final Stack<String> stack = new Stack<String>();
System.out.print(a + " * " + b + " = " + result + "\t-\t");
//Push each character from the in to a stack
for(int n = 0; n < result.length(); n++) {
stack.push(result.substring(n, n + 1));
}
//Check each character against the characters in the stack
for(int n = 0; n < result.length(); n++) {
if(!result.substring(n, n + 1).equals(stack.pop())) {
System.out.println("Not a palindrom");
return;
}
}
}
System.out.println("A palindrom");
}
}
public class Example {
public static void main (String[]args){
int start = 10;
int end = 99;
for (int i = start; i < end; i++) {
for (int j = i ; j <= end; j++) {
printPali(i,j);
}
}
}
public static void printPali(int i, int j) {
if(String.valueOf(i*j).equals(new StringBuilder(String.valueOf(i*j)).reverse().toString()))
System.out.println(i + " * " + j + " = " + i*j);
}
}
你可以通过这种方式实现创建 isPalindrome
函数,其中 return true
如果数字是回文,则检查所需范围内的每个乘法结果的回文,如果这是 true
.
public static boolean isPalindrome(int num){
int rnum = 0,digit;
int n = num;
while(n > 0){
digit = n % 10;
n = n / 10;
rnum = rnum * 10 + digit;
}
if(rnum == num){
return true;
}
else{
return false;
}
}
public static void main(String[] args) {
int a = 10 , b = 99;
for(int i = a; i <= b; i++){
for(int j = i; j <= b; j++){
if(isPalindrome(i * j)){
System.out.println("Mult Palindrome: " + i * j);
}
}
}
}
请注意,您可以将范围放在内部 for 循环 for(int j = a; j <= b; j++)
中,但它在乘法结果中是多余的,没有任何好处,因此将范围从 j=i
到b
.
希望对您有所帮助。
import java.util.Scanner;
public class Javatips {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int number = in.nextInt();
int reverseNumber = 0;
int copyNumber = number;
boolean isPalindrom = true;
while (copyNumber>0){
reverseNumber = 10*reverseNumber + copyNumber%10;
copyNumber=copyNumber/10;
if(reverseNumber==number){
isPalindrom = true;
} else {
isPalindrom = false;
}
}
if(isPalindrom){
System.out.println(number + " is palindrom");
} else {
System.out.println(number + " is not palindrom");
}
}
}
我想写一个回文程序,打印所有由两位数 (10-99) 相乘得到的回文数?
到目前为止,这是我的代码:
public class PrintPalindrom {
public int printPalindrom (int a, int b) {
int result = a*b;
int reverse = 0;
if (a >= 10 && a <= 99 && b >= 10 && b <= 99) {
while (result != 0) {
reverse = reverse * 10;
reverse = reverse + result % 10;
result = result/10;
System.out.println("palindrom is " + result);
}
} else {
System.out.println("Wrong numbers");
}
return result;
}
}
如何打印两位数(10-99)相乘产生的回文数?
我的方法暂时有效...
创建一个方法 (isPalindrome()
) returns 如果数字是回文数则为 true,否则为 false。您可以从 printPalindrom()
方法开始轻松创建它。然后,在你的 main()
:
int from = 10;
int to = 99;
for (int i = from; i <= to; i++) {
for (int j = from ; j <= to; j++) {
int mult = i*j;
if (isPalindrome(mult))
System.out.println(i + "x" + j + "=" + mult + " is plaindrome!");
}
}
请检查此代码:
public class PrintPalindrom {
public int printPalindrom (int a, int b) {
int result = a*b;
int temp = result;
int reverse = 0;
if (a >= 10 && a <= 99 && b >= 10 && b <= 99) {
while (temp != 0) {
reverse = reverse * 10;
reverse = reverse + result % 10;
temp = temp/10;
}
if(result == reverse) {
return result;
}
}
return 0;
}
int from = 10;
int to = 99;
for (int i = from; i < to; i++) {
for (int j = i + 1 ; j <= to; j++) {
int result = printPalindrom(i, j);
if(result > 0) {
System.out.println(result + ",");
}
}
}
}
你可以试试这个,如果允许使用堆栈来检查两个数字的乘积是否是回文。
import java.util.Stack;
public class PrintPalindrom {
public static void main(final String[] args) {
final int MIN = 10;
final int MAX = 99;
for (int i = MIN; i <= MAX; i++) {
for (int j = MIN ; j <= MAX; j++) {
new PrintPalindrom(i, j);
}
}
}
public PrintPalindrom(final int a, final int b) {
if (a >= 10 && a <= 99 && b >= 10 && b <= 99) {
final String result = String.valueOf(a * b); //Convert the product of the int to a String
final Stack<String> stack = new Stack<String>();
System.out.print(a + " * " + b + " = " + result + "\t-\t");
//Push each character from the in to a stack
for(int n = 0; n < result.length(); n++) {
stack.push(result.substring(n, n + 1));
}
//Check each character against the characters in the stack
for(int n = 0; n < result.length(); n++) {
if(!result.substring(n, n + 1).equals(stack.pop())) {
System.out.println("Not a palindrom");
return;
}
}
}
System.out.println("A palindrom");
}
}
public class Example {
public static void main (String[]args){
int start = 10;
int end = 99;
for (int i = start; i < end; i++) {
for (int j = i ; j <= end; j++) {
printPali(i,j);
}
}
}
public static void printPali(int i, int j) {
if(String.valueOf(i*j).equals(new StringBuilder(String.valueOf(i*j)).reverse().toString()))
System.out.println(i + " * " + j + " = " + i*j);
}
}
你可以通过这种方式实现创建 isPalindrome
函数,其中 return true
如果数字是回文,则检查所需范围内的每个乘法结果的回文,如果这是 true
.
public static boolean isPalindrome(int num){
int rnum = 0,digit;
int n = num;
while(n > 0){
digit = n % 10;
n = n / 10;
rnum = rnum * 10 + digit;
}
if(rnum == num){
return true;
}
else{
return false;
}
}
public static void main(String[] args) {
int a = 10 , b = 99;
for(int i = a; i <= b; i++){
for(int j = i; j <= b; j++){
if(isPalindrome(i * j)){
System.out.println("Mult Palindrome: " + i * j);
}
}
}
}
请注意,您可以将范围放在内部 for 循环 for(int j = a; j <= b; j++)
中,但它在乘法结果中是多余的,没有任何好处,因此将范围从 j=i
到b
.
希望对您有所帮助。
import java.util.Scanner;
public class Javatips {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int number = in.nextInt();
int reverseNumber = 0;
int copyNumber = number;
boolean isPalindrom = true;
while (copyNumber>0){
reverseNumber = 10*reverseNumber + copyNumber%10;
copyNumber=copyNumber/10;
if(reverseNumber==number){
isPalindrom = true;
} else {
isPalindrom = false;
}
}
if(isPalindrom){
System.out.println(number + " is palindrom");
} else {
System.out.println(number + " is not palindrom");
}
}
}