HDOJ 2539 點球大戰 --水題阿,但我就是錯了幾次
其實題目很簡單,算法就是模擬,但是要注意題目雖然說沒有歧義,但要注意理解

可能出現的數據情況: no no good。//name 是no 沒進球!
no good good。//name是no good 進球了!
所以在處理上要注意:1。字符串輸入可以有空格--使用gets();
2。從字符串后面數8個判斷是否是“ no good”(注意空格),是則表明沒進,不是則表明進球了。
3。怎么處理從字符串后面數8個呢?還記得數組名和指針的關系?
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int n;
while (scanf("%d",&n)&&n)
{
char name[1002];
int goal[20]={0};
getchar();//過濾回車
for (int i=0;i<n;i++)
{
gets(name);
//puts(name);
int len;
len=strlen(name);
if(len>7)
{
if(strcmp(name+(len-8)," no good")==0)//數組名即指針
{
goal[i]=0;
}
else
goal[i]=1;
}
else goal[i]=1;
}
int t;
t=n/2;
if(n%2==1) t++;
for (i=1;i<=t;i++)
{
printf("%d ",i);
}
printf("Score\n");
int j=0,tmp=0;
for (i=1;i<=t;i++)
{
if(goal[j]==1)
{
printf("O ");
tmp++;
}
else printf("X ");
j+=2;
}
printf("%d\n",tmp);
j=1;
tmp=0;
if(n%2==1) t--;
for (i=1;i<=t;i++)
{
if(goal[j]==1)
{
printf("O ");
tmp++;
}
else printf("X ");
j+=2;
}
if(n%2==1) printf("- ");
printf("%d\n",tmp);
}
}
#include <string.h>
using namespace std;
int main()
{
int n;
while (scanf("%d",&n)&&n)
{
char name[1002];
int goal[20]={0};
getchar();//過濾回車
for (int i=0;i<n;i++)
{
gets(name);
//puts(name);
int len;
len=strlen(name);
if(len>7)
{
if(strcmp(name+(len-8)," no good")==0)//數組名即指針
{
goal[i]=0;
}
else
goal[i]=1;
}
else goal[i]=1;
}
int t;
t=n/2;
if(n%2==1) t++;
for (i=1;i<=t;i++)
{
printf("%d ",i);
}
printf("Score\n");
int j=0,tmp=0;
for (i=1;i<=t;i++)
{
if(goal[j]==1)
{
printf("O ");
tmp++;
}
else printf("X ");
j+=2;
}
printf("%d\n",tmp);
j=1;
tmp=0;
if(n%2==1) t--;
for (i=1;i<=t;i++)
{
if(goal[j]==1)
{
printf("O ");
tmp++;
}
else printf("X ");
j+=2;
}
if(n%2==1) printf("- ");
printf("%d\n",tmp);
}
}
posted on 2010-08-12 11:37 Geek.tan 閱讀(430) 評論(0) 編輯 收藏 引用 所屬分類: ACM解題報告