CVTE第二次笔试
选择瞎答得,直接编程题目
1.
使用递归将字符串中的数字去并按顺序打印
输入例 adfsafsfs123123eogie09789
输出例
123123
09789
#include<iostream>
using namespace std;
void func(int ptr, string &str, bool end)
{
if(ptr == str->length())
return;
if(ptr<str->length() && str[ptr]>='0' && str[ptr]<='9')
{
end = false;
cout<<str[ptr];
}
else
{
if(end == false)
cout<<"\n";
end = true;
}
func(ptr+1, str, end);
}
int main()
{
string str;
cin>>str;
func(0, str, false);
return 0;
}
2.动态规划问题,A,B,C,D,E五个站点,每个站点之间有三种交通方式,公交、地铁、出租,输入一个表,该表有四行三列,每行表示两个站点间三种交通方式的费用,五个站点最多允许换乘两次,求最小花费。
StationCount = 5;
TramsportCount = 3;
int MinimumCost(const int ticketPrices[StationCount-1][TramsportCount])
{
int min = 0xffffffff;
for(int i=0; i<StationCount-1; ++i)
{
int cost = ticketPrices[0][i];
short tc = 0;
for(int j=0; j<StationCount-1; ++j)//可能发生换乘换乘
{
cost = cost+ticketPrices[1][j];
if(i !=j )
++tc;
for(int k=0; k<StationCount-1; ++k)//可能发生换乘换乘
{
cost = cost+ticketPrices[2][k];
if(j != k)
++tc;
for(int m=0; m<StationCount-1; ++m)//检查是否还可以换乘
{
if(tc < 2)
{
cost += ticketPrices[3][m];
if(cost < min)
min = cost;
}
else
{
cost += cost+ticketPrices[3][k];
if(cost < min)
min = cost;
break;
}
}
}
}
}
return min;
}
不积小流无以成江河

浙公网安备 33010602011771号