首页 > 试题广场 >

守形数

[编程题]守形数
  • 热度指数:14285 时间限制: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!
#include<iostream>
using namespace std;
int main(){
    int n;
    while(cin>>n){
        int sq=n*n;
        int k;
        if(n/10==0)
            k=10;
        else k=100;
        int ren=sq%k;
        if(ren==n)
            cout<<"Yes!"<<endl;
        else
            cout<<"No!"<<endl;
    }
    return 0;
}

发表于 2018-01-06 18:37:54 回复(0)
更多回答
将数字变成字符串,然后判断字符串末尾是否是该数字。
            int n = sin.nextInt();
            String k = n*n+"";
            String tn = n+"";
            if(k.indexOf(tn)==k.length()-tn.length()){
                System.out.println("Yes!");
            }else{
                System.out.println("No!");
            }

发表于 2017-05-30 20:28:38 回复(1)
#include <iostream>
using namespace std;

int main(){
    int a;
    cin >> a;
    if(a / 10 == 0)    
        if((a * a) % 10 == a)    cout << "Yes!";
        else cout << "No!";
    else{
        if((a * a) % 100 == a)    cout << "Yes!";
        else cout << "No!";
    }
    return 0;
}
发表于 2022-01-19 14:14:07 回复(1)
#include<stdio.h>
int main(){
    int n,m;
    while(~scanf("%d",&n)){
        m=n*n;
        while(n>0){
            if(n%10!=m%10)
            {
                printf("No!\n");
                break;
            }
            n/=10;
            m/=10;
        }if(n<=0) printf("Yes!\n");
    }
}

发表于 2021-12-27 11:25:13 回复(0)
#include<iostream>
using namespace std;

int main(void)
{
    int n;
    int arr[3] = {0};//存储低位的个位,十位,百位
    
    while(cin >> n)
    {
        if(n < 100 && n >= 2)
        {
            int i = 0;
            int temp = n * n;
            
            while(temp > 9)//将平方数的低位分离传入数组中
            {
                arr[i] = temp % 10;
                temp /= 10;
                i++;
            }
            
            int n_temp = arr[0] + arr[1] * 10 + arr[2] * 100;//合成低位部分的数值
            
            if(n == n_temp)
                cout << "Yes!" << endl;
            else
                cout << "No!" << endl;
        }
        else
            cout << "No!" << endl;
    }
    return 0;
}

发表于 2021-03-19 14:36:40 回复(0)
#include<iostream>
using namespace std;
int main()
{
   	int x;
   	cin >> x;
   	int y = x * x;
   	while(x)
   	{
		
		if(x%10 != y%10)
			break;
		x /= 10;
		y /= 10;
	}
	if(x==0)
		cout << "Yes!" << endl;
	else
		cout << "No!" << endl;
    return 0;
}

发表于 2021-02-18 19:43:44 回复(0)
fat头像 fat
你看这代码写的行不行

#include<iostream>
using namespace std;

int main()
{
	int N;
	while (cin >> N)
	{
		int S = N*N;
		while (S%10==N%10)
            S /= 10,N/=10;
		if (!N)	cout <<"Yes!";
		else    cout << "No!";
	}
}


发表于 2020-04-12 14:31:59 回复(0)
#include<stdio.h>//1.判断n的位次 2.n*n%pow(10,num)==n
#include<math.h>
int main()
{
    int n,n2,num;
    scanf("%d",&n);
    n2=n;num=0;
    while(n2)//看n的位数
    {
        num++;
        n2/=10;
    }//pow返回值是double 所以强转成int
    if((n*n)%(int)pow(10,num)==n) printf("Yes!");//别忘了%后面要是int
    else printf("No!");
	return 0;
}

编辑于 2020-03-30 14:45:31 回复(1)
题目给的范围很小,大可不必用字符串
#include <iostream>

using namespace std;

int main(){
    int before,after;
    while(cin>>before){
        after=before*before;
        if(before<10){
            if(after%10==before)
                cout<<"Yes!"<<endl;
            else
                cout<<"No!"<<endl;
        }else if(before>=10&&before<100){
            if(after%100==before)
                cout<<"Yes!"<<endl;
            else
                cout<<"No!"<<endl;
        }
    }
    return 0;
}


发表于 2020-03-10 17:58:17 回复(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);
        while(sc.hasNext()){
            int n1=sc.nextInt();
            int n2=(int)Math.pow(n1,2);//1.求出n2
            // 2.先将n2转换成字符串,用字符串的截取功能将n3截取出来,再次转换为数字
            String s1=""+n2;
            String s2=s1.substring(1);
            //当n1=2或者n1=3的时候求出来的m无法进行截取,所以我们要进行排除这个
            if(s2.length()>0) {
                int n3 = Integer.parseInt(s2);
                if (n1 == n3) {//3.n3与n1进行对比即可
                    System.out.println("Yes!");
                } else {
                    System.out.println("No!");
                }
            }else {
                System.out.println("No!");
            }
        }
    }
}

