Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.9 kB
2
Indexable
import java.util.Scanner;

public class MagneticMaterials {

    // A constant for the magnetic constant k
    public static final double k = 1e-7;

    // A method to calculate the magnetic force between two objects
    public static double magneticForce(double m1, double m2, double d) {
        return k * m1 * m2 / (d * d);
    }

    // A method to find the point of balance between two magnetic materials
    public static double pointOfBalance(double x1, double x2, double m1, double m2) {
        // Use binary search to find the point where the forces are equal
        double low = x1;
        double high = x2;
        double mid = (low + high) / 2;
        double error = 1e-9; // The maximum error allowed
        while (Math.abs(low - high) > error) {
            // Calculate the forces at the mid point
            double f1 = magneticForce(m1, m2, mid - x1);
            double f2 = magneticForce(m2, m1, x2 - mid);
            // If the forces are equal, return the mid point
            if (Math.abs(f1 - f2) < error) {
                return mid;
            }
            // If the force from the left is greater, move the low point to the right
            if (f1 > f2) {
                low = mid;
            }
            // If the force from the right is greater, move the high point to the left
            if (f2 > f1) {
                high = mid;
            }
            // Update the mid point
            mid = (low + high) / 2;
        }
        // Return the approximate point of balance
        return mid;
    }

    public static void main(String[] args) {
        // Create a scanner object to read input
        Scanner sc = new Scanner(System.in);
        // Read the number of test cases
        int t = sc.nextInt();
        // Loop through each test case
        for (int i = 1; i <= t; i++) {
            // Read the number of magnetic materials
            int n = sc.nextInt();
            // Create arrays to store the x coordinates and magnetic moments of the magnetic materials
            double[] x = new double[n];
            double[] m = new double[n];
            // Read the x coordinates and magnetic moments of the magnetic materials
            for (int j = 0; j < n; j++) {
                x[j] = sc.nextDouble();
                m[j] = sc.nextDouble();
            }
            // Print the case number
            System.out.print("#" + i + " ");
            // Loop through each pair of adjacent magnetic materials and find the point of balance
            for (int j = 0; j < n - 1; j++) {
                double p = pointOfBalance(x[j], x[j + 1], m[j], m[j + 1]);
                // Print the point of balance with 10 digits after the decimal point
                System.out.printf("%.10f ", p);
            }
            // Print a new line after each case
            System.out.println();
        }
        // Close the scanner object
        sc.close();
    }
}