码迷,mamicode.com
首页 > 其他好文 > 详细

守形数

时间:2019-02-07 21:54:14      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:部分   一个   code   pow   std   复制   ret   space   out   

题目描述

守形数是这样一种整数,它的平方的低位部分等于它本身。 比如25的平方是625,低位部分是25,因此25是一个守形数。 编一个程序,判断N是否为守形数。

输入描述:

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

输出描述:

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

输入

复制
25
4

输出

复制
Yes!
No!
#include<iostream>
#include<math.h>
#include<string>
using namespace std;
bool keep_shape(int N)
{
    int M = N*N;
    string str= to_string(M);
    int len = str.length();
    int X = M - (str[0]-0)*pow(10,len-1);
    if(X==N)
        return true;
    else
        return false;
}
int main()
{
    int N;
    while(cin>>N)
    {
        if(keep_shape(N)==true)
            cout<<"Yes!"<<endl;
        else
            cout<<"No!"<<endl;
    }
    return 0;
}

C++ int 与 string相互转化:

int转化string:

to_string()函数:

string to_string (int val);

string to_string (long val);

string to_string (long long val);

string to_string (unsigned val);

string to_string (unsigned long val);

string to_string (unsigned long long val);

string to_string (float val);

string to_string (double val);

string to_string (long double val);

string转换成int:

1.采用标准库中atoi函数,对于其他类型也都有相应的库函数,比如浮点型atof(),long型atol()等等

string str = "123";
int n = atoi(str.c_str());
cout<<n; //123

 

守形数

标签:部分   一个   code   pow   std   复制   ret   space   out   

原文地址:https://www.cnblogs.com/ttzz/p/10355571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!