蓝桥杯——2015年C++A组第1题:方程整数解【枚举】

一、题目

方程: a^2 + b^2 + c^2 = 1000
(或参见【图1.jpg】)


这个方程有整数解吗?有:a,b,c=6,8,30 就是一组解。
你能算出另一组合适的解吗?

请填写该解中最小的数字。

注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

二、思路

直接枚举。

由于a^2+b^2+c^2=1000,那么,a,b,c肯定小于1000的方根

三、题解

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    for (int a=1;a<sqrt(1000);a++)
    {
        for (int b=1;b<sqrt(1000);b++)
        {
            for (int c=1;c<sqrt(1000);c++)
            {
                if(a*a+b*b+c*c==1000)
                {
                    cout << a << " " << b << " " << c << " " << endl;
                }
            }
        }
    }
    return 0;
}

四、结果

6 8 30 
6 30 8 
8 6 30 
8 30 6 
10 18 24 
10 24 18 
18 10 24 
18 24 10 
24 10 18 
24 18 10 
30 6 8 
30 8 6 

Process finished with exit code 0

已经知道6 8 30已经组成一组解,所以另一组解是24 10 18 ,所以另一组解最小的数字是10,即是解

发布了57 篇原创文章 · 获赞 9 · 访问量 3584

猜你喜欢

转载自blog.csdn.net/Jayphone17/article/details/104239663