关于优化这个C程序的建议?
想知道是否有办法可以缩小这个程序。我不喜欢 5 个 printf/scanf 对的外观,但我想不出使用 for 循环来缩小它的方法。我无法将 scanf 调用与 printf 调用相匹配。
for(i = 1; i < 6; i++)
printf("Enter row %d", i);
这部分很简单,但是 scanf 调用和数组赋值让我感到困惑。
//Sums row and column
#include <stdio.h>
int main(void) {
int a[5][5];
int row[5] = {0}, col[5] = {0};
int i, j;
printf("\t--Enter a 5x5 chart--\n");
printf("\tEnter row #1: ");
scanf("%d %d %d %d %d", &a[0][0], &a[0][1], &a[0][2], &a[0][3], &a[0][4]);
printf("\tEnter row #2: ");
scanf("%d %d %d %d %d", &a[1][0], &a[1][1], &a[1][2], &a[1][3], &a[1][4]);
printf("\tEnter row #3: ");
scanf("%d %d %d %d %d", &a[2][0], &a[2][1], &a[2][2], &a[2][3], &a[2][4]);
printf("\tEnter row #4: ");
scanf("%d %d %d %d %d", &a[3][0], &a[3][1], &a[3][2], &a[3][3], &a[3][4]);
printf("\tEnter row #5: ");
scanf("%d %d %d %d %d", &a[4][0], &a[4][1], &a[4][2], &a[4][3], &a[4][4]);
for(i = 0; i < 5; i++) {
for(j = 0; j < 5; j++) {
row[j] += a[j][i];
col[j] += a[i][j];
}
}
printf("\tRow sums: %d %d %d %d %d\n", row[0], row[1], row[2], row[3], row[4]);
printf("\tColumn sums: %d %d %d %d %d\n", col[0], col[1], col[2], col[3], col[4]);
return 0;
}
wondering if there is a way I can shrink this program. I don't like the look of the 5 printf/scanf pairs, but I can't think of a way to use a for loop to shrink it. I have trouble with matching the scanf calls to the printf calls.
for(i = 1; i < 6; i++)
printf("Enter row %d", i);
this part is easy, however, the scanf calls and assignments into the array are baffling me.
//Sums row and column
#include <stdio.h>
int main(void) {
int a[5][5];
int row[5] = {0}, col[5] = {0};
int i, j;
printf("\t--Enter a 5x5 chart--\n");
printf("\tEnter row #1: ");
scanf("%d %d %d %d %d", &a[0][0], &a[0][1], &a[0][2], &a[0][3], &a[0][4]);
printf("\tEnter row #2: ");
scanf("%d %d %d %d %d", &a[1][0], &a[1][1], &a[1][2], &a[1][3], &a[1][4]);
printf("\tEnter row #3: ");
scanf("%d %d %d %d %d", &a[2][0], &a[2][1], &a[2][2], &a[2][3], &a[2][4]);
printf("\tEnter row #4: ");
scanf("%d %d %d %d %d", &a[3][0], &a[3][1], &a[3][2], &a[3][3], &a[3][4]);
printf("\tEnter row #5: ");
scanf("%d %d %d %d %d", &a[4][0], &a[4][1], &a[4][2], &a[4][3], &a[4][4]);
for(i = 0; i < 5; i++) {
for(j = 0; j < 5; j++) {
row[j] += a[j][i];
col[j] += a[i][j];
}
}
printf("\tRow sums: %d %d %d %d %d\n", row[0], row[1], row[2], row[3], row[4]);
printf("\tColumn sums: %d %d %d %d %d\n", col[0], col[1], col[2], col[3], col[4]);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
替换:
为:
Replace:
with:
下面的内容如何读取输入:
What about the following to read the input:
你可以这样做:
You can do something like this:
@Jim Balter 已经在对当前接受的答案的评论中提到了这一点,但它值得作为答案发布,因为它是一个更好、更可维护的解决方案。
请注意,scanf() 使用任何空格作为分隔符,因此不需要在单个 scanf() 调用中输入“行”。
@Jim Balter already mentioned this in his comment to the currently accepted answer, but it deserves being posted as an answer since it is a better and more maintanable solution.
Note that scanf() uses any whitespace as a delimiter so, the 'row' need not be entered in a single scanf() call.