Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
Encuentra secuencias de cualquier rango

Dada una matriz de números, imprima todos y cada uno de los rangos disponibles. Por ejemplo Matriz: 9, 3, 5, 7, 4, 8, 1 Salida: 1, 3-5, 7-9 Nota: Ejecute este problema sin usar una matriz adicional.

¿Cómo procedo? *

 #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; } } } }

* Primero ordenaré en orden ascendente, ¿no sé qué hacer a continuación? PD: Estoy codificando esto en C.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

El siguiente paso es identificar secuencias. Pruebe el siguiente bucle (no completamente depurado):

 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++; }
over 4 years ago · Santiago Trujillo Report

0

Escribí una función simple y legible para ti, échale un vistazo:

 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; } } }

Corre en vivo.

Tenga en cuenta que, para una buena comprensión, la variable current se declara mientras que current y printEnd son iguales. Puede reemplazar current por printEnd .

over 4 years ago · Santiago Trujillo Report

0

Si puede cambiar la matriz original, es decir, si puede ordenarla, entonces el programa puede verse así

 #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 ); }

La salida del programa es

 { 5, 8 } { 8, 9 } { 14 }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!