輸入:
3 2
1 3
2 4
輸出:
3 2
1 3
2 4
即**原樣回顯多行數據**,核心是「一次性讀取所有輸入,再一次性輸出」。
#include
#define MAX_LINES 100
#define MAX_LEN 1024
int main() {
char lines[MAX_LINES][MAX_LEN];
int n = 0;
// 逐行讀取,保留換行符
while (n < MAX_LINES && fgets(lines[n], MAX_LEN, stdin)) {
// 移除 fgets 保留的換行符 (可選)
// lines[n][strcspn(lines[n], "\n")] = '\0';
n++;
}
// 一次性輸出
for (int i = 0; i < n; i++) {
fputs(lines[i], stdout); // 自帶換行
}
return 0;
}
#include
#include
#include
int main() {
char **lines = NULL;
size_t capacity = 0, count = 0;
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, stdin)) != -1) {
if (count >= capacity) {
capacity = capacity ? capacity * 2 : 16;
lines = realloc(lines, capacity * sizeof(char*));
}
lines[count] = malloc(read + 1);
memcpy(lines[count], line, read);
lines[count][read] = '\0';
count++;
}
free(line);
for (size_t i = 0; i < count; i++) {
printf("%s", lines[i]);
free(lines[i]);
}
free(lines);
return 0;
}
int c;
while ((c = getchar()) != EOF) putchar(c);
若需解析數字再按格式輸出:
int a, b;
while (scanf("%d %d", &a, &b) == 2) {
printf("%d %d\n", a, b);
}