双指针
题目描述:
输入一行字符串,其中包括,单词和空格,输出每个单词,一个单词占一行。
输入样例:
red blue green pink black white yellow
输出样例:
red
blue
green
pink
black
white
yellow
双指针算法:
# include <iostream>
# include <string>
using namespace std;
int main()
{
string str;
getline(cin, str);
int n = str.size();
for (int i = 0; i < n; i++)
{
int j = i;
while (j < n && str[j] != ' ')j++;
for (int k = i; k < j; k++)
cout << str[k];
cout << endl;
i = j;
}
return 0;
}
直接解法:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
while (cin >> s)
{
cout << s << endl;
}
return 0;
}
双指针算法公式