该火车票系统为单线,F 查询该路段最小车票数 S 售出车票
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cctype>
using namespace std;
int main(){
int n,i,j,m,f=0,p,q,min,a[32][32];
char s[50],s1[10],s2[10],b[32][10];
cin>>n;
//int **a=new int*[n];
//char **b=new char*[n];
i=0;
while(i<n)
{
cin>>b[i];
i++;
}
cin>>m;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
a[i][j]=m;
else
a[i][j]=0;
}
}
while(gets(s))
{
if(s[0]=='F')
{
i=0;
j=0;
while(s[i]!='\0'&&(!((s[i]-'0'>=0)&&(s[i]-'9'<=0))))
{
i++;
}
while(s[i]!='\0'&&s[i]!=' ')
{
s1[j++]=s[i++];
}
s1[j]='\0';
j=0;
while(s[i]!='\0')
{
if((s[i]-'0'>=0)&&(s[i]-'9'<=0))
s2[j++]=s[i];
i++;
}
s2[j]='\0';
for(i=0;i<n;i++)
{
if(strcmp(b[i],s1)==0)
{p=i;
}
if(strcmp(b[i],s2)==0)
{q=i;
}
}
if(p!=q&&p<q&&p>=0&&q>=0)
{
min=m;
for(i=p;i<q;i++)
{if(min>a[i][i+1])
min=a[i][i+1];
}
cout<<min<<endl;
}else
cout<<0<<endl;
}else if(s[0]=='S')
{
i=0;
j=0;
while(s[i]!='\0'&&(!((s[i]-'0'>=0)&&(s[i]-'9'<=0))))
{
i++;
}
while(s[i]!='\0'&&s[i]!=' ')
{
s1[j++]=s[i++];
}
s1[j]='\0';
j=0;
while(s[i]!='\0')
{
if((s[i]-'0'>=0)&&(s[i]-'9'<=0))
s2[j++]=s[i];
i++;
}
s2[j]='\0';
for(i=0;i<n;i++)
{
if(strcmp(b[i],s1)==0)
{p=i;
}
if(strcmp(b[i],s2)==0)
{q=i;
}
}
for(i=p;i<q;i++)
{
if(a[i][i+1]<=0)
f=1;
}
if(f!=1&&p<q)
{
for(i=p;i<q;i++)
{
a[i][i+1]--;
}
}else
{
cout<<-1<<endl;
}
}
}
return 0;
}