蓝桥杯 算法训练 蜜蜂飞舞


算法训练 蜜蜂飞舞 Description

  “两只小蜜蜂呀,飞在花丛中呀……”

  话说这天天上飞舞着两只蜜蜂,它们在跳一种奇怪的舞蹈。用一个空间直角坐标系来描述这个世界,那么这两只蜜蜂初始坐标分别为(x1,y1,z1),(x2,y2,z2)  。在接下来它们将进行n次飞行,第i次飞行两只蜜蜂分别按照各自的速度向量飞行ti个单位时间。对于这一现象,玮玮已经观察了很久。他很想知道在蜜蜂飞舞结束时,两只蜜蜂的距离是多少。现在他就求教于你,请你写一个程序来帮他计算这个结果。

Input 输入描述:

  第一行有且仅有一个整数n,表示两只蜜蜂将进行n次飞行。

  接下来有n行。

  第i行有7个用空格分隔开的整数ai,bi,ci,di,ei,fi,ti  ,表示第一只蜜蜂单位时间的速度向量为(ai,bi,ci) ,第二只蜜蜂单位时间的速度向量为(di,ei,fi) ,它们飞行的时间为ti 。

  最后一行有6个用空格分隔开的整数x1,y1,z1,x2,y2,z2,如题所示表示两只蜜蜂的初始坐标。

输入样例:

Output 输出描述:

  输出仅包含一行,表示最后两只蜜蜂之间的距离。保留4位小数位。

输出样例:

Sample Input 1

参考上文 Sample Output 1

参考上文 Hint

HINT:时间限制:1.0s 内存限制:512.0MB

Source

蓝桥杯练习系统 ID: 109 原题链接: http://lx.lanqiao.cn/problem.page?gpid=T109

package beeflying;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int x1,y1,z1,x2,y2,z2;
        //两只蜜蜂在各个方向飞行的总位移
        int a = 0,b = 0,c = 0,d = 0,e = 0,f=0;
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        for(int i = 0;i<n;i++) {
            String[] temp = new String[7];
            temp = scanner.nextLine().split(" ");
            a += Integer.valueOf(temp[0])*Integer.parseInt(temp[6]);
            b += Integer.valueOf(temp[1])*Integer.parseInt(temp[6]);
            c += Integer.valueOf(temp[2])*Integer.parseInt(temp[6]);
            d += Integer.valueOf(temp[3])*Integer.parseInt(temp[6]);
            e += Integer.valueOf(temp[4])*Integer.parseInt(temp[6]);
            f += Integer.valueOf(temp[5])*Integer.parseInt(temp[6]);
        }
        String[] temp = new String[6];
        temp = scanner.nextLine().split(" ");
        x1 = a + Integer.parseInt(temp[0]);
        y1 = b + Integer.parseInt(temp[1]);
        z1 = c + Integer.parseInt(temp[2]);
        x2 = d + Integer.parseInt(temp[3]);
        y2 = e + Integer.parseInt(temp[4]);
        z2 = f + Integer.parseInt(temp[5]);

        double result2 = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2);
        System.out.printf("%.4f",Math.sqrt(result2));
        scanner.close();
    }

}

Author: qwq小小舒
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source qwq小小舒 !
  TOC