public class Test4 {
public static void main(String[] args) {
int a[];a=new int[100];
int j = 0;
int s,n,m=1;//m用于记录每个数的反过来念的值
for(int i=1;i<200;i++){
s=i*i;
if(s<10){
a[j++]=s;
}
else{
do{
n=s%10;
s=s/10;
m=m*10+n;
}while(s!=0);
if(m==i*i){
a[j++]=m;
}
}
}
for(int i=0;i<a.length&&a[i]!=0;i++){
System.out.print(a[i]+" ");
}
}
}
求200以内数的平方中所有的回文数,存在数组中 最后打印出;但是只能打印出个位数字 求大神指导
public static void main(String[] args) {
int a[];a=new int[100];
int j = 0;
int s,n,m=1;//m用于记录每个数的反过来念的值
for(int i=1;i<200;i++){
s=i*i;
if(s<10){
a[j++]=s;
}
else{
do{
n=s%10;
s=s/10;
m=m*10+n;
}while(s!=0);
if(m==i*i){
a[j++]=m;
}
}
}
for(int i=0;i<a.length&&a[i]!=0;i++){
System.out.print(a[i]+" ");
}
}
}
求200以内数的平方中所有的回文数,存在数组中 最后打印出;但是只能打印出个位数字 求大神指导
int a[];
a = new int[100];
int j = 0;
int s, n, m = 1; //m用于记录每个数的反过来念的值
for (int i = 1; i < 200; i++) {
s = i * i; if (isSymmetrical(s)) {
a[j++] = s;
}
} for (int i = 0; i < a.length && a[i] != 0; i++) {
System.out.print(a[i] + " ");
}
} // 把数字转化成字符串,也可看成是数组,然后再去判断这个数组中的元素是否对称
// 如果不用数组的方式,还可以用栈的方式来解决这个问题
public static boolean isSymmetrical(int value) {
String str = "" + value; int length = str.length();
for (int i = 0; i < length / 2; ++i) {
if (str.charAt(i) != str.charAt(length - 1 - i)) {
return false;
}
} return true;
}
}输出:
1 4 9 121 484 676 10201 12321 14641
int a[];
a = new int[100];
int j = 0;
int s, n, m = 1; //m用于记录每个数的反过来念的值
for (int i = 1; i < 200; i++) {
s = i * i;
int result = reverse(s); if (s == result) {
a[j++] = s;
}
} for (int i = 0; i < a.length && a[i] != 0; i++) {
System.out.print(a[i] + " ");
}
} // 把一个数反转
public static int reverse(int number) {
int result = 0; while (number != 0) {
result = result * 10 + number % 10;
number /= 10;
} return result;
}
}
public class Test4
{
public static void main(String[] args)
{
int a[];
a=new int[100];
int j = 0;
int s=0,n=0,m=0; //m用于记录每个数的反过来念的值,赋初值。
for(int i=1;i<200;i++)
{
m=0; //每次循环m清零。
s=i*i;
if(s<10)
{
a[j++]=s;
}
else
{
do
{
n=s%10;
s=s/10;
m=(m+n)*10; //这改一下
}while((s/10)!=0); //改一下循环条件。
m=m+s; //加上最高位。
if(m==i*i)
{
a[j++]=m;
}
}
}
for(int i=0;i<a.length&&a[i]!=0;i++)
{
System.out.print(a[i]+" ");
}
}
}