题目描述
聊天时我们经常插入表情符号,如果是连续三个字符”:-)”表示高兴,而”:-(“表示悲伤。给定一段聊天信息,判定其总体的情绪。
输入
仅一行字符串,长度在1-255 之间。
输出
输出有四种情况:
(1)信息中不含任何表情符号,输出”none”
(2)信息中高兴表情和悲伤表情数目相同,输出”unsure”
(3)信息中高兴表情数目多于悲伤表情数目,输出”happy”
(4)信息中高兴表情数目少于悲伤表情数目,输出”sad”
样例输入
:)
样例输出
none
提示
题意还是很简单的,就是求两个模板字符串在主字符串中出现的次数。直接暴力搜索就好了。可我却卡了很久,样例过了,可怎么也过不了oj。后来发现,字符串可以出现空格,wtf!当时我从cin>>s;改到scanf(”%s”,s);却没改到gets(s);!
科普一下。
cin>>s;和scanf(”%s”,s);都可以读字符串,但只能读到空白符或换行符为止。
而gets(s);却可以读一行字符串,包括空格,直到换行符为止。
两者的使用出现在不同情况,以后要多注意两者的区别再使用!
代码
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char s[256];
gets(s);
int u=0,h=0;//u不开心字符数量,h为开心字符数量
int i=0;
int len=strlen(s);
for(i=0;i<len-2;i++)
{
if(s[i]==':'&&s[i+1]=='-'&&s[i+2]==')')
h++;
if(s[i]==':'&&s[i+1]=='-'&&s[i+2]=='(')
u++;
}
if(h==0&&u==0)
printf("none\n");
else if(h==u)
printf("unsure\n");
else if(h>u)
printf("happy\n");
else
printf("sad\n");
return 0;
}
练习网址:http://acm.hnust.edu.cn/JudgeOnline/problem.php?id=1752