以前遇到过类似的情况——在未知数组长度的情况下输入数组各元素,解决问题的关键是如何判断数组何时输入结束,经验以为OJ会以EOF结束输入,但RE了,以'\n'结束输入AC
#include<stdio.h>
const int MAXN = 10100;
int p[MAXN];
int main(){
int k;
while(scanf("%d", &k) != EOF){
int i = 0;
char c;
while(scanf("%d%c", &p[i], &c)){
if(c == '\n')
break;
i++;
}
int temp;
for(int j = 0; j <= i - 1; j++){
if(j == 0){
temp = p[0];
printf("q(x): %d", temp);
}
else{
temp = p[j] + k * temp;
printf("%d", temp);
}
if(j != i - 1)
printf(" ");
else
printf("\n");
}
printf("r = %d\n", p[i] + k * temp);
printf("\n");
}
return 0;
}

本文讨论了在编程中遇到未知数组长度输入时如何有效处理,通过实例展示了使用EOF和'
'作为输入结束标志的方法,并实现了一个简单的数组操作程序。
774

被折叠的 条评论
为什么被折叠?



