首页 > 试题广场 >

守形数

[编程题]守形数
  • 热度指数:14294 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
守形数是这样一种整数,它的平方的低位部分等于它本身。 比如25的平方是625,低位部分是25,因此25是一个守形数。 编一个程序,判断N是否为守形数。

输入描述:
输入包括1个整数N,2<=N<100。


输出描述:
可能有多组测试数据,对于每组数据,
输出"Yes!”表示N是守形数。
输出"No!”表示N不是守形数。
示例1

输入

25
4

输出

Yes!
No!
package test01;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null) {
            int x = Integer.parseInt(s);
            String xs = String.valueOf(x);
            String square = String.valueOf(x * x);
            if (square.startsWith(xs, square.length() - xs.length()))
                System.out.println("Yes!");
            else
                System.out.println("No!");
        }
    }
}

发表于 2021-02-24 11:26:45 回复(0)

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()){
            int n=sc.nextInt();
            isKeepShape(n);
        }
    }

    private static void isKeepShape(int n) {
        String s1=String.valueOf(n);
        String s2=String.valueOf(n*n);
        if (s2.endsWith(s1)){
            System.out.println("Yes!");
        }else {
            System.out.println("No!");
        }
    }
}

发表于 2021-01-28 13:14:49 回复(0)
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            if (String.valueOf(n * n).endsWith( String.valueOf(n))) 
                System.out.println("Yes!");
            else 
                System.out.println("No!");
        }
    }
}


发表于 2020-03-07 10:18:47 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m=n*n;
        String a = Integer.toString(n);
        String b = Integer.toString(m);
        if(b.substring(1,b.length()).equals(a)){
            System.out.println("Yes!");
        }else {
            System.out.println("No!");
        }
    }
}

发表于 2019-12-08 20:12:28 回复(0)
import java.util.Scanner;//一个很笨的办法 简单易懂
public class NumTest{
        public static void main(String[] args){
            Scanner sc=new Scanner(System.in);
            while(sc.hasNext()){
                int n=sc.nextInt();
                int b=n*n;
                int temp=0;
                int num=0;
                int j=0;
                int i=0;
                if(n>=2&&n<100) {
                    while (b > 9) {
                        temp = b % 10;
                        b = b / 10;
                        i=j;
                        if(i==0){
                            num=temp;
                        }
                       while(i>0){
                           num+=temp*10;
                           i--;
                       }
                        j++;
                    }
                }
                if(num==n){
                    System.out.println("Yes!");

                }else{
                    System.out.println("No!");
                }
            }
        }
}

发表于 2019-12-08 00:23:14 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int n = sc.nextInt();
            if(isShouXing(n)) {
                System.out.println("Yes!");
            }else {
                System.out.println("No!");
            }
        }
    }
    public static boolean isShouXing(int n) {
        int pingFang = n*n;
        if(n < 10) {
            if(n == pingFang%10) {
                return true;
            }else {
                return false;
            }
        }else {
            if(n == pingFang%100) {
                return true;
            }else {
                return false;
            }
        }
    }
}
发表于 2019-07-04 16:13:53 回复(0)
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String info;
        
        while((info = reader.readLine())!=null){
            int num = Integer.parseInt(info);
            int num2 = num>10?num*num%10:num*num%10;
            if(num2==num){
                System.out.println("Yes!");
            }else{
                System.out.println("No!");
            }
        }
        reader.close();
    }
}

发表于 2019-01-18 16:17:02 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int x = sc.nextInt();
            String sx = x+"";
            String vx = x*x+"";
            if(vx.endsWith(sx))
                System.out.println("Yes!");
            else
                System.out.println("No!");
        }
    }
}
发表于 2018-06-01 14:17:31 回复(0)

这种题目用字符串处理是最好的啦,本弱鸡不会什么特别简洁的Python写法(等等等等),向各位大佬们学习!

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            int n = in.nextInt();
            String s2 = String.valueOf(n);
            String s = n * n + "";
            String s1 = s.substring(s.length() - s2.length(), s.length());
            int temp = Integer.parseInt(s1);
            if(temp == n) {
                System.out.println("Yes!");
            }
            else {
                System.out.println("No!");
            }
        }
    }

}
发表于 2018-04-06 11:07:14 回复(0)
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    while(sc.hasNext()){
    int m=sc.nextInt();
    while(m<2 || m>100)
    {
    System.out.println("输入有误:请输入2-100之间的整数");
    m=sc.nextInt();
    }  
    int p=m*m;
    String str=" "+p;
    if(m<=3)
    {
    System.out.println("No!");
    }
    else
    {
    if(m<9)
    {
    int dw=Integer.parseInt(str.substring(2));
            if(m==dw)
            {
            System.out.println("Yes!");
            }
            else
            {
            System.out.println("No!");
            }
    }
    else
    {
    int dw=Integer.parseInt(str.substring(str.length()-2));
            if(m==dw)
            {
            System.out.println("Yes!");
            }
            else
            {
            System.out.println("No!");
            }
    }
   
       
    }
   
    }

}
}

发表于 2017-05-31 20:52:18 回复(0)
为什么会提示请检查是否存在数组越界等非法访问情况?
import java.util.*;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int a = sc.nextInt();
int b =a*a;
String A= Integer.toString(a);
String B =Integer.toString(b);
int c =B.length();
String C =B.substring(c-2);
if(A.equals(C))
System.out.println("Yes!");
else
System.out.println("No!");
}

}

}


发表于 2017-04-25 16:49:24 回复(0)
import java.util.Scanner;

public class test{

public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
while(scan.hasNext()){
int n=scan.nextInt();
int nn=n*n;
String str1=n+"";
String str2=nn+"";
if(str1.compareTo(str2.substring(str2.length()-str1.length(), str2.length()))==0){
System.out.println("Yes!");
}else{
System.out.println("No!");
}
}}
}
发表于 2017-04-19 13:19:16 回复(0)
import java.util.*;

public class Main{
    
    public static void main(String[] args){
      	Scanner sc = new Scanner(System.in);
        int i = 0;
        int result = 0;
        while (sc.hasNext()) {
            i = sc.nextInt();
            result = i * i;
            int last = 0;
            if(i >= 10){
                last += result % 10;
                result = result / 10;
                last += (result % 10) * 10;
            } else {
                last += result % 10;
            }
            if(last == i){
                System.out.println("Yes!");
            } else {
                System.out.println("No!");
            }
        }
    }
    
}

发表于 2017-03-23 21:10:07 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String []args){
        Scanner sc =new Scanner(System.in);
        while(sc.hasNextInt()){
            int n=sc.nextInt();
            if(n<10){
                 if(n*n%10==n){
                System.out.println("Yes!");
            }else{
                System.out.println("No!");
            }
            }else{
                if(n*n%100==n){
                System.out.println("Yes!");
            }else{
                System.out.println("No!");
            }
            }
            
        }
    }
}
发表于 2017-03-15 16:20:23 回复(0)
import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext())
		{
			int n=sc.nextInt();
			int sum=0;
			int i=1;
			int m=n*n;
			for(;m!=0;m/=10)
			{
				int g=m%10;
				g*=i;
				sum+=g;
				if(sum==n)
				{
					System.out.println("Yes!");
					break;
				}
				i*=10;
			}
			if(m==0)
			{
				System.out.println("No!");
			}
		}
	}
}
//写这么多干嘛,这么简单的题目

发表于 2017-03-14 11:14:48 回复(0)

问题信息

难度:
15条回答 13891浏览

热门推荐

通过挑战的用户

查看代码