### Station problem c++

I need help with the next problem. Correct the next issue so that it runs. Or any other solution :)
#include
using namespace std;

#define N 101
struct statii { int d, p; } a[N];
int n, x;

void citire() {
ifstream f("date.in");
f >> n >> x;
for(int i=1; i> a[i].d; a[i].p = i; }
f.close();
}
void gridi() {
ofstream g("date.out");
int distanta = 0;
int b[N], k=0;
b[++k] = 1;
for(int i=2; i= x) {
b[++k] = i;
distanta = 0;
}
else distanta += a[i-1].d;
}
g << k << "\n";
for(int i=1; i<=k; i++) g << b[i] << " ";
}

int main() {
citire();
gridi();
}
Problem Requirement:
The owner of a private public transport company received from the city hall the approval to be able to use some of the stations of the Local Public Transport Authority. The available stations are located along the main thoroughfare of the city.
He decides to introduce a fast race that runs through the city, from one end to the other
the other on the main artery. For starters, it deals with stations located on the same side of the road. The owner has a dilemma: if the stops are too desirable, then the passage of the city will take too long and will bore travelers, and if the stations are too rare, the trips will be too few. Therefore, the criteria according to which the owner determines the stations where he will stop the fast race are:
• Between two adjacent stations should be at least x meters.
• The total number of stations should be maximum.
Help the owner choose the stations!
Input data
We will consider the stations located on the same side of the main artery numbered in order, from one end to the other with 1, 2,…, n.
The first line of the STATII.IN input file contains a natural number n, representing the number of stations located on the main artery.
The following line contains n - 1 integers ai, i = 1, 2,…, n - 1 with the meaning: ai the distance between station i and station
i + 1. These numbers will be separated by a space.
Output data
The STATII.OUT output file will contain two lines. A number will be written on the first line
integer k which name the maximum number of stations chosen by the owner, and on the second line will be k numbers, representing the serial numbers of these stations. The numbers
will be written in ascending order.
Restrictions and clarifications
• 1 ≤ n ≤ 1000;
• 1 ≤ ai ≤ 2000, i = 1, 2, ..., n - 1;
• 1 ≤ k ≤ 1000;
• If there are several solutions, only one will be written to the file.
Where is x in your input?
Hello VDNS,

 PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code. Along with the proper indenting it makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/ Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you. You can use the preview button at the bottom to see how it looks. I found the second link to be the most help.

First it would help to restructure your code:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include using namespace std; #define N 101 struct statii { int d, p; } a[N]; int n, x; void citire() { ifstream f("date.in"); f >> n >> x; for (int i = 1; i > a[i].d; a[i].p = i; } f.close(); } void gridi() { ofstream g("date.out"); int distanta = 0; int b[N], k = 0; b[++k] = 1; for (int i = 2; i = x) { b[++k] = i; distanta = 0; } else distanta += a[i - 1].d; } g << k << "\n"; for (int i = 1; i <= k; i++) g << b[i] << " "; } int main() { citire(); gridi(); }``````

Formatting the code makes some of the errors stand out.

Second you should compile the code and see what errors the compiler returns. Then try to fix them.

Until you see if the program works the rest is pointless unless you plan on rewriting the program from scratch.

Single letter variable names work better in describing the program, but a good variable name in the code makes it easier to follow. The instructions do not say what variable names to use, so you have no restrictions.

Blank lines also make it easier to read the code. You need to learn to use them. If you want someone to read your code, and you do, make it easy to read and understand.

I know that you think using `using namespace std;` makes it easier, but you should learn to not use this line.

Also try not to use global variables. You are setting your-self up for trouble even though you think it makes it easier.

Andy
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include #include #include using namespace std; void recurse( int nstat, int i, int sincelast, int x, int n, const vector &a, vector &s, int &nbest, vector &sbest ) { // Check last station if ( i == n ) { if ( sincelast >= x ) // enough distance to put a station here { if ( nstat > nbest ) { nbest = nstat; s[nstat] = i; sbest = s; } } return; } // Check case of NOT putting station at i recurse( nstat, i + 1, sincelast + a[i], x, n, a, s, nbest, sbest ); // Check case of putting station at i (if possible) if ( sincelast >= x ) { s[nstat] = i; recurse( nstat + 1, i + 1, a[i], x, n, a, s, nbest, sbest ); } } int main() { ifstream in( "STATII.IN" ); ofstream out( "STATII.OUT" ); int n, x; in >> n >> x; vector a( n ); for ( int i = 1; i < n; i++ ) in >> a[i]; vector s( 1 + n ); s[1] = 1; vector sbest; int nbest = 0; recurse( 2, 2, a[1], x, n, a, s, nbest, sbest ); out << nbest << '\n'; for ( int i = 1; i <= nbest; i++ ) out << sbest[i] << " "; }``````

Topic archived. No new replies allowed.