hdoj_Problem1.1.8_A+B for Input-Output Practice (VIII)
A+B for Input-Output Practice (VIII)
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
Problem Description
Your task is to calculate the sum of some integers.
Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
Output
For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.
留意between
Sample Input
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3
Sample Output
10
15
6
- 第一次提交没有注意到between,OJ返回
Presentation Error
between的意思是,输出的数字之间用空行分开,但最后一个输出下面是没有空行
- 增加判断条件之后第二次提交通过。
Solution
#include <cstdio>
using namespace std;
#define DEBUG 0 //本地调试定义为1, 提交时定义为0
#if DEBUG
#else
#define fp stdin //当DEBUG被定义为0时, fp被替换为stdin
#endif
int main() {
#if DEBUG
FILE* fp;
fp = fopen("a.txt", "r"); //a.txt作为文件输入
#endif
int n;
while (fscanf(fp, "%d", &n) != EOF) {
break;
}
int* sum_arr = new int[n];
for (int i = 0; i < n; i++) {
int m;
while (fscanf(fp, "%d", &m) != EOF) {
break;
}
int sum = 0;
for (int j = 0; j < m; j++) {
int add;
while (fscanf(fp, "%d", &add) != EOF) {
break;
}
sum += add;
}
sum_arr[i] = sum;
}
for (int i = 0; i < n; i++) {
printf("%d\n", sum_arr[i]);
if (i < n - 1) {
printf("\n");
}
}
delete[] sum_arr;
return 0;
}
如果觉得要反复修改宏定义的DEBUG
麻烦,可以把调试语句改为
#ifdef DEBUG
#else
#define fp stdin
#endif
int main() {
#ifdef DEBUG
FILE* fp;
fp = fopen("a.txt", "r");
#endif
需要进行本地调试时,使用g++编译选项定义DEBUG
$ g++ 1.1.8.cpp -DDEBUG
运行
$./a