【问题标题】:Wrong answer in task - mistake in algorithm任务中的错误答案 - 算法中的错误
【发布时间】:2018-05-15 18:55:40
【问题描述】:

我正在做作业(首先对不起英语)。

考虑 N 个硬币排成一排。每个硬币都显示正面或反面。这些硬币的邻接度是显示同一面的相邻硬币对的数量。返回通过反转一枚硬币可以获得的最大可能邻接度,其中一个硬币必须反转

例如我有

1 1 0 1 0 0 

在更改 third 后,我们得到 1 1 1 1 0 0,所以我们有 4 对。

但是我的代码例如不起作用

1 1 

我应该得到0,但得到1

  public static void main(String[] args) {
    int[] A = {1, 1};

    int n = A.length;
    int result = 0;
    for (int i = 0; i < n - 1; i++) {
        if (A[i] == A[i + 1])
            result = result + 1;
    }
    int r = 0;
    for (int i = 0; i < n; i++) {
        int count = 0;
        if (i > 0) {
            if (A[i - 1] != A[i])
                count = count + 1;
            else
                count = count - 1;
        }
        if (i < n - 1) {
            if (A[i + 1] != A[i])
                count = count + 1;
            else
                count = count - 1;
        }
        r = Math.max(r, count);
    }
    System.out.println(result + r);
}

我在哪里做错了?

【问题讨论】:

  • 你必须换一枚硬币吗?结果是什么,r 是什么?在您的代码中,您永远不会更改代码,并且 11 是一对所以返回 1 是可以的
  • 为什么 11 给出 0?有 10 个,你就有一个 1 连续。
  • azro 是的,这是强制性的,结果是对数。 juvian 因为 1 1 在反转一个之后你得到 1 0 这不是一对(对是 0, 0 或 1, 1)
  • 但你的代码永远不会改变硬币
  • 如果这是唯一错误的情况,当你的结果为 1 时返回 0,因为永远不可能有一对 1

标签: java algorithm


【解决方案1】:

请注意“其中一枚硬币必须反转”

这意味着你必须翻转并且只能掷一枚硬币!!!

让我们分析一下:

  1. 测试用例 A = {1,1} 或 A ={0,0},
    那么result =1,所以在“反转一个硬币”之后结果应该变成0。

  2. 测试用例 A = {1,1,1} 或 A ={0,0,0}(所有硬币都朝上或朝下),
    则结果为 2,因此在“反转 ONE 硬币”后,结果应更改为 1(取最大值)。

  3. 等等。

因此 if (result == n-1) return result-1; 必须放在你的程序中。 没有这样的声明,你的程序就有缺陷。

public static int solution(int[] A) 
{
            int n = A.length;
            int result = 0; 

            for (int i = 0; i < n - 1; ) 
            {
                if (A[i] == A[i + 1]) 
                    result = result + 1;
                i = i+1;
            }      
            if (result == n-1)   return result-1;   // to cover {1,1}, {1,1,1}

            int max = 0;
            for (int i = 0; i <n; i++) 
            {
                int count = 0;
                if (i > 0)   // starting up from 1 and  covering the last
                {
                    if (A[i-1] != A[i])
                        count = count + 1;    
                    else
                       count = count - 1;
                }
                if (i < n - 1) 
                {
                    if (A[i] != A[i + 1])   // starting with 0
                        count = count + 1;
                    else
                        count = count - 1;
                }               
                max = Math.max(max,count);              
            }     
            return result + max; // 
        }

