zoukankan      html  css  js  c++  java
  • POJ 2993 Emag eht htiw Em Pleh

    连续超时了N次。

    具体不知道是不是因为用了cin.ignore()这个函数导致读到文件尾不能结束。。。?(等待解决)

    可以考虑文件结束符是EOF,而player估计初始化的是NULL,到while的时候由于忽略了这个文件结束符,故会一直等待。

    getline()函数会丢弃最后的enter符。

    网上摘抄总结:

    第一:要注意不同的函数是否接受空格符、是否舍弃最后的回车符的问题!
    读取字符时:
    scanf()以Space、Enter、Tab结束一次输入,不会舍弃最后的回车符(即回车符会残留在缓冲区中);
    getchar()以Enter结束输入,也不会舍弃最后的回车符;
    读取字符串时:
    scanf()以Space、Enter、Tab结束一次输入
    gets()以Enter结束输入(空格不结束),接受空格,会舍弃最后的回车符!

    第二:为了避免出现上述问题,必须要清空缓冲区的残留数据,可以用以下的方法解决:
    方法1:C语言里提供了函数清空缓冲区,只要在读数据之前先清空缓冲区就没问题了!
          这个函数是fflush(stdin)。
    方法2:自己取出缓冲区里的残留数据。
    (说实话这个语句我也没看懂,呵呵!为什么格式控制是这样的!希望高手指点一下!)
          scanf("%[^\n]",string);

    View Code
      1 #include<iostream>
    2 #include<cstdio>
    3 #include<cstring>
    4 #include<cstdlib>
    5 using namespace std;
    6
    7 #define ONLINE
    8
    9 void online()
    10 {
    11 #ifdef ONLINE
    12 #else
    13 freopen("F:\\t1.txt","r",stdin);
    14 freopen("F:\\t2.txt","w",stdout);
    15 #endif
    16 }
    17
    18 char map[9][9];
    19
    20 void init()
    21 {
    22 string player;
    23 cin>>player;
    24 // cin.ignore();
    25
    26 string str;
    27 getline(cin,str);
    28
    29 int i=0;
    30 memset(map,0,sizeof(map));//第二个参数为int
    31 while(str[i]!='\0')
    32 {
    33 ++i;
    34 char ch;
    35 int column,row;
    36 if(str[i]>'Z')
    37 {
    38 ch='P';
    39 column=str[i]-'a'+1;
    40 }
    41 else
    42 {
    43 ch=str[i];
    44 column=str[++i]-'a'+1;
    45 }
    46 row=9-(str[++i]-'0');
    47 map[row][column]=ch;
    48 ++i;
    49
    50 }
    51
    52 cin>>player;
    53 // cin.ignore();
    54
    55 getline(cin,str);
    56 i=0;
    57 while(str[i]!='\0')
    58 {
    59 ++i;
    60 char ch; //大写要转换成小写
    61 int column,row;
    62 if(str[i]>'Z')
    63 {
    64 ch='p';
    65 column=str[i]-'a'+1;
    66 }
    67 else
    68 {
    69 ch=str[i]+'a'-'A';
    70 column=str[++i]-'a'+1;
    71 }
    72 row=9-(str[++i]-'0');
    73 map[row][column]=ch;
    74 ++i;
    75
    76 }
    77 }
    78
    79 void output()
    80 {
    81 string str="+---+---+---+---+---+---+---+---+";
    82 for(int i=1;i<=8;i++)
    83 {
    84 cout<<str<<endl;
    85 for(int j=1;j<=8;j++)
    86 {
    87 char c,ch;
    88 if((i+j)%2==0)
    89 c='.';
    90 else
    91 c=':';
    92 if(map[i][j]!=0)
    93 ch=map[i][j];
    94 else
    95 ch=c;
    96 printf("|%c%c%c",c,ch,c);
    97 }
    98 cout<<"|"<<endl;
    99 }
    100 cout<<str<<endl;
    101 }
    102
    103 int main()
    104 {
    105 online();
    106 init();
    107 output();
    108 return 0;
    109 }

  • 相关阅读:
    104. 二叉树的最大深度
    Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]
    python-admin管理后台
    django-cookies 和session
    django-关系映射
    django-关系映射 一对一 一对多 多对多
    django-Meta类
    django-orm聚合查询和原生数据库查询
    django-F对象、Q对象
    django-orm删除数据
  • 原文地址:https://www.cnblogs.com/YipWingTim/p/2218422.html
Copyright © 2011-2022 走看看