ZOJ Monthly, November 2010解题报告 » ZOJ3427


#include <list>
#include <cstdio>

using namespace std;

char buf[65536], *pbuf;

int main() {
	int x, y, z, nbuf;
	list<int> a, b;
	list<int>::iterator it;

	while (scanf(" [ %d : %d ]%[^\n]", &x, &y, buf) != EOF) {
		pbuf = buf;
		while (sscanf(pbuf, "%*[^-0-9]%d%n", &z, &nbuf) > 0) {
			pbuf += nbuf;

		it = a.begin();
		advance(it, x);
		for (int i = x; i < y; ++i) {
			printf("%s%d", i == x ? "" : ", ", *it);
			it = a.erase(it);
		a.splice(it, b);

	return 0;

//Run ID 	Submit Time 	Judge Status 	Problem ID 	Language 	Run Time(ms) 	Run Memory(KB) 	User Name 	Admin
//420 	2010-11-13 03:42:47 	Accepted 	A 	C++ 	0 	244 	watashi@Zodiac 	Source
4 Responses to “ZOJ3427”
  1. kirk says:

    大牛,为什么 scanf(” [ %d : %d ]%[^\n]“, &x, &y, buf) 改为
    scanf(” [ %d : %d ] =%[^\n]“, &x, &y, buf)就莫名其妙的WA了

    • watashi says:

      如果一行恰好是”…. =\n”这样的,那么后面那个读法就只读入两个东西,%[^\n]会读入失败,所以buf的值就不会修改,还是上次读入的值

  2. lijunle says:

    这题按照大牛的代码,慢慢改自己的,WA得不明不白,AC得更加不明不白。大牛,我找不到有%n这个用法, 可以介绍一些资料么?

    • watashi says:

      以下内容来自manpage (man -s 3 scanf)

      n Nothing is expected; instead, the number of characters consumed
      thus far from the input is stored through the next pointer,
      which must be a pointer to int. This is not a conversion,
      although it can be suppressed with the * assignment-suppression
      character. The C standard says: “Execution of a %n directive
      does not increment the assignment count returned at the comple‐
      tion of execution” but the Corrigendum seems to contradict this.
      Probably it is wise not to make any assumptions on the effect of
      %n conversions on the return value.

      资料还是多看文档,scanf和printf还有cstdlib都有很多功能很强大的函数,更不用说numeric, algorithm和functional

Leave a Reply