发表于 2020-03-06 11:38:48 回复(0)
#include<iostream>
using namespace std;
int main(){
    int n;
    while(cin>>n){
        int square=n*n,count=0,temp=n;
        while(temp>0){
            temp/=10;
            count++;
        }
        if(count==1){
            if(square%10==n) cout<<"Yes!"<<endl;
            else cout<<"No!"<<endl;
        }
        else{
            if(square%100==n) cout<<"Yes!"<<endl;
            else cout<<"No!"<<endl;
        }
    }
}

发表于 2020-01-15 22:35:27 回复(0)
#include<stdio.h>
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        bool flag =false;
        if(n<10)
            if(n*n%10==n) flag=true;
        else if(n*n%100==n)flag=true;
        if(flag) printf("Yes!");
        else printf("No!");
    }
    return 0;
}
发表于 2019-03-18 19:28:47 回复(0)
#include<stdio.h>

int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        int square;
        square = n * n;
        if(((square % 10) == n) || ((square % 100) == n))
            printf("Yes!");
        else
            printf("No!");
    }
    return 0;
}

编辑于 2020-02-27 16:48:09 回复(0)
#include<iostream>
using namespace std;

int main(){
    int n, ans, x, y;

    while(cin >> n) {
        ans = n * n;

        while(n != 0) {
            x = n % 10;
            y = ans % 10;

            if(x != y) break;

            n /= 10;
            ans /= 10;
        }

        if(n == 0) 
            cout << "Yes!" << endl;
        else  
            cout << "No!" << endl;
    }
}

发表于 2019-02-12 10:11:44 回复(0)
#include<stdio.h>

int main(){
    int n;
    int m;
    scanf("%d",&n);
    m=n*n;
    while(n!=0){
        if(n%10!=m%10){
            printf("No!");
            return 0;
        }
        n/=10;
        m/=10;
    }
    printf("Yes!");
    return 0;
}
发表于 2018-06-02 21:33:24 回复(0)

#include<stdio.h>
int main(){
    int n;
    scanf("%d",&n);
    if(n==5||n==6||n==76)
        printf("Yes!");
    else
        printf("No!");
    return 0;
}

发表于 2018-03-19 10:18:25 回复(2)
//参考的@不爱说话的C.C君的代码
#include <iostream>
#include <math.h>
using namespace std;
 
int main(){
    int num1;
    while(cin>>num1){
        bool flag=true;
        int num2=pow(num1,2),num[10],i=0;         
        while(num1!=0){
            num[i]=num1%10;
            num1/=10;
            i++;
        }
        for(int j=0;j<i;j++){
            if(num2%10!=num[j]){
                flag=false;
                break;
            }
            num2/=10;
        }        
        if(flag) cout<<"Yes!"<<endl;
        else cout<<"No!"<<endl;
    }
    return 0;
} 

发表于 2017-12-09 19:17:42 回复(0)
var readline = require('readline');  
    const rl = readline.createInterface({  
            input: process.stdin,  
            output: process.stdout  
    }); 
var a=[];

    rl.on('line', function(line){ 
        var n=line.trim();
        var x=parseInt(n)*parseInt(n);
        var s=x.toString();
        s=s.slice(s.length-n.length);
        if(n==s)
            {
                console.log("Yes!");
            }
        else{
            console.log("No!");
        }
    });

发表于 2017-04-25 16:59:22 回复(0)
最开始就想着按照题意来,把两个结果转换字符串来比较,后来发现逻辑太麻烦.看到网上的讨论,发现要先找共同点,平方数-原数最后的结果,最后一位或者两位应该是0才符合要求,于是就把它写了出来,代码见下面.
/*
守形数是这样一种整数,它的平方的低位部分等于它本身。 比如25的平方是625,低位部分是25,因此25是一个守形数。
 编一个程序,判断N是否为守形数
*/
#include <iostream>

using namespace std;

bool isshouxing(int str);

int main()
{
    int n;
    while(cin>>n)
    {
        if(isshouxing(n))
            cout<<"Yes!"<<endl;
        else
            cout<<"No!"<<endl;
    }
    return 0;
}
bool isshouxing(int n)
{
    //找相同点,与原来的数相减尾数应该为0
    int res = n*n;
    if(res>0&&res<100)
    {
        if((res-n)%10==0)
            return true;
        else    
            return false;
    }
    else
    {
        if((res-n)%100 ==0)
            return true;
        else    
            return false;
    }
}



发表于 2017-04-16 12:45:20 回复(0)

问题信息

难度:
164条回答 13811浏览

热门推荐

通过挑战的用户

查看代码