HDU 2072单词数
2015-10-05 09:47
375 查看
题目链接:HDU2072
Code:
需要注意的是:如果把if(c!=’ ‘) s+=c;中的判断条件去掉,则“ my friend my”输出为3,而不是正确结果2,因为当行首有空格时,错误的解法会把空格(仅为第一个)加进第一个字符串。
Code:
题目意思:求每一行不同单词数 解题思路:利用STL中的set保存取到的单词输出set.size()
Code:
#include<iostream> #include<ctype.h> #include<cstdio> #include<set> using namespace std; int main() { set<string> Set; char c; while((c=getchar())!='#') { string s=""; while(c!='\n') { if(c!=' ') s+=c; while((c=getchar())!=' '&&c!='\n') s+=c; if(s.length()) Set.insert(s); s=""; } cout<<Set.size()<<endl; Set.clear(); } }
需要注意的是:如果把if(c!=’ ‘) s+=c;中的判断条件去掉,则“ my friend my”输出为3,而不是正确结果2,因为当行首有空格时,错误的解法会把空格(仅为第一个)加进第一个字符串。
若是求每行单词数,不必考虑相同与否,则题目更为简单。
Code:
#include<iostream> #include<ctype.h> #include<cstdio> using namespace std; int main() { char s[512]; s[0]=' '; while(true) { gets(s+1); int count=0; if(s[1]=='#') break; for(int i=1;s[i];i++) if(isalpha(s[i])&&s[i-1]==' ') count++; cout<<count<<endl; } }
相关文章推荐
- Integral of sin(x)/x
- 2014华中科技大学复试
- ubuntu(linux)在打开终端时自动全屏或最大化设置
- codeforces 245H H. Queries for Number of Palindromes(区间dp)
- 2.14 折线图
- sort函数
- Swift学习笔记13——类继承(Inheritance)
- java拾遗之abstract class 与 interface
- 开源协议的比较
- 再次递归思想-路劲跨越多个阵列
- 安卓手机已保存WiFi密码查看助手(开源)
- 页面架构-布局解决方案
- ZOJ 2760 How Many Shortest Path 最大流+floyd求最短路
- 框架的小知识集
- 如何学好编程
- 计算器
- 转:Nutch学习笔记--抓取过程简析
- LinkedList的万能用法
- Swift语法之 guard
- 谈论json - json经常使用的功能