In C + +, you can consider using a template that has already been written to solve the problem:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main ()
{
string line,word;
vector<string> S1;
Getline (Cin,line); Enter a row of data
//bind a row of data together on a Istringstream object
Istringstream stream (line);
while (stream >> Word)
{
s1.push_back (word);
}
for (Auto it = S1.begin (); it!= s1.end (); it++)
{
cout << *it << "" << flush; The contents of the buffer are immediately sent into the cout and the output buffer is flushed
}
cout << Endl;
return 0;
}
It is important to:
Vector<string> str;
string line, Word;
Getline (cin, line); Enter a row
of data Istringstream stream (line);//bind to Istringstream object while
(stream >> Word) to remove the word from the stream stream
{
str.push_back (word);
}
This will cut the input string into words.
If you write by hand, you need to pay attention to the border is very detailed, to be very careful, if you do not write, use this method to replace, this method takes a lot of time, specific topics specific consideration.
Based on this approach, consider a problem:
A set of data in each input file.
The length of three line segments is given in three lines respectively. Each line begins with an integer n (1<=n<=5), indicating the number of words that are given, followed by n English words, each word representing a digit, and the length of the segment being directly spliced by these English words (for example, one two three representative 123,one one representative 11) , each two words are separated by a single space. The numbers 0 to 9 are expressed in the following 10 words: Zero, one, two, three, four, five, six, seven, eight, nine. Data guarantees that each line segment is not 0 in length.
If the triangle can be formed, then the output yes, otherwise the output No.
Input: 1 One 2 One one 3 one one one
Output:
NO
This question, and does not use to the above writing, because the propositional person reduces the difficulty, in each row input before all adds a number, based on this number, can simply according to a word one word input.
The code is as follows:
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
# Include <algorithm>
using namespace std;
Map<string,int> MP = {
{"zero", 0},{"one", 1},{"two", 2},{"three", 3},{"four", 4},
{"Five", 5},{"six", 6}, {"Seven", 7},{"eight", 8},{"Nine", 9}
};
int main ()
{
string s;
int arr[3] = {0};
int k;
for (int i = 0; i < 3; i++)
{
cin >> K;
for (int j = 0; J < K; J +)
{
cin >> s;
Arr[i] = ten * Arr[i] + mp[s];
}
if (Arr[0] + arr[1] > arr[2] && arr[0] + arr[2] > arr[1] && arr[1] + arr[2] > arr[0])
{
cout << "YES" << Endl;
}
else
{
cout << "NO" << Endl;
}
return 0;
}
If you add a little bit of change, ask: Do not enter a number at the beginning of each line, enter it directly:
One//representative 1 One One//representative one One One one
//Representative 111
Then this problem requires the knowledge of cutting strings.
The code is as follows:
#include <iostream> #include <string> #include <sstream> #include <vector> #include <map>
using namespace Std;
Map<string,int> MP = {{"zero", 0},{"one", 1},{"two", 2},{"three", 3},{"four", 4},
{"Five", 5},{"six", 6},{"seven", 7},{"eight", 8},{"Nine", 9}};
int Strtonum (vector<string> str)//According to {int ans = 0;
for (int i = 0; i < str.size (); i++) {ans = * ans + mp[str[i]];
return ans;
int main () {string S,word;
int arr[3] = {0};
int k;
for (int i = 0; i < 3; i++) {vector<string> str;
Getline (CIN, s); Istringstream Stream (s);
Bind to Istringstream object while (stream >> Word)//out Word to {str.push_back (word);//Add container Standby
} Arr[i] = Strtonum (str); if (Arr[0] + arr[1] > arr[2] && arr[0] + arr[2] > arr[1] && arr[1] + arr[2] > Arr[0]) {cout << "YES" << Endl;
else {cout << "NO" << Endl;
return 0; }