【讨论】:

    【解决方案2】:

    我同意这里的一个答案,说你必须注意规则:“其中一个硬币必须反转” 这意味着您必须掷出一枚硬币。

    而且还有一条规定是不允许添加或删除代码行,最多只能修改行代码,对吧?

    问题是硬币最初都在同一张面上。假设我们有这个测试用例:

    0 0 0 0 0 0
    

    初始状态是 5 对,但是在恰好抛一枚硬币后,可能的最大对数应该是 4(而原始代码返回 5 )。

    因此,我对这个问题的决定是将变量max 的初始值更改为-1。

    这是生成的代码,我们只需要修改一行代码:

    public static void main(String[] args) {
        int[] A = {1, 1};
    
        int n = A.length;
        int result = 0;
        for (int i = 0; i < n - 1; i++) {
            if (A[i] == A[i + 1])
                result = result + 1;
        }
        int r = -1;
        for (int i = 0; i < n; i++) {
            int count = 0;
            if (i > 0) {
                if (A[i - 1] != A[i])
                    count = count + 1;
                else
                    count = count - 1;
            }
            if (i < n - 1) {
                if (A[i + 1] != A[i])
                    count = count + 1;
                else
                    count = count - 1;
            }
            r = Math.max(r, count);
        }
        System.out.println(result + r);
    }
    

    【讨论】:

    • for (int i = 0; i
    【解决方案3】:

    您可以通过拆分工作来实现:

    • 遍历数组,一个一个地改变一个硬币
    • 对于每个更改,计算您可以制作多少对(方法 nbPair

    public static void main(String[] args) {
        int[] A = {1, 1, 0, 1, 0, 0};
    
        int nbPairMax = 0;
        for (int i = 0; i < A.length; i++) {
            int[] copy = Arrays.copyOf(A, A.length);
            copy[i] = (copy[i] + 1) % 2; // 0->1, 1->0
            nbPairMax = Math.max(nbPairMax, nbPair(copy));
        }
        System.out.println(nbPairMax);
    
    }
    
    private static int nbPair(int[] array) {
        int result = 0;
        for (int i = 0; i < array.length - 1; i++) {
            if (array[i] == array[i + 1]) {
                result = result + 1;
            }
        }
        return result;
    }
    

    例如,使用{1, 1, 0, 1, 0, 0},循环将调用方法nbPair() 并进行6 种不同的可能更改,并计算您可以进行的配对数:

    [0, 1, 0, 1, 0, 0] >> 1
    [1, 0, 0, 1, 0, 0] >> 2
    [1, 1, 1, 1, 0, 0] >> 4
    [1, 1, 0, 0, 0, 0] >> 4
    [1, 1, 0, 1, 1, 0] >> 2
    [1, 1, 0, 1, 0, 1] >> 1
    

    【讨论】:

      【解决方案4】:
      public static int coinReversal(int a[]){
              int cost1 = 1, cost2 = 0;
              int b[] = a.clone();
              b[0] = 0;
              if(b[0] == 0){
                  for(int i = 0 ; i < b.length-1 ; i++ ){
                      if(b[i] == b[i+1] ){
                          cost1+=1;
                          if(b[i+1] == 1){
                              b[i+1] = 0;
                          }else{
                              b[i+1] = 1;
                          }
                      }
                  }
              }
              if(a[0] == 1){
                  for(int i = 0 ; i < a.length-1 ; i++ ){
                      if(a[i] == a[i+1] ){
                          cost2+=1;
                          if(a[i+1] == 1){
                              a[i+1] = 0;
                          }else{
                              a[i+1] = 1;
                          }
                      }
                  }
              }
      
              return  cost2 > cost1 ? cost1 : cost2;
          }
      

      【讨论】:

        【解决方案5】:

        我发现了 2 个错误; 如果数组中的所有元素都相同,则第一个相关。

        [1,1,1,1,1]

        如果我们的数组只有一个元素,则为第二个。

        [1]

        代码:

        public static int solution(int[] A) 
        {
                    int n = A.length;
                    int result = 0; 
        
                    for (int i = 0; i < n - 1; ) 
                    {
                        if (A[i] == A[i + 1]) 
                            result = result + 1;
                        i = i+1;
                    }
                    // Add these 2 lines below
        
                    if (A.length == 1 )  return 0; 
                    if (result == n-1)   return result-1;
        
                    int max = 0;
                    for (int i = 0; i <n; i++) 
                    {
                        int count = 0;
                        if (i > 0)   
                        {
                            if (A[i-1] != A[i])
                                count = count + 1;    
                            else
                               count = count - 1;
                        }
                        if (i < n - 1) 
                        {
                            if (A[i] != A[i + 1])   // starting with 0
                                count = count + 1;
                            else
                                count = count - 1;
                        }               
                        max = Math.max(max,count);              
                    }     
                    return result + max; // 
                }
        
        

        【讨论】:

          【解决方案6】:

          在这个解决方案中,我以线性方式进行

          public static void main(String[] args) {
              int[] A = { 1, 1, 0, 1, 0, 0};
          
              int sum1 = 0, sum2 = 0;
              for (int i = 0; i < A.length; i++) {
                  if (i % 2 === 0) {
                      if (0 !== A[i]) {
                          sum1++;
                      }
                      if (1 !== A[i]) {
                          sum2++;
                      }
                  } else {
                      if (1 !== A[i]) {
                          sum1++;
                      }
                      if (0 !== A[i]) {
                          sum2++;
                      }
                  }
              }
              if (sum1 > sum2) {
                  System.out.println(sum2);
              }
              else {
                  System.out.println(sum1);
              }
          }
          
          

          【讨论】:

            【解决方案7】:

            我解决了这个硬币反转的问题,以便在 python 中获得替代的正面和反面。 这是正确通过的测试用例的问题解决方案。

            def coins_reversal(A):
            """
            Reverse the coins to get alternative heads and tails.
            
            :param list A: list of heads and tails of coins
            :return: number of coins to reverse to get alternative heads and tails
            """
               result = 0
               NEW_A = A.copy()
               for i in range(0, len(A) - 1):
                   if NEW_A[i] == 0 and NEW_A[i+1] == 0:
                       result += 1
                       if NEW_A[i - 1] == 0:
                           NEW_A[i] = 1
                       else:
                           NEW_A[i + 1] = 1
                   elif NEW_A[i] == 1 and NEW_A[i+1] == 1:
                       result += 1
                       if NEW_A[i - 1] == 1:
                           NEW_A[i] = 0
                       else:
                           NEW_A[i + 1] = 0
               return result
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-02-19
              • 2020-02-12
              • 1970-01-01
              • 2019-02-22
              • 1970-01-01
              相关资源
              最近更新 更多