标签:
注:编译环境为Visual Studio 2012,答案仅供参考。
101.合唱队问题
问题描述
N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形。 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2…,K,他们的身高分别为T1,T2,…,TK, 则他们的身高满足T1<…Ti+1>…>TK(1<=i<=K)。 你的任务是,已知所有N位同学的身高,计算最少需要几位同学出列,可以使得剩下的同学排成合唱队形。
输入格式 Input Format 输入的第一行是一个整数N(2<=N<=100),表示同学的总数。第一行有n个整数,用空格分隔,第i个整数Ti(130<=Ti<=230)是第i位同学的身高(厘米)。
输出格式 Output Format 输出包括得到的最优队列的同学个数 以及最终同学的身高排列
#include <iostream>
#include <vector>
using namespace std;
int find(int n){
vector<int> v(n,0), vAsc(n,1), vDesc(n,1);
for (int i = 0; i < n; i++)
{
cin >> v[i];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (v[i] > v[j] && vAsc[i] < vAsc[j] + 1)
{
vAsc[i] = vAsc[i] + 1;
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (v[i] < v[j] && vDesc[i] < vDesc[j] + 1)
{
vDesc[i] = vDesc[i] + 1;
}
}
}
int max = 0;
for (int i = 0; i < n; i++)
{
int tmp = vAsc[i] + vDesc[i] - 1;
if (max < tmp)
{
max = tmp;
}
}
return n - max;
}
int main()
{
int n;
cin >> n;
cout << find(n) << endl;
return 0;
}
102.给定一个n*m的矩阵,请编程实现以逆时针旋转方式打印出它的每个元素,例如输入矩阵
1 8 7
2 9 6
3 4 5
则输出为1 2 3 4 5 6 7 8 9。
#include <iostream>
#include <vector>
using namespace std;
void cycle(const vector<vector<int> > &v, vector<vector<bool> > &canvisit, int row, int col, int n, int m){
int pos1 = row, pos2 = col;
for (int i = row; i < n; i++)
{
if (canvisit[i][col])
{
cout << v[i][col] << " ";
canvisit[i][col] = 0;
pos1 = i;
}
}
for (int i = col; i < m; i++)
{
if (canvisit[pos1][i])
{
cout << v[pos1][i] << " ";
canvisit[pos1][i] = 0;
pos2 = i;
}
}
for (int i = n-1; i >= row; i--)
{
if (canvisit[i][pos2])
{
cout << v[i][pos2] << " ";
canvisit[i][pos2] = 0;
pos1 = i;
}
}
for (int i = m-1; i > col; i--)
{
if (canvisit[pos1][i])
{
cout << v[pos1][i] << " ";
canvisit[pos1][i] = 0;
}
}
}
int main()
{
int n, m;
cin >> n >> m;
vector<vector<int> > v;
vector<bool> tmp_bv(m,1);
vector<vector<bool> > canvisit(n,tmp_bv);
for (int i = 0; i < n; i++)
{
vector<int> tmp(m,0);
for (int j = 0; j < m; j++)
{
cin >> tmp[j];
}
v.push_back(tmp);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (canvisit[i][j]) cycle(v,canvisit,i,j,n,m);
}
}
cout << endl;
return 0;
}
103.请实现一个函数,用来对一段英文进行分词(单词之间包含一个或多个空格),并且对单词进行快速排序(升序)。函数的输入为英文字符串,输出为排序后的单词序列。例如输入“what are you doing”,输出序列{“are”,“doing”,“what”,“you”}
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
bool isLess(const string &s1, const string &s2){
auto b1 = s1.begin(), b2 = s2.begin();
while (b1 != s1.end() && b2 != s2.end())
{
if (*b1 > *b2)
{
return false;
}
else if (*b1 < *b2)
{
return true;
}
b1++;
b2++;
}
if (b1 != s1.end()) return false;
return true;
}
void my_sort(vector<string> &v, int low, int high){
if (low >= high)
{
return;
}
int index = low, nextLow = low, nextHight =high;
while (low < high)
{
for (int i = high; i >= low; i--)
{
high = i;
if (isLess(v[i],v[index]))
{
string tmp = v[i];
v[i] = v[index];
v[index] = tmp;
index = i;
break;
}
}
for (int i = low; i <= high; i++)
{
low = i;
if (isLess(v[index],v[i]))
{
string tmp = v[i];
v[i] = v[index];
v[index] = tmp;
index = i;
break;
}
}
}
my_sort(v,nextLow,index-1);
my_sort(v,index+1,nextHight);
}
void convert(const string &s, vector<string> &result){
istringstream is(s);
string tmp;
while (is >> tmp)
{
cout << tmp << endl;
result.push_back(tmp);
}
my_sort(result,0,(int)result.size()-1);
}
int main()
{
string s;
vector<string> result;
getline(cin,s);
convert(s,result);
cout << "{";
for (unsigned i = 0; i < result.size(); i++)
{
cout << "\"" << result[i] << "\"";
if(i != result.size()-1) cout << ",";
}
cout << "}" << endl;
return 0;
}
104.回文数字间隔
描述:给定一个输入,介于0~999999之间,并且给定的数是回文数,e.g. 123321,那么统计直到下一个回文数之间的间隔,并且当统计到999999时,下一个数为0,即是循环计数。
e.g. 输入:123321
输出:1100
#include <iostream>
#include <vector>
using namespace std;
bool isRight(int n){
vector<int> v;
while (n)
{
v.push_back(n%10);
n /= 10;
}
for (unsigned i = 0; i < v.size()/2; i++)
{
if (v[i] != v[v.size()-1-i])
{
return false;
}
}
return true;
}
int dis(int n){
if (n == 999999)
{
return 1;
}
for (int i = n + 1; i <= 999999; i++)
{
if (isRight(i))
{
return i - n;
}
}
return 999999 - n;
}
int main()
{
int n;
cin >> n;
cout << dis(n) << endl;
return 0;
}
105.字符串匹配
描述:字符串匹配问题,给定两个字符串,求字符串2,在字符串1中的最先匹配结果。字符串2中可以存在’*’符号,且该符号可以代表任意字符,即字符串2中存在通配符。
e.g. 输入:abcdefghabef, a*f
输出:abcdef
#include <iostream>
#include <string>
using namespace std;
void match(const string &s1, const string &s2, string &result){
bool flag;
string tmp;
auto b1 = s1.begin(), b2 = s2.begin();
while (b1 != s1.end())
{
if (*b1 != *b2)
{
b1++;
continue;
}
auto tb1 = b1, tb2 = b2;
flag = true;
while (tb2 != s2.end())
{
if (tb1 == s1.end())
{
flag = false;
break;
}
if (*tb2 != ‘*‘)
{
if (*tb1++ != *tb2++)
{
flag = false;
break;
}
continue;
}
while (tb2 != s2.end() && *tb2 == ‘*‘)
{
tb2++;
}
if (tb2 == s2.end()) break;
while (tb1 != s1.end() && *tb1 != *tb2)
{
tb1++;
}
}
if (flag){
result = string(b1,tb1);
return;
}
}
}
int main()
{
string s1,s2,result;
getline(cin,s1);
getline(cin,s2);
match(s1,s2,result);
cout << result << endl;
return 0;
}
标签:
原文地址:http://blog.csdn.net/sps900608/article/details/52244866