博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
四平方和
阅读量:4562 次
发布时间:2019-06-08

本文共 1693 字,大约阅读时间需要 5 分钟。

四平方和定理,又称为拉格朗日定理:

每个正整数都可以表示为至多4个正整数的平方和。

如果把0包括进去,就正好可以表示为4个数的平方和。

比如:

5 = 0^2 + 0^2 + 1^2 + 2^27 = 1^2 + 1^2 + 1^2 + 2^2(^符号表示乘方的意思)

对于一个给定的正整数,可能存在多种平方和的表示法。

要求你对4个数排序:

0 <= a <= b <= c <= d

并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法

程序输入为一个正整数N (N<5000000)

要求输出4个非负整数,按从小到大排序,中间用空格分开

例如,输入:

5

则程序应该输出:

0 0 1 2

再例如,输入:

12

则程序应该输出:

0 2 2 2

再例如,输入:

773535

则程序应该输出:

1 1 267 838

资源约定:

峰值内存消耗 < 256M

CPU消耗 < 3000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

提交时,注意选择所期望的编译器类型。

 

穷举。

代码:

#include 
#include
#include
#include
#include
#include
#include
using namespace std;int main(){ int n; scanf("%d",&n); int d = sqrt(n); for(int i = 0;i <= d;i ++) { for(int j = i;j <= d;j ++) { for(int k = j;k <= d;k ++) { int d = n - i * i - j * j - k * k; int e = sqrt(d); if(e * e == d) { cout<
<<' '<
<<' '<
<<' '<

 

#include 
#include
#include
#include
#include
using namespace std;int ans[4],flag;void dfs(int k,int sum,int last) { if(k >= 3) { int d = sqrt(sum); if(d * d == sum) { ans[3] = d; flag = 1; } return; } for(int i = last;i * i <= sum;i ++) { if(flag) return; ans[k] = i; dfs(k + 1,sum - i * i,i); }}int main() { int n; scanf("%d",&n); dfs(0,n,0); for(int i = 0;i < 4;i ++) { if(i) putchar(' '); printf("%d",ans[i]); }}

 

转载于:https://www.cnblogs.com/8023spz/p/8419302.html

你可能感兴趣的文章
安卓开发环境搭建(转)
查看>>
Harris角点检测
查看>>
Struts2的处理流程及为Action的属性注入值
查看>>
设计中最常用的CSS选择器
查看>>
Maven项目打包成可执行Jar文件
查看>>
nginx http proxy 正向代理
查看>>
对BFC的总结
查看>>
23醒
查看>>
win7每天出现taskeng.exe进程的解决方案
查看>>
React Children
查看>>
大数据等最核心的关键技术:32个算法
查看>>
Maven多模块项目搭建
查看>>
redis列表list
查看>>
雷林鹏分享: C# 简介
查看>>
ORA-12505: TNS: 监听程序当前无法识别连接描述符中所给出的SID等错误解决方法
查看>>
实用类-<Math类常用>
查看>>
构建之法阅读笔记之四
查看>>
10.15习题2
查看>>
Windows Server 2008 R2 备份与恢复详细实例
查看>>
Ubuntu上kubeadm安装Kubernetes集群
查看>>