题解 P4414 【[COCI2006-2007#2] ABC】

· · 题解

简单模拟,可是却难了我一个晚上。

0分WA代码

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int dat[3];
char ord[3];

int main()
{
    for (int i=0; i<3; i++)
        scanf("%d",&dat[i]);
    for (int i=0; i<3; i++)
        scanf("%c",&ord[i]);
    sort(dat,dat+3);
    for (int i=0; i<3; i++)
        printf("%d ",dat[ord[i]-'A']);
    return 0;
}

调试时,才发现scanf("%c")会读入回车符。 有三个解决办法:

  1. 在两次输入间加入两个getchar(),注意一定是两个,因为回车包含\n和\r。
  2. 改为scanf("%s",ord)。
  3. 改用更好的cin。 然后就可以AC了。