搜狗的“圆周上两点间的距离”,一直是50%,还没有错误提示。

一直50%,用的是二个for循环嵌套解得。求大神解答。


import java.io.BufferedReader;
import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));  try {
            String input = reader.readLine();  int n = Integer.parseInt(input);  double[] na = new double[n];  for (int i = 0; i < n; i++) {
                na[i] = Double.parseDouble(reader.readLine());  } double max=0;  for(int i=0;i<n;i++)
            { for(int k=i+1;k<n;k++){ double temp=na[k]-na[i];  if(temp>180.0){
                        temp=360.0-temp;  } if(temp>max){
                        max=temp;  }
                }
            }
            System.out.println(String.format("%.8f",max));  }catch (IOException e) {
       }
    }

}





#搜狗#
全部评论
//计算差值进行存储,然后累加差值来求最大距离,考试时没想出来。。。 //后来想的这个方案,不知道效率能行不................... #include<iostream> #include<vector> #include <iomanip> #include <algorithm> #include <deque> using namespace std; int main(){ int n; while(cin>>n){ vector<float> vf(n,0.0); for (int i=0;i<n;i++) { cin>>vf[i]; } //计算差值 deque<float> df(n,0.0); for (int i=0;i<n-1;i++) { df[i]=vf[i+1]-vf[i]; } float max=0.0; float sum=0.0; long long count=0; for (int i=0;i<n-1;i++) { sum+=df[i]; if (sum<=180) { max=sum; } else { max=(360-(vf[i+1]-vf[count])) > max ? (360-(vf[i+1]-vf[count])) : max; //这里求出了第一个点可能的最大值 sum-=df[count]; count++; //递进一个点 } } cout<<fixed<<setprecision(8)<<max<<endl; df.clear(); vf.clear(); } return 0; }
点赞 回复 分享
发布于 2017-09-08 21:23
90% 调不出来 public static void main(String[] args){     Scanner sc=new Scanner(System.in);     int number=sc.nextInt();     double[] array=new double[number];     for(int i=0;i<number;i++){         array[i]=sc.nextDouble();     }     double max=180d;     double result=0d;     double temp;     int start=0;     int end=1;     while(end<=number-1){         temp = array[end] - array[start];         if(temp<=max){             if(temp>result) {                 result = temp;             }             end++;         }else{             temp = 360d - array[end] + array[start];             if (temp > result) {                 result = temp;             }             start++;         }     }  System.out.println(String.format("%.8f", result)); }
点赞 回复 分享
发布于 2017-09-08 18:53
#include<iostream> #include<vector> #include <iomanip> #include<algorithm> using namespace std; double process(vector<double> nodes){ if ( nodes.size() < 2){ return 0.0; } int indexi = 0; int indexj = 1; double res = 0.0; while (indexj < nodes.size()){ if (nodes[indexj] - nodes[indexi] <= 180.0){ res = max(res, nodes[indexj] - nodes[indexi]); indexj++; } else{ res = max(res, 360.0 - (nodes[indexj] - nodes[indexi])); indexi++; } } return res; } int main() { vector<double> nodes; int n; cin >> n; double tmp; for (int i = 0; i < n; i++){ cin >> tmp; nodes.push_back(tmp); } cout << fixed << setprecision(8) << process(nodes); return 0; }
点赞 回复 分享
发布于 2017-09-08 18:48
最后修改了只有80%
点赞 回复 分享
发布于 2017-09-08 18:39
package test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Main14 {     /*      * 普通方法      */     public static double getMaxDis(double[] data)     {         double maxDis = Double.MIN_VALUE;         double num = 360.0;         for(int i=0;i<data.length-1;i++)         {             for(int j=i+1;j<data.length;j++)             {                 double nowDis = Math.abs(data[i]-data[j]);                 double nowDis2 = Math.min(nowDis, Math.abs(num-nowDis));                 maxDis = Math.max(maxDis, nowDis2);             }         }         return maxDis;     }     /*      * 二分方法      */     public static double getMaxDis2(double[] data)     {         double maxDis = -360.00000000;         double num = 360.00000000;         for(int i=0;i<data.length;i++)         {             double rotate = (data[i] + 180) % 360;             int start = 0;             int end = data.length-1;             int mid = 0;             while(start <= end)             {                 mid = (start+end)/2;                 if(data[mid] > rotate)                 {                     end = mid - 1;                 }else if(data[mid] < rotate)                 {                     start = mid + 1;                 }else{                     break;                 }             }             double nowDis = Math.abs(data[i]-data[mid]);             double nowDis2 = Math.min(nowDis, Math.abs(num-nowDis));             maxDis = Math.max(maxDis, nowDis2);         }         return maxDis;     }     public static void main(String[] args) {         BufferedReader localReader = new BufferedReader(                 new InputStreamReader(System.in));         String str = null;         try {             str = localReader.readLine();         } catch (IOException e1) {             e1.printStackTrace();         }         int n = Integer.parseInt(str);                          double[] data = new double[n];         for(int i=0;i<n;i++)         {             try {                 str = localReader.readLine();             } catch (IOException e) {                 e.printStackTrace();             }             data[i] = Double.parseDouble(str);         }         double maxDis = getMaxDis2(data);         System.out.println(String.format("%.8f", maxDis));              } } 最后的二分方法没有在线测试,普通方法AC50%
点赞 回复 分享
发布于 2017-09-08 18:34
双层循环应该是过不了的,测试的数据可能比较大。
点赞 回复 分享
发布于 2017-09-08 17:51
双层循环肯定不过啊。不用想。
点赞 回复 分享
发布于 2017-09-08 17:50
题目都没说输入是类型的,数组,文件还是啥的。。。我假设是数组了- -原来还可以这样读输入。
点赞 回复 分享
发布于 2017-09-08 17:49
转为Long Long 然后二分就可以了!
点赞 回复 分享
发布于 2017-09-08 17:48
我也是一直是50%
点赞 回复 分享
发布于 2017-09-08 17:46
我也是卡在50%了,直接交了。随缘~~~~~~~~~~~~~~~·
点赞 回复 分享
发布于 2017-09-08 17:45

相关推荐

昨天 19:03
门头沟学院 运营
点赞 评论 收藏
分享
自由水:笑死了,敢这么面试不敢让别人说
点赞 评论 收藏
分享
面试了几家,全程问项目,八股一点都不问,可惜准备了这么久
独角仙梦境:现在感觉问八股像是中场休息一样的,问几个八股放松一下再上强度
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务