Conditional substitution of numbers into a two-d

Asked 2 years ago, Updated 2 years ago, 103 views

I want to save the given numbers in two dimensions.For example, if a sequence {3,12,4,1,5,8} is given, the first digit 3 is inserted into array 1, the second digit 12 is larger than 3, so it is inserted into another array 2, 4 is smaller than 12, and 1 is scanned from left, so it is inserted into array 1.
Structurally, the two-dimensional list

 ints={3,12,4,1,5,8}
intarray [N][N]; // N is the number of characters entered.
array[0][0]=s[1];
int j = 1;
for(inti=1;i<N;i++){
    if(s[i]<array[i-1][i-1]{
        array[i][i] = array[i-1][i-1];
        array[i-1][i-1]=s[i];
    }
    else{
        if array[i][i+j]>s[i];
        array[i][i+j]=s[i];
}

I was thinking of a structure like this, but I can't deal with it when the number is larger than the second array, and if I use the for loop again, the calculation will be huge.Also, since it is an array, it takes a lot of time to insert it on top of existing elements...
Shouldn't we use a two-dimensional array?I would appreciate it if you could help me.

Additional
I have corrected the questionnaire.
Expected output is

head-1-3
head-4-12
head-5
head-8

It's like
I'd like to look like this image, but I'm sorry that I couldn't explain it well...
Expected Behavior

c algorithm data-structure

2022-09-30 19:11

1 Answers

An example of a code using a two-dimensional array.
 You did not insert any elements in the middle of the array.

I thought I could understand what the code was for, but I still don't know.
 I'm not sure if the behavior is as intended by the person who asked me.
 As you said performance was important, we did not replace the elements in the array during the process, but rearranged them in ascending order at the end.

  • Input datas
    {3,12,4,1,5,11,2,10,9,8,4}
    says .
  • Show progress for debugging purposes
#include<stdio.h>
#define NUM(A) (sizeof(A)/sizeof(A[0]))
#define N NUM(s)//N is the number of input values
ints[] = {3,12,4,1,5,11,2,10,9,8,4}; // Input data
intarray[N][N]={0};
inta_num[N] = {0};
void print_result(int num);
void debug_print_input(ints[], int num);

int main(intargc, char*argv[]){
  intis;// indexofs
  int tmp;
  introw_num;
  debug_print_input(s,N);
  for (is=0; is<N;is++) {// Input data
    printf("%s\n", "-------------------------------------");
    debug_print_input(s, is +1);
    for (inti=0;i<N;i++) {// line
      if(array[i][0]==0){// No leading column configured
        /* Unconditionally set input data to the first column */
        array[i][0]=s[is];
        a_num[i]+=1;
        row_num=i+1;
        break;
      } else if(s[is]<array[i][0]) {// Input data is less than the first column
        /* Set input data to first column and old first column to last */
        tmp = array[i][0];
        array[i][0]=s[is];
        array[i][a_num[i]]=tmp;
        a_num[i]+=1;
        break;
      }
    }
    print_result(row_num);
  }
  // Invert the second and subsequent columns of each row
  for(inti=0;i<row_num;i++){
    for(int j=1;j<(a_num[i]+1)/2;j++){
      tmp = array[i][j];
      array[i][j] = array[i][a_num[i]-1];
      array[i][a_num[i]-1] = tmp;
    }
  }
  printf("%s\n", "=========================================");
  printf("Final Results\n");
  print_result(is);
  return 0;
}
void print_result(int num){
  for(inti=0;i<num;i++){
    if(array[i][0]==0)
      break;
    printf("%2d:",i);
    for(int j=0;j<num;j++){
      if(array[i][j]==0)
        break;
      printf("(%2d), array[i][j]);
    }
    printf("\n");
  }
}
void debug_print_input(ints[], int num){
  printf("s=");
  char*dlmt="{";
  for(intis=0;is<num;is++){
    printf("%s%d", dlmt, s[is]);
    dlmt=", ";
  }
  printf("}\n");
}
s={3,12,4,1,5,11,2,10,9,8,4}
-----------------------------------------
s = {3}
 0:( 3)
-----------------------------------------
s = {3,12}
 0:( 3)
 1:(12)
-----------------------------------------
s = {3,12,4}
 0:( 3)
 1:( 4) (12)
-----------------------------------------
s = {3,12,4,1}
 0:( 1) ( 3)
 1:( 4) (12)
-----------------------------------------
s = {3,12,4,1,5}
 0:( 1) ( 3)
 1:( 4) (12)
 2:( 5)
-----------------------------------------
s = {3,12,4,1,5,11}
 0:( 1) ( 3)
 1:( 4) (12)
 2:( 5)
 3:(11)
-----------------------------------------
s = {3,12,4,1,5,11,2}
 0:( 1) ( 3)
 1:( 2) (12) ( 4)
 2:( 5)
 3:(11)
-----------------------------------------
s = {3,12,4,1,5,11,2,10}
 0:( 1) ( 3)
 1:( 2) (12) ( 4)
 2:( 5)
 3:(10) (11)
-----------------------------------------
s = {3,12,4,1,5,11,2,10,9}
 0:( 1) ( 3)
 1:( 2) (12) ( 4)
 2:( 5)
 3:( 9) (11) (10)
-----------------------------------------
s = {3,12,4,1,5,11,2,10,9,8}
 0:( 1) ( 3)
 1:( 2) (12) ( 4)
 2:( 5)
 3:( 8) (11) (10) ( 9)
-----------------------------------------
s = {3,12,4,1,5,11,2,10,9,8,4}
 0:( 1) ( 3)
 1:( 2) (12) ( 4)
 2:( 4) ( 5)
 3:( 8) (11) (10) ( 9)
=========================================
final result
 0:( 1) ( 3)
 1:( 2) ( 4) (12)
 2:( 4) ( 5)
 3:( 8) ( 9) (10) (11)


2022-09-30 19:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.