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

aabb问题

时间:2017-01-10 19:32:34      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:相等   name   mat   iostream   i++   存在   最大   names   ret   

输出所有形如aabb的4位完全平方数(即前两位数字相等,后两位也相等),此题也就是7744问题

此题不可用 if(sqrt(n)==floor(sqrt(n))),判断sqrt(n)是否为整数,因为存在一个精度问题。

floor函数用法:floor(x),有时候也写做Floor(x),其功能是“向下取整”,或者说“向下舍入”,即取不大于x的最大整数(与“四舍五入”不同,下取整是直接取按照数轴上最接近要求的值左边的值,也就是不大于要求的值的最大的那个)。

实例:floor(3.14) = 3.0
   floor(9.999999) = 9.0

原型:#include <math.h>
   double floor( double arg );

详细用法链接:http://baike.baidu.com/link?url=kBABJWlmZ2nMTs9c_P1MKi6WCotbeFBY2aS9BP2mLrc6C9IsCV636tiBfFP1-YulZSuntRzpV5ePyn_mNCLP55-JIELJQL88AlJ0j1uDOxC

方法(1)

#include <iostream>
using namespace std;
int main()
{
    for(int i=1;i<10;i++)
        for(int j=0;j<10;j++)
        {
            int n=i*1100+j*11;
            int m=floor(sqrt(n)+0.5);
            if(m*m==n)
                cout<<n<<endl;
        }
        return 0;
}

 

方法(2)枚举法

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
int main()
{
    for(int x=30;;x++)
    {
        int n=x*x;
        if(n<1000)
            continue;
        if(n>9999)
            break;
        //取高位aa
        int hi=n/100;
        //取低位bb
        int lo=n%100;
        if(hi/10==hi%10&&lo/10==lo%10)
            cout<<n<<endl;
    }
    return 0;
}

 

aabb问题

标签:相等   name   mat   iostream   i++   存在   最大   names   ret   

原文地址:http://www.cnblogs.com/Aftersoon-sun/p/6270403.html

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