查找任何范围的序列

时间:2015-07-30 08:24:10

标签: c arrays sorting range sequence

给定一组数字,打印每个可用范围。 例如 数组:9,3,5,7,4,8,1 输出:1,3-5,7-9 注意:请在不使用其他阵列的情况下执行此问题。

我该怎么办? *

#include<stdio.h>
int main()
{
int a[]={9,8,8,7,6,5,14};
int n= sizeof(a) / sizeof(a[0]);
int i,j;
int temp;
for(i=0;i<n;i++)
         {
               for(j=i+1;j<n;j++)
               {
                     if(a[i]>a[j])
                     {
                           temp=a[i];
                           a[i]=a[j];
                           a[j]=temp;
                     }
               }
        }
}

* 我将按升序排序,我不知道接下来该做什么? P.S:我在C编码。

4 个答案:

答案 0 :(得分:0)

下一步是识别序列。尝试以下循环(未完全调试):

first= next= a[0];
for (i=1; i<n; i++) {
    if (a[i] > next+1) {
        if (next>first)
             printf("%d-%d,", first, next);
        else printf("%d,", first);
        first= next= a[i];
    }
    else next++;
}

答案 1 :(得分:0)

我为你写了一个简单易读的功能,看看:

# Procfile.dev
web: bundle exec unicorn -c ./config/unicorn.development.rb

# Procfile.testing
web: bundle exec unicorn -c ./config/unicorn.testing.rb

Run live.

注意,为了更好地理解变量void printRange(int sortedArray[], int len) { int i, current, next, printStart, printEnd, startIndex = 0; bool print = false; for (i = 0; i < len; i++) { printStart = sortedArray[startIndex]; printEnd = sortedArray[i]; current = sortedArray[i]; if(i < len -1) { next = sortedArray[i + 1]; } else next = current; if (next - current != 1) { startIndex = i + 1; print = true; } if (print) { if (printStart - printEnd == 0) { printf("%d,", printStart); } else { printf("%d-%d,", printStart, printEnd); } print = false; } } } 已声明,而currentcurrent相同。您可以将printEnd替换为current

答案 2 :(得分:0)

如果您可以更改原始数组,如果您可以对其进行排序,那么程序可能看起来像

#include <stdlib.h>
#include <stdio.h>

int cmp( const void *lhs, const void *rhs )
{
    int a = *( const int * )lhs;
    int b = *( const int * )rhs;

    return ( b < a ) - ( a < b );
}

int main()
{
    int a[] = { 9, 8, 8, 7, 6, 5, 14 };
    const size_t N = sizeof( a ) / sizeof( *a );

    qsort( a, N, sizeof( int ), cmp );
/*    
    for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
    printf( "\n" );
*/    
    int *p = a;
    int *start = a, *end = a;
    do
    {
        if ( ++p == a + N || *p  != *end + 1 )
        {
            printf( "{ %d", *start );
            start == end ? printf( " }\n" ) : printf( ", %d }\n", *end );
            start = end = p;
        }
        else
        {
            end = p;
        }
    } while ( p != a + N );            
}    

程序输出

{ 5, 8 }
{ 8, 9 }
{ 14 }

答案 3 :(得分:0)

这里已经为这个任务提供了一些很好的答案,但也许开头的排序部分值得多谈一点。特别是如果您在学校、大学或工作面试中需要类似的东西。

最简单的排序技术/算法类似于 BubbleSort,它可以通过 2 个 for 循环轻松实现。

<块引用>
void BubbleSort (int a[], int length)
{
  int i, j, temp;
  
   for (i = 0; i < length; i++)
   {
       for (j = 0; j < length - i - 1; j++)
       {
           if (a[j + 1] < a[j])
           {
               temp = a[j];
               a[j] = a[j + 1];
               a[j + 1] = temp;
           }
       }
   }
}

Source and more information

使用整数(或任何类型的数字)对此类数组进行排序的最佳方法是 QuickSort。该算法非常先进,但如果您在 Youtube 上观看过精彩的视频或阅读此article,您肯定知道它是如何工作的。

<块引用>
void quick(int array[], int start, int end){
   if(start < end){
       int l=start+1, r=end, p = array[start];
       while(l<r){
           if(array[l] <= p)
               l++;
           else if(array[r] >= p)
               r--;
           else
               swap(array[l],array[r]);
       }
       if(array[l] < p){
           swap(array[l],array[start]);
           l--;
       }
       else{
           l--;
           swap(array[l],array[start]);
       }
       quick(array, start, l);
       quick(array, r, end);
   }
}

Source and more information

注意: QuickSort 使用一种称为递归的技术。如果您不熟悉哪个,可以在这里查看:

<块引用>

在计算机科学中,递归是一种解决问题的方法,其中的解决方案取决于对同一问题的较小实例的解决方案。此类问题一般可以通过迭代解决,但这需要在编程时识别和索引较小的实例。 递归通过使用在自己的代码中调用自身的函数来解决此类递归问题。该方法可以应用于许多类型的问题,递归是计算机科学的中心思想之一。

Source and more information