L1-吃火锅
以上图片来自微信朋友圈:这种天气你有什么破事打电话给我基本没用。但是如果你说“吃火锅”,那就厉害了,我们的故事就开始了。
本题要求你实现一个程序,自动检查你朋友给你发来的信息里有没有 chi1 huo3 guo1。
输入格式:
输入每行给出一句不超过 80 个字符的、以回车结尾的朋友信息,信息为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。当读到某一行只有一个英文句点 . 时,输入结束,此行不算在朋友信息里。
输出格式:
首先在一行中输出朋友信息的总条数。然后对朋友的每一行信息,检查其中是否包含 chi1 huo3 guo1,并且统计这样厉害的信息有多少条。在第二行中首先输出第一次出现 chi1 huo3 guo1 的信息是第几条(从 1 开始计数),然后输出这类信息的总条数,其间以一个空格分隔。题目保证输出的所有数字不超过 100。
如果朋友从头到尾都没提 chi1 huo3 guo1 这个关键词,则在第二行输出一个表情 -_-#。
输入样例 1:
Hello!
are you there?
wantta chi1 huo3 guo1?
that's so li hai le
our story begins from chi1 huo3 guo1 le
.
输出样例 1:
5
3 2
输入样例 2:
Hello!
are you there?
wantta qi huo3 guo1 chi1huo3guo1?
that's so li hai le
our story begins from ci1 huo4 guo2 le
.
输出样例 2:
5
-_-#
一直卡中间的一个点,不知道是哪里的错。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <math.h>
using namespace std;
int main()
{
string s;
int cnt=0;
int sum=0,flag=0;
while(getline(cin,s) && s!="."){
cnt++;
int len = s.length();
for(int i=0;i<len;i++){
if(s[i]=='c'&&s[i+1]=='h'&&s[i+2]=='i'&&s[i+3]=='1'){
if(s[i+5]=='h'&&s[i+6]=='u'&&s[i+7]=='o'&&s[i+8]=='3'){
if(s[i+10]=='g'&&s[i+11]=='u'&&s[i+12]=='o'&&s[i+13]=='1'){
sum++;
if(sum==1)
flag=cnt;
}
}
}
}
}
cout<<cnt<<endl;
if(sum==0){
cout<<"-_-#"<<endl;
}
else{
cout<<flag<<" "<<sum<<endl;
}
return 0;
}
修改为采用直接调用查找子串的函数就对了。。当然这也是赛后才学到的方法。。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <math.h>
using namespace std;
int main()
{
string s;
int cnt=0;
int sum=0,flag=0;
string::size_type idx;
string b ="chi1 huo3 guo1";
while(getline(cin,s) && s!="."){
cnt++;
idx=s.find(b);//在 s 中 找 b;
if(idx == string::npos){
//不存在
}
else{
sum++;
if(sum==1){
flag=cnt;
}
}
}
cout<<cnt<<endl;
if(sum==0){
cout<<"-_-#"<<endl;
}
else{
cout<<flag<<" "<<sum<<endl;
}
return 0;
}
当然,以下写法更简单,s.find("###")==-1 说明没找到,否则返回找到的位置。由于本题无需判断位置,只要不等于-1即可。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string s;
int cnt=0,first,sum=0;
while(getline(cin,s) && s!="."){
sum++;
if(s.find("chi1 huo3 guo1") != -1){
cnt++;
if(cnt==1){
first = sum;
}
}
}
cout<<sum<<endl;
if(cnt==0){
cout<<"-_-#"<<endl;
}
else
cout<<first<<" "<<cnt<<endl;
return 0;
}
补充:判断一个字符串是否是另一个字符串的子串。
STL中的find()函数,提供了强大的功能。
当我们判断一个字符串是否包含另一个字符串的时候,可以使用find();
如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a="abcdefghigklmn";
string b="def";
string c="123";
string::size_type idx;
idx=a.find(b);//在a中查找b.
if(idx == string::npos )//不存在。
cout << "not found\n";
else//存在。
cout <<"found\n";
idx=a.find(c);//在a中查找c。
if(idx == string::npos )//不存在。
cout << "not found\n";
else//存在。
cout <<"found\n";
return 0;
}
当然了,上述的idx我们也可以定义成int类型.
实际上,可能定义成int类型更符合我们的认知。代码如下:
string str1="abcdefghigklmn";
string str2="def";
string str3="123";
int idx = 0;
idx = str1.find(str2);
cout<<"index_str2 = "<<idx<<endl;
idx = str1.find(str3);
cout<<"index_str3 = "<<idx<<endl;
输出的结果:
显然,
str2是str1的字串,返回的是第一个匹配字符的索引;
str3不是str1的子串,返回的是-1;
因此,我们可以通过返回值idx来确定是不是子串!
如果是子串,则返回非负整数
如果不是子串,则返回-1;
L1-8 刮刮彩票(20)
“刮刮彩票”是一款网络游戏里面的一个小游戏。如图所示:
每次游戏玩家会拿到一张彩票,上面会有 9 个数字,分别为数字 1 到数字 9,数字各不重复,并以 3×3 的“九宫格”形式排布在彩票上。
在游戏开始时能看见一个位置上的数字,其他位置上的数字均不可见。你可以选择三个位置的数字刮开,这样玩家就能看见四个位置上的数字了。最后玩家再从 3 横、3 竖、2 斜共 8 个方向中挑选一个方向,方向上三个数字的和可根据下列表格进行兑奖,获得对应数额的金币。
输入格式:
输入第一部分给出一张合法的彩票,即用 3 行 3 列给出 0 至 9 的数字。0 表示的是这个位置上的数字初始时就能看见了,而不是彩票上的数字为 0。
第二部给出玩家刮开的三个位置,分为三行,每行按格式 x y 给出玩家刮开的位置的行号和列号(题目中定义左上角的位置为第 1 行、第 1 列。)。数据保证玩家不会重复刮开已刮开的数字。
最后一部分给出玩家选择的方向,即一个整数: 1 至 3 表示选择横向的第一行、第二行、第三行,4 至 6 表示纵向的第一列、第二列、第三列,7、8分别表示左上到右下的主对角线和右上到左下的副对角线。
输出格式:
对于每一个刮开的操作,在一行中输出玩家能看到的数字。最后对于选择的方向,在一行中输出玩家获得的金币数量。
输入样例:
1 2 3
4 5 6
7 8 0
1 1
2 2
2 3
7
输出样例:
1
5
6
180
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <math.h>
using namespace std;
int main()
{
int a[3][3]={0};
int p,sum=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cin>>a[i][j];
sum+=a[i][j];
}
}
//是0的话要换成它本来应该是的数,1~9 没有0,emmmm
//比赛的时候怎么都没理解它的意思,没读懂,,心酸
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(a[i][j]==0){
a[i][j]=45-sum;
}
}
}
int x,y;
for(int i=0;i<3;i++){
cin>>x>>y;
printf("%d\n",a[x-1][y-1]);
}
int d;
cin>>d;
int ans=0;
if(d==1){
ans = a[0][0]+a[0][1]+a[0][2];
}
if(d==2){
ans = a[1][0]+a[1][1]+a[1][2];
}
if(d==3){
ans = a[2][0]+a[2][1]+a[2][2];
}
if(d==4){
ans = a[0][0]+a[1][0]+a[2][0];
}
if(d==5){
ans = a[0][1]+a[1][1]+a[2][1];
}
if(d==6){
ans = a[0][2]+a[1][2]+a[2][2];
}
if(d==7){
ans = a[0][0]+a[1][1]+a[2][2];
}
if(d==8){
ans = a[0][2]+a[1][1]+a[2][0];
}
if(ans==6){
cout<<"10000"<<endl;
}
if(ans==7){
cout<<"36"<<endl;
}
if(ans==8){
cout<<"720"<<endl;
}
if(ans==9){
cout<<"360"<<endl;
}
if(ans==10){
cout<<"80"<<endl;
}
if(ans==11){
cout<<"252"<<endl;
}
if(ans==12){
cout<<"108"<<endl;
}
if(ans==13){
cout<<"72"<<endl;
}
if(ans==14){
cout<<"54"<<endl;
}
if(ans==15){
cout<<"180"<<endl;
}
if(ans==16){
cout<<"72"<<endl;
}
if(ans==17){
cout<<"180"<<endl;
}
if(ans==18){
cout<<"119"<<endl;
}
if(ans==19){
cout<<"36"<<endl;
}
if(ans==20){
cout<<"306"<<endl;
}
if(ans==21){
cout<<"1080"<<endl;
}
if(ans==22){
cout<<"144"<<endl;
}
if(ans==23){
cout<<"1800"<<endl;
}
if(ans==24){
cout<<"3600"<<endl;
}
return 0;
}
L1- 7 前世档案(20)
输入样例:
3 4
yny
nyy
nyn
yyn
输出样例:
3
5
6
2
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <math.h>
using namespace std;
int main()
{
int m,n;
cin>>n>>m;
char c;
getchar();
for(int i=0;i<m;i++){
int cnt = 1;
for(int j=0;j<n;j++){
c = getchar();
if(c=='y'){
cnt = cnt*2;
}
else{
cnt = cnt*2+1;
}
}
int y=pow(2,n)-1;//上一层最后一个节点编号
cout<<cnt-y<<endl;
getchar();
}
return 0;
}