我编了一个程序,感觉写得挺笨拙的,也花了不少时间。本身我自己是新手,也正好拿这个题做个练习,大家一定会有更好的方法,希望大家拿出来分享。下面是我写的程序,输入:
aB,AC,Ad,bE,Bf,Cg,CH,DI,EJ,EK,eL,fM,Fn,Go,gP,Hq,HR,HS,IT,IU,IV,iW,jx,jy,
输出:
终点的个数为:15
分别为:k l m n o p q r s t u v w x y
#include<string>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int del(vector<char> &origin,int &con);
int main()
{
string station;
cout<<"输入一些路段:"<<endl;
cin>>station;
int count=0;
vector<char> result;
for(string::size_type last=1; last<=station.size();)
{
string::size_type first=0;
while(first<=station.size())
{
station[first]=toupper(station[first]);
station[last]=toupper(station[last]);
if(station[first]==station[last])
break;
if(first>station.size()-3)
{
station[last]=tolower(station[last]);
result.push_back(station[last]);
++count;
}
first=first+3;
}
last=last+3;
}
del(result,count);
cout<<"终点的个数为:"<<count<<endl;
cout<<"分别为:";
for(vector<char>::size_type j=0;j!=result.size();++j)
cout<<result[j]<<" ";
cout<<endl;
return 0;
}
int del(vector<char> &origin,int &con)
{
for(vector<char>::iterator size=origin.begin();size!=origin.end();++size)
{
vector<char>::iterator ind=origin.begin();
while(find(size+1,origin.end(),*size)!=origin.end())
{
ind=find(size+1,origin.end(),*size);
origin.erase(ind);
--con;
}
}
return 0;
}