gpt4 book ai didi

c - 读取两行整数

转载 作者:行者123 更新时间:2023-12-01 13:21:21 24 4
gpt4 key购买 nike

通过使用 this建议我这样阅读输入:

10 11 12 13

但是当我想读取两行输入时:

10 11 12 13
9 8 78 3 12 11 2

代码:

#include <stdio.h>
#include <stdlib.h>

int main() {
int i=0, j=0, k=0, l=0;
int arr[10000];
int brr[10000];
char temp, temp2;
do {
scanf("%d%c", &arr[i], &temp);
i++;
} while(temp != '\n');

do {
scanf("%d%c", &brr[j], &temp2);
j++;
} while(temp2 != '\n');

for(k=0; k<i; k++) {
printf("%d ", arr[k]);
}

for(l=0; l<j; l++) {
printf("%d ", brr[l]);
}

return 0;
}

程序已卡住(example)。

来自 OP 评论的信息:

  • 第一行以换行结束
  • 第二行以EOF结尾

如何将两行整数读入两个不同的数组?

最佳答案

代码假定在第二个输入行之后有一个换行符。
如果没有……无穷大。

您可以扩展循环条件,使其在 scanf() 未能成功读取两个部分时也停止。

关于c - 读取两行整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49789232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com