【问题标题】:Minimum difference in an array数组中的最小差异
【发布时间】:2012-09-20 02:40:51
【问题描述】:

我想找出数组所有元素之间的最小差异。我通读了其他各种问题,但在以下代码中找不到错误的确切来源。

#include<iostream>
#include<stdio.h>
#include<cstdlib>

using namespace std;

void quicksort(long int *lp, long int *rp);

int main()
{
    int t,n;
    long int s[5000];

    cin>>t;
    while(t--){
    cin>>n;
    for(int i=0;i<n;i++) cin>>s[i];
    quicksort(&s[0],&s[n-1]);
    //cout<<"passes:"<<passes<<endl;
    //for(int i=0;i<n;i++) cout<<s[i]<<" ";
    long int min = abs(s[1]-s[0]);
    //cout<<endl<<min;
    for(int i=1;i<(n-1);i++){
        long int temp = abs(s[i]-s[i+1]);
        if (temp <= min) min = temp;
    }
    cout<<min;
    }
}

void quicksort(long int *lp,long int *rp){
    int arraysize= (rp-lp)+1;
    if(arraysize > 1){
       long int *pivot = (lp+(arraysize/2));
       long int swap=0;
      long int *orgl = lp;
      long int *orgr = rp;
      while(lp!=rp){
        while (*lp < *pivot) lp++;
        while (*rp > *pivot) rp--;
        if (lp == pivot) pivot=rp;
        else if (rp == pivot) pivot=lp;
        swap = *lp;
        *lp = *rp;
        *rp = swap;
        if((*lp == *pivot) || ( *rp == *pivot)) break;
    }
    quicksort(orgl,pivot-1);
    quicksort(pivot+1,orgr);
}

}

此链接中给出了问题说明:http://www.codechef.com/problems/HORSES 你能找出我程序中的错误吗?

【问题讨论】:

  • 错误是什么?它做错了什么?
  • 首先,我建议您使用标准的 C qsort 函数,而不是自己制作。或者更好的是 std::sort 函数,因为您使用的是 C++。

标签: c++ algorithm


【解决方案1】:

您使用的是 C++,因此与其使用不能真正保证 O(n*logn) 的自定义快速排序,不如使用来自&lt;algorithm&gt; 的排序。 这个逻辑看起来不错:

    long int min = abs(s[1]-s[0]);
    //cout<<endl<<min;
    for(int i=1;i<(n-1);i++){
        long int temp = abs(s[i]-s[i+1]);
        if (temp <= min) min = temp;
    }

By the way: 
cout<<min; 
Add cout<<min << endl;

【讨论】:

  • 谢谢,当我使用 中的 sort() 时它起作用了。我的问题在于快速排序实现。
【解决方案2】:

线

if((*lp == *pivot) || ( *rp == *pivot)) break;

错了。考虑

5 4 7 5 2 5
      ^
    pivot

哎呀。

这一行

long int temp = abs(s[i]-s[i+1]);

是不必要的复杂。由于数组(假定)已排序,

long int temp = s[i+1] - s[i];

不调用abs 也一样。

【讨论】:

  • 是的......这就是确切的错误......纠正它并且代码工作......谢谢
【解决方案3】:
sort(s, s + n); // #include <algorithm> O(n*log n)

否则排序/查找最小值算法是正确的。有基于随机化、桶排序的 O(n) 算法。

#include <algorithm> // sort()
#include <iostream>

int main() {
  using namespace std;
  int ntests, n;
  const int maxn = 5000; // http://www.codechef.com/problems/HORSES
  int s[maxn];
  cin >> ntests; // read number of tests
  while (ntests--) {
    cin >> n; // read number of integers
    for (int i = 0; i < n; ++i) cin >> s[i]; // read input array

    sort(s, s + n); // sort O(n*log n)
    // find minimal difference O(n)
    int mindiff = s[1] - s[0]; // minn = 2
    for (int i = 2; i < n; ++i) {
      int diff = s[i] - s[i-1];
      if (diff < mindiff) mindiff = diff;
    }
    cout << mindiff << endl;
  }
}

【讨论】:

    【解决方案4】:
    #include <iostream> using namespace std;
    
    int main() {
        int a[] = {1,5,2,3,6,9};
        int c=0;
        int n = sizeof(a)/sizeof(a[0]);
        for(int i=0;i<n-1;i++){
            for(int j=i+1;j<n;j++){
                cout<<a[i]<<" - "<<a[j]<<" = "<<a[i]-a[j]<<endl;
                if(abs(a[i]-a[j]) == 2)
                    c++;
            }
        }
        cout<<c<<endl;
        return 0; }
    

    【讨论】:

    • 嗨,欢迎来到 StackOverflow!答案不应该是简单的 sn-ps 代码。你能详细说明一下这段代码为什么以及如何回答这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    相关资源
    最近更新 更多