陈子豪啊 2017-11-29 03:36 采纳率: 0%
浏览 666

如何理解这段程序代码段呢

public class text

{

static int a[] = {1,2,3,4,5,6,7,8,9};

static int n = a.length;

static double all = 10;

static int count = 0;

public static void main(String[] args) {

    perm(0);

    System.out.println(count);

}



public static void perm(int offset){



    if(offset == n-1){

        compare();

        return;

    }

    for(int i= offset; i< n; i++){

        swap(i, offset);

        perm(offset+1);

        swap(i,offset);

    }



}



public static void swap(int i, int offset){

    int temp;



    temp =a[offset];

    a[offset] =a[i];

    a[i] = temp;



}

public static void compare(){

     double number = a[0] + a[1]*1.0/a[2] + (a[3]*100+a[4]*10+a[5])*1.0/(a[6]*100+a[7]*10+a[8]);

     if(Math.abs(number - all) < 0.000001 ){

         count++;

     }



}

}

  • 写回答

2条回答 默认 最新

  • 关注

    通读下来的最后输出是count,这是个计数程序。
    1.程序进入perm,在这个里面进行for循环,此时offset=0
    2.进入swap,将a[i]与a[offset]交换,此时执行递归perm(offset+1)
    3.递归n次循环m次之后,得到了count结果

    不知道是做什么的程序

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算