結果

問題 No.955 ax^2+bx+c=0
ユーザー 37zigen37zigen
提出日時 2019-12-18 01:42:26
言語 Java21
(openjdk 21)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 2,321 bytes
コンパイル時間 2,370 ms
コンパイル使用メモリ 80,052 KB
実行使用メモリ 58,060 KB
最終ジャッジ日時 2023-10-19 02:06:32
合計ジャッジ時間 22,757 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 141 ms
57,920 KB
testcase_01 AC 144 ms
57,960 KB
testcase_02 AC 139 ms
57,624 KB
testcase_03 AC 137 ms
55,992 KB
testcase_04 AC 136 ms
57,420 KB
testcase_05 AC 138 ms
57,396 KB
testcase_06 AC 142 ms
57,392 KB
testcase_07 AC 141 ms
57,380 KB
testcase_08 AC 138 ms
57,380 KB
testcase_09 AC 143 ms
57,368 KB
testcase_10 AC 139 ms
57,476 KB
testcase_11 AC 139 ms
57,404 KB
testcase_12 AC 138 ms
57,804 KB
testcase_13 AC 138 ms
57,808 KB
testcase_14 AC 140 ms
57,812 KB
testcase_15 AC 139 ms
57,448 KB
testcase_16 AC 141 ms
57,516 KB
testcase_17 AC 140 ms
57,576 KB
testcase_18 AC 137 ms
57,352 KB
testcase_19 AC 142 ms
57,444 KB
testcase_20 AC 138 ms
57,356 KB
testcase_21 AC 136 ms
55,816 KB
testcase_22 AC 139 ms
57,840 KB
testcase_23 AC 137 ms
57,408 KB
testcase_24 AC 138 ms
57,884 KB
testcase_25 AC 137 ms
57,884 KB
testcase_26 AC 138 ms
57,680 KB
testcase_27 AC 137 ms
58,032 KB
testcase_28 AC 138 ms
57,672 KB
testcase_29 AC 137 ms
57,452 KB
testcase_30 AC 139 ms
57,448 KB
testcase_31 AC 137 ms
57,888 KB
testcase_32 AC 140 ms
57,472 KB
testcase_33 AC 143 ms
57,664 KB
testcase_34 AC 140 ms
57,900 KB
testcase_35 AC 139 ms
57,888 KB
testcase_36 AC 139 ms
57,896 KB
testcase_37 AC 141 ms
57,472 KB
testcase_38 AC 142 ms
57,692 KB
testcase_39 AC 141 ms
57,628 KB
testcase_40 AC 140 ms
57,908 KB
testcase_41 AC 140 ms
57,520 KB
testcase_42 AC 140 ms
57,916 KB
testcase_43 AC 142 ms
57,916 KB
testcase_44 AC 141 ms
57,916 KB
testcase_45 AC 141 ms
57,944 KB
testcase_46 AC 141 ms
57,928 KB
testcase_47 AC 141 ms
57,920 KB
testcase_48 AC 141 ms
57,476 KB
testcase_49 AC 141 ms
57,456 KB
testcase_50 AC 141 ms
57,912 KB
testcase_51 AC 142 ms
57,468 KB
testcase_52 AC 139 ms
56,016 KB
testcase_53 AC 141 ms
57,588 KB
testcase_54 AC 143 ms
57,792 KB
testcase_55 AC 141 ms
57,896 KB
testcase_56 AC 141 ms
57,532 KB
testcase_57 AC 142 ms
57,548 KB
testcase_58 AC 137 ms
57,884 KB
testcase_59 AC 142 ms
57,612 KB
testcase_60 AC 143 ms
57,872 KB
testcase_61 AC 139 ms
57,396 KB
testcase_62 AC 143 ms
57,476 KB
testcase_63 AC 142 ms
57,560 KB
testcase_64 AC 141 ms
57,376 KB
testcase_65 AC 138 ms
56,048 KB
testcase_66 AC 138 ms
57,436 KB
testcase_67 AC 144 ms
57,476 KB
testcase_68 AC 138 ms
57,840 KB
testcase_69 AC 145 ms
57,472 KB
testcase_70 AC 145 ms
57,700 KB
testcase_71 AC 138 ms
57,392 KB
testcase_72 AC 140 ms
57,460 KB
testcase_73 AC 142 ms
57,512 KB
testcase_74 AC 141 ms
57,908 KB
testcase_75 AC 140 ms
57,612 KB
testcase_76 AC 142 ms
57,364 KB
testcase_77 AC 142 ms
55,508 KB
testcase_78 AC 141 ms
57,480 KB
testcase_79 AC 138 ms
57,516 KB
testcase_80 AC 137 ms
57,388 KB
testcase_81 AC 140 ms
57,856 KB
testcase_82 AC 139 ms
57,880 KB
testcase_83 AC 140 ms
57,828 KB
testcase_84 AC 136 ms
57,508 KB
testcase_85 AC 137 ms
57,996 KB
testcase_86 AC 138 ms
57,960 KB
testcase_87 AC 138 ms
55,888 KB
testcase_88 AC 137 ms
57,876 KB
testcase_89 AC 136 ms
57,880 KB
testcase_90 AC 136 ms
57,544 KB
testcase_91 AC 137 ms
57,792 KB
testcase_92 AC 136 ms
57,348 KB
testcase_93 AC 145 ms
57,368 KB
testcase_94 AC 140 ms
58,060 KB
testcase_95 AC 141 ms
57,904 KB
testcase_96 AC 142 ms
57,660 KB
testcase_97 AC 139 ms
57,492 KB
testcase_98 AC 139 ms
57,916 KB
testcase_99 AC 139 ms
57,468 KB
testcase_100 AC 137 ms
57,440 KB
testcase_101 AC 139 ms
57,532 KB
testcase_102 AC 137 ms
57,808 KB
testcase_103 AC 140 ms
57,456 KB
testcase_104 AC 139 ms
57,832 KB
testcase_105 AC 137 ms
57,776 KB
testcase_106 AC 136 ms
55,348 KB
testcase_107 AC 141 ms
57,424 KB
testcase_108 AC 140 ms
57,428 KB
testcase_109 AC 141 ms
57,428 KB
testcase_110 AC 142 ms
57,424 KB
testcase_111 AC 141 ms
57,384 KB
testcase_112 AC 141 ms
57,860 KB
testcase_113 AC 140 ms
57,820 KB
testcase_114 AC 143 ms
57,380 KB
testcase_115 AC 139 ms
57,832 KB
testcase_116 AC 139 ms
57,456 KB
testcase_117 AC 139 ms
57,860 KB
testcase_118 AC 141 ms
57,388 KB
testcase_119 AC 140 ms
57,444 KB
testcase_120 AC 138 ms
57,800 KB
testcase_121 AC 142 ms
57,780 KB
testcase_122 AC 144 ms
57,464 KB
testcase_123 AC 143 ms
57,540 KB
testcase_124 AC 141 ms
57,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.*;
import java.io.*;
import java.math.*;

