作者bil193 (丁丁)
看板C_and_CPP
標題[ACM ] 10903 緩衝區?
時間Sun Dec 13 13:16:21 2009
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 )
( 未必需要依照此格式,文章條理清楚即可 )
題號: 10903
http://luckycat.kshs.kh.edu.tw/homework/q10903.htm
http://tinyurl.com/y8r7jos
這是我AC的code:(看黃色字即可)
========================================
#include <iostream>
#include<cstring>
using namespace std;
typedef struct player{
int win;
int lose;
}player;
player myplayer[110];
int compare(char* refa,char* refb);
int main()
{
char buf[32]={'0'},astr[10]={'0'},bstr[10]={'0'};
int a,b,i,n,k,tmp,j=0;
while(true)
{
scanf("%d",&n);
if(n==0) break;
scanf("%d",&k);
memset(&myplayer,0,sizeof(player)*110);
if(j>0)
printf("\n");
j++;
for(i=0;i<k*n*(n-1)/2;i++)
{
scanf("%d %s %d %s",&a,&astr,&b,&bstr);
tmp=compare(astr,bstr);
if(tmp==1)
{
myplayer[a-1].win++;
myplayer[b-1].lose++;
}
else if(tmp==-1)
{
myplayer[b-1].win++;
myplayer[a-1].lose++;
}
}
for(i=0;i<n;i++)
{
if(myplayer[i].win ==0 && myplayer[i].lose==0)
printf("-\n");
else
printf("%.3lf\n",myplayer[i].win/double(myplayer[i].win+myplayer[i].lose));
}
}
return 0;
}
int compare(char* refa,char* refb)
{
if(refa[0]=='s' && refb[0]=='p')
return 1;
else if(refa[0]=='p' && refb[0]=='r')
return 1;
else if(refa[0]=='r' && refb[0]=='s')
return 1;
else if(refa[0]=='r' && refb[0]=='p')
return -1;
else if(refa[0]=='p' && refb[0]=='s')
return -1;
else if(refa[0]=='s' && refb[0]=='r')
return -1;
else
return 0;
}
=============================================
然而,如果用我改成這樣:(紅色是修改的)
(用常用的fgets和sscanf來讀入字串和分析)
================================================
#include <iostream>
#include<cstring>
using namespace std;
typedef struct player{
int win;
int lose;
}player;
player myplayer[110];
int compare(char* refa,char* refb);
int main()
{
char buf[32]={'0'},astr[10]={'0'},bstr[10]={'0'};
int a,b,i,n,k,tmp,j=0;
while(true)
{
scanf("%d",&n);
if(n==0) break;
scanf("%d",&k);
memset(&myplayer,0,sizeof(player)*110);
if(j>0)
printf("\n");
j++;
for(i=0;i<k*n*(n-1)/2;i++)
{
//fflush(stdin);
fgets(buf,sizeof(buf),stdin); //改成紅色兩行這樣就會TLE
sscanf(buf,"%d %s %d %s",&a,&astr,&b,&bstr);
tmp=compare(astr,bstr);
//fgets上面那行fflush(stdin)如果使用
if(tmp==1)
//的話(有正確結果)但仍TLE
{
myplayer[a-1].win++;
myplayer[b-1].lose++;
}
else if(tmp==-1)
{
myplayer[b-1].win++;
myplayer[a-1].lose++;
}
}
for(i=0;i<n;i++)
{
if(myplayer[i].win ==0 && myplayer[i].lose==0)
printf("-\n");
else
printf("%.3lf\n",myplayer[i].win/double(myplayer[i].win+myplayer[i].lose));
}
}
return 0;
}
int compare(char* refa,char* refb)
{
if(refa[0]=='s' && refb[0]=='p')
return 1;
else if(refa[0]=='p' && refb[0]=='r')
return 1;
else if(refa[0]=='r' && refb[0]=='s')
return 1;
else if(refa[0]=='r' && refb[0]=='p')
return -1;
else if(refa[0]=='p' && refb[0]=='s')
return -1;
else if(refa[0]=='s' && refb[0]=='r')
return -1;
else
return 0;
}
==========================================================
分析後得到的結果是:
要讀
2 4
1 rock 2 paper
用
scanf("%d",&n);
scanf("%d",&k);
scanf("%d %s %d %s",&a,&astr,&b,&bstr);
是可以。
但用
scanf("%d",&n);
scanf("%d",&k);
fflush(stdin); //如果沒有加這行清空緩衝區,fgets會被緩衝區的'\n'用掉
fgets(buf,sizeof(buf),stdin);
sscanf(buf,"%d %s %d %s",&a,&astr,&b,&bstr);
卻會TLE
請問這是為什麼呢?
另外,聽學校老師說不要用fflush(stdin)請問為什麼呢?
大家有什麼清空緩衝區的好方法嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.230.240
推 chchwy:這樣清掉stdin行嗎XDDD 想吃掉\n用 "%d " 加個空格就好 12/13 13:20
→ sunneo:因為那不是標準 你在翻reference的時候應該有看到吧 12/13 17:57