A-圆周率日挑战_河南萌新联赛2024第(三)场:河南大学 (nowcoder.com)
1.python-code
from decimal import *
getcontext().prec = 50
pi = Decimal("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679")
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
mi = (float('inf'), 0, 0)
for i in range(n):
x = a[i][0]
y = a[i][1]
mi = min(mi, ((abs(pi - Decimal(x) / Decimal(y)), x, y)))
print(*mi[1:])
2.java-code
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int c=0;
BigDecimal b = new BigDecimal("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679");
BigDecimal k = new BigDecimal("4.0");
long[] x=new long[n];
long[] y=new long[n];
for(int i=0;i<n;i++){
x[i]=in.nextLong();
y[i]=in.nextLong();
BigDecimal dividend = new BigDecimal(x[i]);
BigDecimal divisor = new BigDecimal(y[i]);
BigDecimal result = dividend.divide(divisor, 100, RoundingMode.HALF_UP);
BigDecimal difference = result.subtract(b);
BigDecimal absValue = difference.abs();
int comparison = k.compareTo(absValue);
if(comparison>0){
k=absValue;
c=i;
}
if(comparison==0){
if(x[i]<x[c]){
c=i;
}
}
}
System.out.println(x[c]+" "+y[c]);
}
}