陈子豪啊 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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题