a program that reads the minimum values of each column from the text and adds those numbers together

Asked 1 years ago, Updated 1 years ago, 290 views

I am sorry to ask you questions.
What I want to ask this time is a program that reads the minimum value (except 0) of each line of text from the specified line, then goes to the same row number as the column with the minimum value, and adds the minimum value.
More than just that, columns with the same number as the row selected as the minimum value are initialized and never thought twice.

I don't think it's easy to understand just by writing, so I'll give you a specific example and explain it below.(The text I used this time is Euro.txt.)

<uro.txt>

6
0.0 340.0 1270.0 1450.0 2400.0 780.0
340.0 0.0 1060.0 1120.0 2100.0 490.0
1270.0 1060.0 0.0 1370.0 2370.0 1250.0
1450.0 1120.0 1370.0 0.0 1040.0 700.0
2400.0 2100.0 2370.0 1040.0 0.0 1620.0
780.0 490.0 1250.0 700.0 1620.0 0.0

For example, suppose you specify "line 1" for the text shown above.
①In that case, the minimum value of the first row is "340 of the second row of one row".⇒ Head to the second line
②The minimum value for the second row is 340 but the first row was chosen earlier, so considering the minimum value except for the first row,
 "It becomes 490 in the second row and sixth row" 六 heading to the sixth row
③The minimum value of row 6 is 490, but row 1 and row 2 were selected earlier, so considering the minimum value except row 1 and row 2,
 "It becomes 700 in the fourth row of the sixth row" 四 heading to the fourth row
④The minimum value of row 4 is 700, but row 6 is selected as で, so considering the minimum value except row 1, 2, and 6,
 "It becomes ""1040 in row 4 and row 5"" 五 heading to row 5
" ⑤The minimum value of line 5 is 1040, but line 4 is selected as で, so considering the minimum value except column 1, 2, 4, and 6,
 "There is only one left, and it becomes ""2370 in the fifth row and the third row"" 三 heading to the third row."
⑥Since all rows are selected in the third row, "1270 of the first row of the third row" is selected to return to the original first row.一 Head to the first line
(In the last operation, select the same column as the first row)

From the above, when the column numbers visited are shown in order, they become 1, 2, 6, 4, 5, 6, 5, 3, 1.
Also, the sum of these is 340+490+700+1040+2370+1270=6210, so the expected execution result is

Data file name: Euro.txt
Data File Name: Euro.txt     
0.000   340.000  1270.000  1450.000  2400.000   780.000   
340.000     0.000  1060.000  1120.000  2100.000   490.000  
1270.000  1060.000     0.000  1370.000  2370.000  1250.000  
1450.000  1120.000  1370.000     0.000  1040.000   700.000  
2400.000  2100.000  2370.000  1040.000     0.000  1620.000   
780.000   490.000  1250.000   700.000  1620.000     0.000
Please enter your departure city number (1--6).  1
The circuit is 1 2 6 4 5 3 1
The total travel distance is 6210.000.

I would like to do this, but I have to create a program in such a complicated way.
I've been asking my friends for a whole week, and I haven't seen any signs of a goal.

Therefore, we can only program numbers from the file, but
For now, here's the program:

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

constint FNLEN=50; /* filename length */
constint MAX_CITY = 100; /* Maximum number of cities */

int main()
{
  FILE*fp;
  inti,j,ncity,dept;
  int visit [MAX_CITY]; /* 0: Not visited, 1: Visited (whether visited) */
  int visit_list [MAX_CITY]; /* Visit order */
  double dist [MAX_CITY] [MAX_CITY], total_dist, min_dist;
  char file_name [FNLEN]; /* data filename */
    
  printf("Data file name:");
  scanf("%s", file_name);
    
  if((fp=fopen(file_name, "r"))==NULL){/*Exit if file opening fails*/
    printf("%s: Unable to open file!\n", file_name);
    return-1;
  }

  /* Load data from a file*/
  printf("data filename:%s\n", file_name);
  fscanf(fp, "%d", & ncity); /* Load City Count */
  
  if(ncity>MAX_CITY){
    printf("Number of cities must be %3d or less!\n",MAX_CITY);
    return-1;
  }
  
  for(i=0;i<ncity;i++){/*Read distance matrix*/
    for(j=0;j<ncity;j++){
      fscanf(fp, "%lf", & dist[i][j]);
      printf("%10.3lf", dist[i][j]);
    }
    printf("\n");
  }
        
  fclose(fp); /* file close*/

  /* Keyboard the city number of the city you are leaving*/
  printf("Please enter the city number (1--%3d) of the origin.",ncity);
  scanf("%d", & dept);
  if(dept>ncity||dept<0){
    printf("The city number is invalid.\n");
    return(-1);
  }
  
  /* Array visit and visit_list initialization*/
  for(i=0;i<ncity;i++){
    visit[i] = 0;
    visit_list[i] = -1;
  }
    
  /* ----------------------------- Greedy arithmetic start
  intmin;
  min = visit [0];
  for (i=dept; i<ncity;i++)
  for (j=0; j<ncity;j++)
     if(min<visit[j])
       min = visit [j];
         
  /* --------------------------*/
 
}

