| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2224 人关注过本帖
标题:NKOJ 1023: A+B+C+D+... 的挑战
只看楼主 加入收藏
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
 问题点数:0 回复次数:18 
NKOJ 1023: A+B+C+D+... 的挑战

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编辑过]

搜索更多相关主题的帖子: NKOJ 挑战 
2007-07-19 07:59
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
我的第一直觉就是用string流,getline每一行分别处理。

不知道是否符合你的要求。

Fight  to win  or  die...
2007-07-19 09:38
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 
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);
}
}
2007-07-19 13:49
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
以下是引用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;
}



I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-07-19 16:18
mp3aaa
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:2013
专家分:8
注 册:2006-2-15
收藏
得分:0 
#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);}
}
一看就知道是用缓冲区来做 不过他出这个题有什么意思啊?

羊肉串 葡萄干 哈密瓜!!
2007-07-20 04:38
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
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

I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-07-20 05:06
mp3aaa
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:2013
专家分:8
注 册:2006-2-15
收藏
得分:0 
忘了 我先做的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编辑过]


羊肉串 葡萄干 哈密瓜!!
2007-07-20 13:54
mp3aaa
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:2013
专家分:8
注 册:2006-2-15
收藏
得分:0 
楼上是外国人么

羊肉串 葡萄干 哈密瓜!!
2007-07-20 13:59
Not
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-7-15
收藏
得分:0 
How about this case.
1000000000000000 121212312313 21212121 0
3 2 2 0 4 5
2007-07-20 14:21
mp3aaa
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:2013
专家分:8
注 册:2006-2-15
收藏
得分:0 
以下是引用Not在2007-7-20 14:21:05的发言:
How about this case.
1000000000000000 121212312313 21212121 0
3 2 2 0 4 5

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


羊肉串 葡萄干 哈密瓜!!
2007-07-20 14:39
快速回复:NKOJ 1023: A+B+C+D+... 的挑战
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017760 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved