AcWing 1221. 四平方和

Problem: AcWing 1221. 四平方和

思路

这是一个四平方和的问题,我们需要找到四个整数a、b、c和d,使得a^2 + b^2 + c^2 + d^2 = n。我们可以通过三层循环来枚举a、b和c的值,然后计算d的值。如果a^2 + b^2 + c^2 + d^2 = n,那么我们就找到了一个解。

解题方法

我们首先枚举a的值,从0开始,直到a^2 * 4 > n。然后对于每一个a的值,我们枚举b的值,从a开始,直到a^2 + b^2 * 3 > n。然后对于每一个b的值,我们枚举c的值,从b开始,直到a^2 + b^2 + c^2 * 2 > n。然后我们计算d的值,d = sqrt(n - a^2 - b^2 - c2)。如果a2 + b^2 + c^2 + d^2 = n,那么我们就找到了一个解,打印出a、b、c和d的值,并结束程序。

复杂度

时间复杂度:

我们需要枚举a、b和c的值,所以时间复杂度为 O ( n ( 3 / 2 ) ) O(n^(3/2)) O(n(3/2))

空间复杂度:

我们只需要常数的空间来存储a、b、c和d的值,所以空间复杂度为 O ( 1 ) O(1) O(1)

Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Main {
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	static StreamTokenizer sr = new StreamTokenizer(in);
	static int n;

	public static void main(String[] args) throws IOException {
		n = nextInt();
		for (int a = 0; a * a * 4 <= n; a++) {
			for (int b = a; a * a + b * b * 3  <= n; b++) {
				for (int c = b; a * a + b * b + c * c * 2 <= n; c++) {
					int d = (int) Math.sqrt(n - a * a - b * b - c * c);
					if (a * a + b * b + c * c + d * d == n) {
						out.println(a + " " + b + " " + c + " " + d);
						out.flush();
						return;
					}
				}
			}
		}

	}

	static int nextInt() throws IOException {
		sr.nextToken();
		return (int) sr.nval;
	}

}

相关推荐

  1. AcWing 1221. 平方和

    2024-03-28 10:02:04       35 阅读
  2. AcWing:4662. 因数平方和

    2024-03-28 10:02:04       62 阅读
  3. AcWing 1227. 分巧克力

    2024-03-28 10:02:04       40 阅读
  4. AcWing 1211. 蚂蚁感冒

    2024-03-28 10:02:04       43 阅读
  5. AcWing--因数平方和-->数论,整数分块

    2024-03-28 10:02:04       61 阅读
  6. 阿里云大数据ACAACP复习题(121~140)

    2024-03-28 10:02:04       51 阅读
  7. AcWing 1229.日期问题(枚举题,细节多)

    2024-03-28 10:02:04       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-28 10:02:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-28 10:02:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-28 10:02:04       82 阅读
  4. Python语言-面向对象

    2024-03-28 10:02:04       91 阅读

热门阅读

  1. shutil模块篇

    2024-03-28 10:02:04       38 阅读
  2. 手机安卓系统内嵌测试代码分享

    2024-03-28 10:02:04       48 阅读
  3. 在 Android 系统中,窗口(Window)按照功能和层级

    2024-03-28 10:02:04       44 阅读
  4. 视觉循迹小车(旭日x3派、摄像头、循迹)

    2024-03-28 10:02:04       43 阅读
  5. 2023.03.28

    2024-03-28 10:02:04       44 阅读
  6. 软考 - 软件架构设计师 - 架构风格

    2024-03-28 10:02:04       43 阅读
  7. 【React】React 组件 API

    2024-03-28 10:02:04       46 阅读
  8. 深入理解 React 中的 children props 和 render props

    2024-03-28 10:02:04       51 阅读
  9. 11 React 组件通信 父传子

    2024-03-28 10:02:04       40 阅读
  10. React系列之常用ReactHook

    2024-03-28 10:02:04       38 阅读
  11. MySQL 8.0 支持对单个数据库设置只读!

    2024-03-28 10:02:04       40 阅读
  12. MySQL数据库基础篇-SQL

    2024-03-28 10:02:04       42 阅读