注册 登录
编程论坛 C++教室

NKOJ 1023: A+B+C+D+... 的挑战

HJin 发布于 2007-07-19 07:59, 2230 次点击

This is an annoying problem. How do you handle the newline char '\n' in the input stream?

Although my C++ code was accepted, I am not satisfied --- I used cin.peek(). So the qeustion remains to be

1. Can you do it in C?
2. Can you do it without using cin.peek() and/or putback()?

Note that there maybe more than one spaces between two numbers, and there maybe 0 or more spaces before '\n'.


Problem statement:

http://acm.nankai.edu.cn/p1023.html

对于输入的数据进行求和。

Input
输入有多行数据,每行有若干整数,这些整数数以空格分割,请分别求出每行整数的和。

Output
输出的每行对应输入的每行,每行一个数字,即为输入的一行整数之和。

Sample Input
100 200 4
45 45

Sample Output
304
90

[此贴子已经被作者于2007-7-19 8:02:19编辑过]

18 回复
#2
aipb20072007-07-19 09:38
我的第一直觉就是用string流,getline每一行分别处理。

不知道是否符合你的要求。
#3
leeco2007-07-19 13:49
1023 Accepted GNU C 0.29k 0ms 672KB 2007-07-19 13:45:05

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int n,res;
    char buf[10000],*p;
    while(gets(buf)){
        sscanf(strtok(buf,\" \n\"),\"%d\",&res);
        while(p=strtok(NULL,\" \n\")){
            sscanf(p,\"%d\",&n);
            res+=n;
        }   
        printf(\"%d\n\",res);
    }
}
#4
HJin2007-07-19 16:18
以下是引用leeco在2007-7-19 13:49:00的发言:
1023 Accepted GNU C 0.29k 0ms 672KB 2007-07-19 13:45:05

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int n,res;
    char buf[10000],*p;
    while(gets(buf)){
        sscanf(strtok(buf,\" \n\"),\"%d\",&res);
        while(p=strtok(NULL,\" \n\")){
            sscanf(p,\"%d\",&n);
            res+=n;
        }   
        printf(\"%d\n\",res);
    }
}

very smart soln --- thought there is some obvious solution, but it seems it is not that obvious.

Here is my C++ version (I thought those who are interested in this problem already tried it)


#include <iostream>
using namespace std;


int main()
{
    int a, sum=0;


    while( cin>> a )
    {
        sum += a;
        while(cin.peek() == ' ')
            getchar();
        if(cin.peek() == '\n')
        {
            cout<<sum<<endl;
            sum = 0;
        }
    }


    return 0;
}


#5
mp3aaa2007-07-20 04:38
#include<conio.h>
main()
{
int c,b=0;
char e;
while(1){while(scanf("%d%c",&c,&e)&&e!='\n')b+=c;printf("%d",b+c);}
}
一看就知道是用缓冲区来做 不过他出这个题有什么意思啊?
#6
HJin2007-07-20 05:06
mp3aa:

seems to be a good idea.

tried you soln, does not handle the case in which you have spaces before '\n' well, say

4 5 6 '\n'
1 2 3
#7
mp3aaa2007-07-20 13:54
忘了 我先做的while(scanf("%d%c",&c,&e)&&e!='\n')b+=c;printf("%d\n",b+c) 后来加的while 忘了初始值了
#include<conio.h>
#include<stdio.h>
main()
{
int c,b=0;
char e;
while(1){
c=0,b=0;
while(scanf("%d%c",&c,&e)&&e!='\n')b+=c;printf("%d\n",b+c);}
}

[此贴子已经被作者于2007-7-20 13:54:40编辑过]

#8
mp3aaa2007-07-20 13:59
楼上是外国人么
#9
Not2007-07-20 14:21
How about this case.
1000000000000000 121212312313 21212121 0
3 2 2 0 4 5
#10
mp3aaa2007-07-20 14:39
以下是引用Not在2007-7-20 14:21:05的发言:
How about this case.
1000000000000000 121212312313 21212121 0
3 2 2 0 4 5

要是大数相加我还不做了来 嫌麻烦!

#11
HJin2007-07-20 15:20
you are not asked to deal with overlfow --- instead the main prolbem is to handle the '\n' in the input stream.

#12
mp3aaa2007-07-20 16:59
以下是引用HJin在2007-7-20 15:20:36的发言:
you are not asked to deal with overlfow --- instead the main prolbem is to handle the '\n' in the input stream.

回车没问题啊?我在TC下和 CFREE下都可以
还有 你能不能说中文啊
You can speak Chinese?

只有本站会员才能查看附件,请 登录


[此贴子已经被作者于2007-7-20 17:03:45编辑过]

#13
HJin2007-07-20 20:03
1 2 3
6
4 5
9
9 10
19
11 13 '\n' (note there are 3 spaces before '\n')

Your program for the final test input results in a dead loop.
#14
mp3aaa2007-07-20 23:30
以下是引用HJin在2007-7-20 20:03:49的发言:
1 2 3
6
4 5
9
9 10
19
11 13 '\n' (note there are 3 spaces before '\n')

Your program for the final test input results in a dead loop.

...不是吧 题目只是说有空格分开但没有说最后家上空格啊。。。。
按这样挑毛病的话 那毛病太多了

#15
leeco2007-07-21 00:50
回复:(mp3aaa)以下是引用HJin在2007-7-20 20:03:49...
以通过ONLINE JUDGE的测试数据为准。你过了就算你对,否则就是错的。
#16
mp3aaa2007-07-21 02:21
恩 我试过了如果我不用死循环的话 就过了 不过我又加了些代码。。。。
#17
leeco2007-07-21 22:39
回复:(mp3aaa)恩 我试过了如果我不用死循环的话 就...

能否看一下你的方法,发一个过掉的完整代码看看

#18
mp3aaa2007-07-22 16:44

我这里用了两个代码 一个是后来做出来的 一个是改进的 (虽然改进的不在是死循环 但是因为在有些编译器里要输入两个ctrl+z才能退出 所以通不过)

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char input[100],*p;
int i=0;
while(gets(input)){
i=0;i+=atoi(strtok(input," "));
while(p=strtok(NULL," "))
i+=atoi(p);
printf("%d\n",i);
}
}



只有本站会员才能查看附件,请 登录

[此贴子已经被作者于2007-7-22 16:47:08编辑过]

#19
mp3aaa2007-07-22 16:45
#include<stdio.h>
int main()
{
int c,b=0;
char e,k='o';
while(e!=EOF){
c=b=0;
while(scanf("%d",&c)&&printf("%d\b"+!(k==(e=getchar())))&&e==' ')b+=c;printf("%d\n",c+b);}
}
1