class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
//		for (long a = -10; a <= 10; ++a)
//			for (long b = -10; b <= 10; ++b)
//				for (long c = -10; c <= 10; ++c)
		new Main().run(a, b, c);
	}

	void run(long a, long b, long c) {
		if (a < 0) {
			a *= -1;
			b *= -1;
			c *= -1;
		}
		if (a == 0 && b == 0 && c == 0) {// 0x^2+0x+0=0
			System.out.println(-1);
			return;
		} else if (a == 0 && b == 0 && c != 0) {// 0x^2+0x+c=0
			System.out.println(0);
		} else if (a == 0) {// 0x^2+bx+c=0
			// b!=0
			System.out.println(1);
			System.out.println(-(double) c / b);
		} else if (a != 0) {
			BigDecimal DD = BigDecimal.valueOf(b * b)
					.subtract(BigDecimal.valueOf(4).multiply(BigDecimal.valueOf(a * c)));
			if (DD.compareTo(BigDecimal.ZERO) == -1) {
				System.out.println(0);
			} else if (DD.compareTo(BigDecimal.ZERO) == 0) {
				System.out.println(1);
				System.out.println(BigDecimal.valueOf(-b).divide(BigDecimal.valueOf(2 * a), 50, BigDecimal.ROUND_HALF_UP)
						.doubleValue());
			} else {
				System.out.println(2);
				double x1 = BigDecimal.valueOf(-b).subtract(sqrt(DD, 50))
						.divide(BigDecimal.valueOf(2 * a), 50, BigDecimal.ROUND_HALF_UP).doubleValue();
				double x2 = BigDecimal.valueOf(-b).add(sqrt(DD, 50))
						.divide(BigDecimal.valueOf(2 * a), 50, BigDecimal.ROUND_HALF_UP).doubleValue();// (double)(-b+Math.sqrt(DD))/(2*a));
				if (x1 > x2) {
					double tmp = x1;
					x1 = x2;
					x2 = tmp;
				}
				System.out.println(x1);
				System.out.println(x2);
			}

		}
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

	public static BigDecimal sqrt(BigDecimal a, int scale) {
		// とりあえずdoubleのsqrtを求める
		BigDecimal x = new BigDecimal(Math.sqrt(a.doubleValue()), MathContext.DECIMAL64);
		if (scale < 17)
			return x;

		BigDecimal b2 = new BigDecimal(2);
		for (int tempScale = 16; tempScale < scale; tempScale *= 2) {
			// x = x - (x * x - a) / (2 * x);
			x = x.subtract(x.multiply(x).subtract(a).divide(x.multiply(b2), scale, BigDecimal.ROUND_HALF_EVEN));
		}
		return x;
	}
}
0