标签:
注:编译环境为Visual Studio 2012,答案仅供参考。
已知2条地铁线路,其中A为环线,B为东西向线路,线路都是双向的。经过的站点名分别如下,两条线交叉的换乘点用T1、T2表示。编写程序,任意输入两个站点名称,输出乘坐地铁最少需要经过的车站数量(含输入的起点和终点,换乘站点只计算一次)。
地铁线A(环线)经过车站:A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18
地铁线B(直线)经过车站:B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15
#include <iostream>
#include <string>
using namespace std;
inline int min(int a, int b){
return a > b ? b : a;
}
inline int max(int a, int b){
return a < b ? b : a;
}
int minDistance(int station1, bool type1, int station2, bool type2){
if (!type1 && !type2)
{
int minStation = min(station1, station2);
int maxStation = max(station1, station2);
return min(maxStation - minStation, minStation + 20 - maxStation);
}
else if (type1 && type2)
{
int minStation = min(station1, station2);
int maxStation = max(station1, station2);
return maxStation - minStation;
}
else if (!type1 && type2)
{
int d1 = minDistance(station1, type1, 10, type1) + minDistance(station2, type2, 6, type2);
int d2 = minDistance(station1, type1, 15, type1) + minDistance(station2, type2, 12, type2);
return min(d1, d2);
}
else
{
int d1 = minDistance(station1, type1, 6, type1) + minDistance(station2, type2, 10, type2);
int d2 = minDistance(station1, type1, 12, type1) + minDistance(station2, type2, 15, type2);
return min(d1, d2);
}
}
int computeStation(const string &s, bool &type){
const string tmp(s.begin()+1, s.end());
int num = stoi(tmp);
if (s[0] == ‘A‘)
{
type = 0;
if (num >= 10) num++;
if (num >= 14) num++;
return num;
}
else if (s[0] == ‘B‘)
{
type = 1;
if (num >= 6) num++;
if (num >= 11) num++;
return num;
}
return -1*num;
}
int passStations(const string &s1, const string &s2){
bool type1 = 0, type2 = 0;
int station1 = computeStation(s1, type1);
int station2 = computeStation(s2, type2);
if (station1 <= 0 && station2 <= 0)
{
return station1 == station2 ? 1 : 6;
}
else if (station1 <= 0)
{
if (station1 == -1) return min(minDistance(10,0,station2,type2), minDistance(6,1,station2,type2))+1;
else return min(minDistance(15,0,station2,type2), minDistance(12,1,station2,type2))+1;
}
else if (station2 <= 0)
{
if (station2 == -1) return min(minDistance(10,0,station1,type1), minDistance(6,1,station1,type1))+1;
else return min(minDistance(15,0,station1,type1), minDistance(12,1,station1,type1))+1;
}
else
{
return minDistance(station1,type1,station2,type2)+1;
}
}
int main()
{
string s1, s2;
cin >> s1 >> s2;
cout << passStations(s1,s2) << endl;
return 0;
}
32.输入一串数,以’,’分隔,输出所有数中去掉最大值、最小值之后剩下的个数。(其中最大值与最小值可能有多个)
Smple input:3,3,5,3,6,9,7,9 Sample outPut: 3
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int residualNum(const string &s){
vector<int> v;
auto begin = s.begin();
while (begin != s.end())
{
auto ahead = begin + 1;
while (ahead != s.end() && *ahead != ‘,‘)
{
ahead++;
}
string tmp(begin, ahead);
v.push_back(stoi(tmp));
if (ahead == s.end()) break;
begin = ++ahead;
}
int min = v[0], max = v[0], minCount = 1, maxCount = 1;
for (int i = 1; i < v.size(); i++)
{
if (v[i] < min){
min = v[i];
minCount = 1;
}
else if (v[i] == min)
{
minCount++;
}
if (v[i] > max){
max = v[i];
maxCount = 1;
}
else if (v[i] == max)
{
maxCount++;
}
}
return v.size() - minCount - maxCount;
}
int main()
{
string s;
cin >> s;
cout << residualNum(s) << endl;
return 0;
}
33.要从5个人中选取2个人作为礼仪,其中每个人的身高范围为160-190,要求2个人的身高差值最小(如果差值相同的话,选取其中最高的两人),以升序输出两个人的身高。
Smple input:161 189 167 172 188 Sample outPut: 188 189
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void selectMan(vector<int> &v, int &i1, int &i2){
sort(v.begin(),v.end());
int min = v[1] - v[0], index = 0;
for (unsigned i = 1; i < v.size()-1; i++)
{
if (v[i+1] - v[i] <= min)
{
min = v[i+1] - v[i];
index = i;
}
}
i1 = v[index];
i2 = v[index+1];
}
int main()
{
vector<int> v;
for (int i = 0; i < 5; i++)
{
int num;
cin >> num;
v.push_back(num);
}
int i1,i2;
selectMan(v,i1,i2);
cout << i1 << " " << i2 << endl;
return 0;
}
34.输入一串字符串,其中有普通的字符与括号组成(包括‘(’、‘)’、‘[’,’]’),要求验证括号是否匹配,如果匹配则输出0、否则输出1.
Smple input:dfa(sdf)df[dfds(dfd)] Smple outPut:0
#include <iostream>
#include <string>
#include <stack>
using namespace std;
bool isMatch(const string &s){
stack<char> charStack;
for (auto begin = s.begin();begin != s.end();++begin){
if (*begin == ‘(‘ || *begin == ‘[‘ || *begin == ‘{‘)
{
charStack.push(*begin);
continue;
}
if (*begin == ‘)‘)
{
if (!charStack.empty() && charStack.top() == ‘(‘)
{
charStack.pop();
continue;
}
return false;
}
if (*begin == ‘]‘)
{
if (!charStack.empty() && charStack.top() == ‘[‘)
{
charStack.pop();
continue;
}
return false;
}
if (*begin == ‘}‘)
{
if (!charStack.empty() && charStack.top() == ‘{‘)
{
charStack.pop();
continue;
}
return false;
}
}
return !charStack.empty();
}
35.判断回文数,是返回1,不是返回0。
#include <iostream>
#include <vector>
using namespace std;
bool isPalindrome(int n){
vector<int> v;
while (n != 0)
{
v.push_back(n%10);
n /= 10;
}
for (unsigned i = 0; i < v.size()/2; i++)
{
if (v[i] != v[v.size()-i-1])
{
return false;
}
}
return true;
}
int main()
{
int n;
cin >> n;
cout << isPalindrome(n) << endl;
return 0;
}
标签:
原文地址:http://blog.csdn.net/sps900608/article/details/52190739