每天打卡一小时 第六天 编译四部曲
书接上回 求特殊方程的正整数解
第一部曲 自然语言
第一步 建立for 循环
第二步 数学计算 进行选择
第二部曲 流程图
第三部曲 伪代码(代码)
#include<bits/stdc++.h>
using namespace std;
bool check(auto x)
{
if(x-(int)x==0)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int n,count=0;
cin>>n;
for(int i=1;i<n;i++) //i代表x.枚举x,算出符合的y
{
if(((n-i*i)>=i*i)&&check(sqrt(n-i*i)))
{
cout<<i<<" "<<sqrt(n-i*i)<<" ";
count++;
}
}
if(count==0)
{
cout<<"No solution"<<endl;
}
return 0;
}
第四部曲 总结
可以运用数学,提高算法效率