任务中的错误答案 - 算法中的错误

时间:2018-05-15 18:55:40

标签: java algorithm

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

考虑连续排列的N个硬币。每枚硬币都显示头部或尾部。这些硬币的邻接是显示相同面的相邻coinst对的数量。返回可以通过倒转一个硬币获得的最大可能的邻接,其中一个coinst必须被反转

例如我有

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);
}

我犯了错误?

6 个答案:

答案 0 :(得分:1)

您可以通过拆分工作来实现这一目标:

  • 迭代数组,逐个更改硬币
  • 对于每次更改,计算您可以制作的对数(方法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},循环将使用6种不同的可能更改调用方法nbPair(),并计算您可以进行的对数:

[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

答案 1 :(得分:1)

请注意“其中一个硬币必须反向”

这意味着您必须翻转并且只能投一个硬币!

我们来分析一下

  1. 测试用例A = {1,1}或A = {0,0},
    然后结果= 1,因此在“反转一枚硬币”之后,结果应更改为0。

  2. 测试用例A = {1,1,1}或A = {0,0,0}(所有硬币都朝上或朝下),
    那么结果将是2,因此在“反转一枚硬币”之后,结果应更改为1(最大使用)。

  3. 等等。

因此如果(结果== n-1)返回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 :(得分:1)

我同意这里的答案之一,即您必须注意以下规则:“其中一个硬币必须取反” 这意味着您必须翻转一个硬币。

还有一条规则说不允许您添加或删除代码行,并且最多只能修改行代码,对吗?

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

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);
}

答案 3 :(得分:0)

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;
    }

答案 4 :(得分:0)

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

func insertArticle(a *Article) (err error) { tx, err := db.Begin() if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() res, err := tx.Exec("INSERT INTO content (content) VALUES (?)", a.Content) if err != nil { return err } contentId, err := res.LastInsertId() if err != nil { return err } res, err = tx.Exec("INSERT INTO article_meta (article_title, article_author, description) VALUES (?, ?, ?)", a.Title, a.Auth, a.Description) if err != nil { return err } articleId, err := res.LastInsertId() if err != nil { return err } _, err = tx.Exec("INSERT INTO articles (content_id, article_id) VALUES (?, ?)", contentId, articleId) if err != nil { return err } return tx.Commit() }

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

[1,1,1,1,1]

代码:

[1]

答案 5 :(得分:0)

在这个解决方案中,我以线性方式做到了

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);
    }
}