WA了一万次!!!
一般的数据都过了有木有!
试试你只输入空格的输出数据!
有木有啊有木有!
1.记得数组初始化为0
2.添加单词成功后才进行是否插入单词的操作
//代码写得太难看 欢迎拍砖
第一次用STL的set写的
第二次用数组写的
SET:
#include "iostream"
#include "algorithm"
#include "set"
#include "string"
#include "vector"
using namespace std;
int main()
{
set<string> s;
string t;
while (getline(cin,t)&& t[0] != '#')
{
int ok = 0;
char a[100];
int c = 0,i = 0;
while (t[i] && i < t.length()) {
int ok = 0; //标志变量 是否添加单词成功
memset(a,0,sizeof(a));
while (t[i] == ' ' && t[i]) i++; //忽略前置空格
while (t[i] != ' '&& t[i] ) { a[c++] = t[i++]; ok = 1; }
while (t[i] == ' ' && t[i] ) i++; //忽略中间以及末尾的空格
if (ok) { //添加单词成功才插入
if(s.find(a) == s.end()) s.insert(a);
c = 0;
}
}
cout << s.size() << endl;
s.clear();
}
return 0;
}
数组:
#include "iostream" #include "algorithm" #include "set" #include "string" #include "vector" using namespace std; char n[100][100]; int c = 0; int same() { int sum = 0; for (int i = 0;i < c;++ i) for (int j = i+1;j < c;++ j) { if (strcmp(n[i],n[j])==0) { sum ++; break; } } return sum; } int main() { string s; int i; memset(n,0,sizeof(n)); while (getline(cin,s) && s[0] != '#') { int num = 0; i = 0; while (s[i] && i < s.length()) { int ok = 0; while (s[i] == ' ' && i < s.length()) i++; while (s[i] != ' ' && s[i]) { n[c][num++] = s[i++]; ok = 1;} while (s[i] == ' ' && i < s.length()) i++; num = 0; if (ok) c++; } cout << c - same() << endl; c = 0; memset(n,0,sizeof(n)); } return 0; }