【问题标题】:Printf does not work unless another printf is present除非存在另一个 printf,否则 Printf 不起作用
【发布时间】:2016-04-14 11:28:29
【问题描述】:

我试图在 HackerRank 上解决 C 中的一个挑战。有一个 n 行 n 列的方阵。挑战是将矩阵对角线的两个和之间的绝对差打印为单个整数。

这是链接: https://www.hackerrank.com/challenges/diagonal-difference

我能够得到正确的答案,但是代码末尾的 printf() 语句不起作用,除非我在 for 循环语句之前添加了不必要的 printf("")。 有人可以解释为什么会这样吗?

提前致谢

代码如下:

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main()
{
    int n; 
    scanf("%d",&n);
    int a[n][n],sum_d1=0,sum_d2=0,a_i,a_j;
    for( a_i = 1; a_i <= n; a_i++)
    {
       for(a_j = 1; a_j <= n; a_j++)
       {
           scanf("%d",&a[a_i][a_j]);
           if(a_j==a_i)
               sum_d1=sum_d1+a[a_i][a_j];
       }
    }
    printf("");// the last printf works only when this statement is present
    for(a_i=1;a_i<=n;a_i++)
    {
        for(a_j=n;a_j>0;a_j--)
        {
            if((a_i+a_j)==(n+1))
                sum_d2=sum_d2+a[a_i][a_j];
        }
    }
    printf("%d",abs(sum_d1-sum_d2)); //this doesn't work if there is no printf above
    return 0;
}

【问题讨论】:

  • 您有未定义的行为,因为您在位置 [n] 处索引您的数组。
  • 您还有一个缺少换行的错误。将\n 添加到格式字符串的末尾以修复它。这样,该行将正确结束,而不是在程序退出时悬空。
  • @TomKarzes 为什么在那之前的空字符串 printf 会解决这个问题?
  • 您的主要错误是您将数组视为从 1..n 索引。他们不是。有效索引从 0..(n-1) 开始。今天很少有语言使用从一为基础的索引。
  • 由于您在数组边界之外访问,因此您正在破坏一些内存。额外的printf 调用导致第二个printf 工作只是随机的机会。修复你的错误。

标签: c


【解决方案1】:

结尾的换行符是这里的关键。标准输出仅在遇到出于性能原因的行结束时才会刷新。如果您更改为 fprintf(stderr, ...) 那么您的代码将按原样工作。您也可以在 printf 之后使用 fflush(stdout)。

【讨论】:

    【解决方案2】:

    正如其他几个人所提到的,您的主要问题是您的数组声明/使用。 C 中的数组的第一个索引从 0 开始,而不是从 1 开始。因此,当您尝试将内容存储在数组中或再次访问它们时,它超出了数组的边界,并且可能会以意想不到的方式进行。

    数组示例:数组[2][3]

        0  1 
    0  a|  b | 
    1  c|  d | 
    2  e|  f |
    
    Array[0][0] == a 
    Array[1][1] == d 
    Array[1][2] == f
    

    在您的代码中,如果有人输入“1”,则会创建一个大小为 [1][1] 的数组 - 单个元素。这应该使用 a[0][0] 访问,但在代码中是由 a[1][1] 访问的,这是一个无效元素,因为它不是数组的一部分。

    如果不确切知道您的代码打算做什么,我无法完全修复它,但一个好的开始如下:

    #include <math.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <limits.h>
    #include <stdbool.h>
    
    int main(){
        int n; 
        scanf("%d",&n);
        int a[n][n],sum_d1=0,sum_d2=0,a_i,a_j;
        for( a_i = 0; a_i < n; a_i++){
           for(a_j = 0; a_j < n; a_j++){
               scanf("%d",&a[a_i][a_j]);
               if(a_j==a_i)
                   sum_d1=sum_d1+a[a_i][a_j];
           }
        }
        //printf("");// the last printf works only when this statement is present
        for(a_i=0;a_i<n;a_i++)
        {
            for(a_j=n-1;a_j>=0;a_j--)
            {
                if((a_i+a_j)==(n+1))
                    sum_d2=sum_d2+a[a_i][a_j];
            }
        }
        printf("%d",abs(sum_d1-sum_d2)); //this doesn't work if there is no printf above
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-07
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多