1. 计算字符串最后一个单词的长度,单词以空格隔开。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
string s;
getline(cin,str);
s = str.substr(str.find_last_of(' ') + 1, str.length()-str.find_first_of(' '));
cout << s.length() << endl;
return 0;
}
2.
计算最少出列多少位同学,使得剩下的同学排成合唱队形
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n;
cin >> n;
int nt = n;
vector<int> a;
int num;
while (nt>0 && cin >> num)
{
a.push_back(num);
nt--;
}
int maxlen = 0;
int temp = 0;
int*b = new int[n];
int*c = new int[n];
for (int i = 0; i < n; i++)
{
b[i] = 1;
for (int j = 0; j<i; j++)
{
if ((a[i]>a[j]) && (b[j] + 1>b[i]))
b[i] = b[j] + 1;
}
}
for (int i = n - 1; i >= 0; i--)
{
c[i] = 1;
for (int j = n - 1; j > i; j--)
{
if ((a[i] > a[j]) && (c[j] + 1 > c[i]))
c[i] = c[j] + 1;
}
}
for (int i = 0; i<n; i++)
if (b[i] + c[i] - 1>maxlen)
maxlen = b[i] + c[i] - 1;
cout << n - maxlen;
delete[] b;
delete[] c;
return 0;
}