I am well aware that this is a rather round question, but if you can,
Could you show me the program?

c

2022-09-30 21:50

2 Answers

We do not provide the source code itself.
If you want to implement the steps listed in ~ to の, please refer to the following and code them.

  • The array in the order of visits has the last minute to return to the departure city, so the maximum number of +1 minutes is reserved

  • Initialize variables that require initialization that can be initialized at the time of definition (by the way, the visit order array does not need to be initialized with -1)

  • Re-create the greedy arithmetic part as follows

    • The number of cities displayed in the UI (1 origin) and the number of cities handled in the internal array (0 origin) are different, so write them down consciously
    • Before the for loop processing initializes information about the cities in which you have visited and the first cities in the order in which you want to process the for loop
    • Outside for loop to set the visit order sequence from beginning to end
    • Min is defined as double instead of int and first in the outer for loop.Then initialize with the maximum value of double type.
    • Define variables to remember to use the minimum index value determined by the inner for loop after the inner loop ends
      • The inner for loop contains information on the distance from one city to another in each column, so you can do it to get the shortest unvisited city index value and distance information for your next visit
      • If you have visited (visited == 1) or yourself (distance == 0.0), do not take action and move to the next round of the loop
      • If the value indicated by the current loop position is smaller than the minimum value stored, the value is set to the minimum value and the index value is also stored
    • Inside forSet the minimum index value to the visited array as the next visit city, add it to the visit order array, and add the minimum value to the total travel distance
    • Add the first city to the visit order array after the outer for loop and add the distance from the last city to the first city to the total distance traveled
  • <li>><p> Patrol route display displays an array of visits from 0 to a value of the number of cities (which is displayed until returning to the departure city).Note the 0/1 origin type of stored data when displaying

The order of visits has the last minute to return to the departure city, so the maximum number of +1 minutes is reserved.

Initialize variables that need to be initialized at the time of definition (by the way, you don't need to initialize with -1)

Re-create the greedy arithmetic as follows

  • The number of cities displayed in the UI (1 origin) and the number of cities handled in the internal array (0 origin) are different, so write them down consciously
  • Before the for loop processing initializes information about the cities in which you have visited and the first cities in the order in which you want to process the for loop
  • Outside for loop to set the visit order sequence from beginning to end
  • Min is defined as double instead of int and first in the outer for loop.Then initialize with the maximum value of double type.
  • Define variables to remember to use the minimum index value determined by the inner for loop after the inner loop ends
    • The inner for loop contains information on the distance from one city to another in each column, so you can do it to get the shortest unvisited city index value and distance information for your next visit
    • If you have visited (visited == 1) or yourself (distance == 0.0), do not take action and move to the next round of the loop
    • If the value indicated by the current loop position is smaller than the minimum value stored, the value is set to the minimum value and the index value is also stored
  • Inside forSet the minimum index value to the visited array as the next visit city, add it to the visit order array, and add the minimum value to the total travel distance
  • Add the first city to the visit order array after the outer for loop and add the distance from the last city to the first city to the total distance traveled
  • The inner for loop contains information on the distance from one city to another in each column, so you can do it to get the shortest unvisited city index value and distance information for your next visit
  • If you have visited (visited == 1) or yourself (distance == 0.0), do not take action and move to the next round of the loop
  • If the value indicated by the current loop position is smaller than the minimum value stored, the value is set to the minimum value and the index value is also stored

In the circuit display, a visiting order array is displayed from 0 to a value of the number of cities ( thereby displayed until returning to the departure city).Note the 0/1 origin type of stored data when displaying


2022-09-30 21:50

#!/usr/bin/env bash
set-oerrexit
set-onounset

R = "${1}"

>route.txt
for in$(seq1$(head-n1data.txt);do
  echo$R
  sed1d data.txt\
    | sed-n${R}p\
    | xargs-n 1 echo\
    | nl-nln\
    | grep-v'\t0\.0$'\
    | sort-k2,2n\
    | sed-n's /\s.*//p'\
      >tmp.txt

  while read C;do
    grep$Croute.txt>/dev/null&&continue

    echo$R>>route.txt
    R = $C
    break
  done<tmp.txt
done
echo "${1}"


2022-09-30 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.