---
title: "JAVA经典算法40题(16)"
author: "方XiRui"
date: "2012-12-17T11:29:32Z"
tags: ["IT", "Java", "算法"]
categories: ["编程语言"]
slug: "java-40-16"
draft: false
lastmod: "2012-12-17T11:29:32Z"
id: 1748913
url: "https://fxrde.iteye.com/blog/1748913"
---

【程序31】题目：将一个数组逆序输出。  
程序分析：用第一个与最后一个交换。  
其实，用循环控制变量更简单：

```
for(int k=11;k>=1;k--)
    System.out.print(myarr[k]+",");
```

 【程序32】题目：取一个整数a从右端开始的4～7位。  
程序分析：可以这样考虑：  
(1)先使a右移4位。  
(2)设置一个低4位全为1,其余全为0的数。可用~(~0 &lt; &lt;4)  
(3)将上面二者进行&运算。

```
public class Ex32 {
    public static void main(String[] args){
        int a=0;
        long b=18745678;
        a=(int) Math.floor(b % Math.pow(10,7)/Math.pow(10, 3));
        System.out.println(a);
    }
}
```