查看: 1598| 回复: 4
跳转到指定楼层
上一主题 下一主题
收起左侧

[入门|算法|数据结构] 求检查下,compile正常但是输出不了是什么情况,一直显示运行

全局:
公开课
学校名称: Princeton
Unit号: 1
开课时间: 2015-01-07
课程全名: Algorithm
平台: Coursera

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x





public class Percolation{
    private boolean[] status ;
    private int size,top,bottom;
    private WeightedQuickUnionUF uf;

    public Percolation(int N){
         if (N<=0){
        throw new IllegalArgumentException();
    }
        uf=new WeightedQuickUnionUF(N*N);
        status=new boolean[N*N];
        size=N;
        for( int i=0;i<N*N;i++){
            status[i]=false;
            for( i=0;i<N;i++){
                uf.union(i,top);
                uf.union(N*(N-1)+i,bottom);
            }
        }
    }
        private int getIndex(final int i, final int j){
            if(i<1||i>size||j<1||j>size){
                throw new IndexOutOfBoundsException();
            }
            final int index=size*(i-1)+(j-1);
            return index;
        }

        public void open (int i,int j){
            if(i<1||i>size||j<1||j>size){
                throw new IndexOutOfBoundsException();
            }
            final int index=getIndex(i,j);
            if (!status[index]){
                status[index]=true;
                if(i-2>=0){
                    int index1=size*(i-2)+(j-1);
                    if(status[index1]){
                        uf.union(index,index1);
                    }
                }
                 if(i+1<=size){
                    int index2=size*(i)+(j-1);
                    if(status[index2]){
                        uf.union(index,index2);
                    }
                }
                  if(j-2>=0){
                    int index3=size*(i-1)+(j-2);
                    if(status[index3]){
                        uf.union(index,index3);
                    }
                }
                   if(j+1<=size){
                    int index4=size*(i-1)+(j);
                    if(status[index4]){
                        uf.union(index,index4);
                    }
                }
            }
        }

        public boolean isOpen(int i, int j){
            return status[getIndex(i,j)];
        }
        public boolean isFull(int i, int j){
            if(i<1||i>size||j<1||j>size){
                throw new IndexOutOfBoundsException();
            }
            if(uf.connected(getIndex(i,j),top)&&isOpen(i,j))
            {
                return true;
            }
            return false;
        }



        public boolean percolates() {
            if (uf.connected(top,bottom)) {
                return true;
            }
                return false;
        }

}        




















public class PercolationStats {
    private final double[] ArrResult;
    private final int num;
    public PercolationStats(int N, int T)    // perform T independent experiments on an N-by-N grid
    { ArrResult = new double[T];
        num=T;
        for(int k=0;k<num;k++){
            final Percolation p =new Percolation(N);
            int count =0;
            double prob=0;
            while(!p.percolates()){
                int i=StdRandom.uniform(1,N);
                int j=StdRandom.uniform(1,N);
                if(!p.isOpen(i,j)){
                    p.open(i,j);
                    count++;
                }
            }
            prob=(double)count/(N*N);
            StdOut.println(prob);
            ArrResult[k]=prob;
        }
    }   
        public double mean()                      // sample mean of percolation threshold
        {
            return StdStats.mean(ArrResult);
        }

        public double stddev()                    // sample standard deviation of percolation threshold
            {
                return StdStats.stddev(ArrResult);
            }


         public double confidenceLo()              // low  endpoint of 95% confidence interval
                {
                    return mean()-((1.96*stddev())/Math.sqrt(num));
                }

          public double confidenceHi()              // high endpoint of 95% confidence interval
                    {
                         return mean()+((1.96*stddev())/Math.sqrt(num));
                    }
          public static void main(String[] args)    // test client (described below)
          {
                  final PercolationStats ps=new PercolationStats(Integer.parseInt(args[0]),
                    Integer.parseInt(args[1]));
              StdOut.printf("mean  =%s\n",ps.mean());
               StdOut.printf("stddev  =%s\n",ps.stddev());
                StdOut.printf("conf interval  =%s,%s\n",ps.confidenceLo(),ps.confidenceHi());
          }





}


上一篇:Web Application Architectures 2015 - Module4
下一篇:[Coursera] Web Application Architectures 2015 - Module5
🔗
一剑终情 2015-4-2 23:42:48 | 只看该作者
全局:
本帖最后由 一剑终情 于 2015-4-2 09:45 编辑

扫了一下,两层for循环的变量都是i,不知道是不是这个问题

敢不敢多写几个print自己debug下
回复

使用道具 举报

🔗
 楼主| lsd6811298 2015-4-9 15:38:21 | 只看该作者
全局:
一剑终情 发表于 2015-4-2 23:42
扫了一下,两层for循环的变量都是i,不知道是不是这个问题

敢不敢多写几个print自己debug下

谢谢!刚开始学。。有没有什么一些需要知道的应该从一开始就该养成的好习惯?
回复

使用道具 举报

🔗
wwt 2015-4-9 16:17:08 | 只看该作者
全局:
lsd6811298 发表于 2015-4-9 15:38
谢谢!刚开始学。。有没有什么一些需要知道的应该从一开始就该养成的好习惯?

学习如何高效快速的debug
回复

使用道具 举报

🔗
 楼主| lsd6811298 2015-4-9 17:45:48 | 只看该作者
全局:
wwt 发表于 2015-4-9 16:17
学习如何高效快速的debug

对啊,写的时候吭哧吭哧写完了,一编译运行就是不成,找bug找半天
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表