头条编程题 串珠 错在哪里 求大神

我的思路是 将每种颜色的珠子的位置几率下来 然后根据 距离来算

比如

5 2 3
3 1 2 3
0
2 2 3
1 2
1 3
第一种颜色true false false false false
第二种颜色true false true true false
第三种颜色true false true false true

Scanner scanner =new Scanner(System.in);
        int count =0;
        int n= scanner.nextInt();
        int m =scanner.nextInt();
        int c=scanner.nextInt();
        boolean yanse[][]=new boolean [51][10001];
        int i,j;
        for ( i =1 ;i<=n;i++){
            int num_i =scanner.nextInt();
            if(num_i==0){
                continue;
            }
             for (j=1 ;j<=num_i;j++){
                 int se= scanner.nextInt();
                 yanse[se][i]=true;
             }


        }
        for(int i1 =1 ;i1<= c;i1++){
            for (int j1=1 ;j1<=n;j1++){
                System.out.print(yanse[i1][j1]+"  ");
            }
            System.out.print("\n");
        }

        int sum =0;

        int last =0;
        for (int i1 =1 ;i1<=c;i1++){
            for (int j1=1;j1<n;j1++){

                for(int l1=j1+1;l1<=n;l1++){
                    if(yanse[i1][j1]&&yanse[i1][l1]){
                        sum=l1-j1;
                        if (sum==n-1)
                        {
                            sum=1;
                        }
                        if(sum<=m-1){
                            count++;
                        }
                    }

                }
//                if(yanse[i1][1]&&yanse[i1][n]){
//                    count=count+1;
//                }
            }
        }



        System.out.println(count);


    }
全部评论
 for(int l1=j1+1;l1<=n;l1++){                     if(yanse[i1][j1]&&yanse[i1][l1]){                         sum=l1-j1;                         if (sum==n-1)                         {                             sum=1;                         }                         if(sum<=m-1){                             count++;                         }                     } 这里错了,没看太懂你的 sum=1 是干啥的。 循环数组。对于 i<j, 你是不是应该  i+n 和 j 来比比看啊,尤其头尾各有一些,加起来是 m 长度的时候,你貌似把这些情况都遗漏了。。。。。。。
点赞 回复 分享
发布于 2017-09-10 21:30
大哥 你和我写的一样貌似,我的也过不了。就想问哪里错了
点赞 回复 分享
发布于 2017-09-10 21:22
想法一样,也是错了,不知道为什么。。。 不过你最后sum<=m-1之后是不是应该跳出循环?
点赞 回复 分享
发布于 2017-09-10 21:20
兄弟,你这代码谁看的懂啊?给你个100%AC的代码吧,我的逻辑可能有点绕,你先看看吧。 import java.util.Scanner; public class Main{ public static void main(String ...args){ Scanner in = new Scanner(System.in); String[] line = in.nextLine().split(" "); int n = Integer.parseInt(line[0]); int m = Integer.parseInt(line[1]); int c = Integer.parseInt(line[2]); boolean[][] table = new boolean[n][c]; for (int i = 0; i < n; i++) { line = in.nextLine().split(" "); int k = Integer.parseInt(line[0]); for (int j = 1; j <= k; j++) { table[i][Integer.parseInt(line[j])-1] = true; } } int result = 0; int len = 0; for (int j = 0; j < c; j++) { len = 0; boolean isContinue = false; for(int i = 0; i < n; i++){ if(len >= m){ len = 0; isContinue = false; } if(isContinue){ if(table[i][j]){ result++; isContinue = false; break; }else{ len++; } }else{ if(table[i][j]){ isContinue = true; len++; } } } //判断环型数组末尾 if(isContinue){ for(int i = 0; i < n; i++){ if(len >= m){ break; } if(table[i][j]){ result++; break; }else{ len++; } } } } System.out.println(result); } }
点赞 回复 分享
发布于 2017-09-10 21:16

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务