成都计算机培训 网络报名 Java软件开发培训课程 朗沃成都软件Java培训中心师资介绍 成都软件培训中心开班信息 朗沃成都Java软件开发培训中心学员就业情况 Java朗沃成都软件培训中心在线咨询
成都朗沃教育课程升级啦!
□ 您现在的位置:首页> 学员天地> java学习> 正文

java经典算法40题(四)

朗沃成都软件培训学校在线咨询         朗沃成都软件培训学校在线报名

关键词:java学习

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

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

【程序32   题目:取一个整数a从右端开始的47位。  
程序分析:可以这样考虑:
  
(1)
先使a右移4位。
  
(2)
设置一个低4位全为1,其余全为0的数。可用
~(~0 < <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);
  }
  }
【程序33
  
题目:打印出杨辉三角形(要求打印出10行如下图)
  
1.
程序分析:
  
1  
1   1  
1   2   1  
1   3   3   1  
1   4   6   4   1  
1   5   10   10   5   1  
public class Ex33 {
 public static void main(String args[]){
     int i,j;
     int a[][];
     a=new int[8][8];
    for(i=0;i<8;i++){
       a[i][i]=1;
       a[i][0]=1;
      }
    for(i=2;i<8;i++){
     for(j=1;j<=i-1;j++){
    a[i][j]=a[i-1][j-1]+a[i-1][j];
     }
    } 
    for(i=0;i<8;i++){
    for(j=0;j<i;j++){ 
     System.out.printf("  "+a[i][j]);
     }
    System.out.println();
    }
   }
}

【程序34   题目:输入3个数a,b,c,按大小顺序输出。  
1.
程序分析:利用指针方法。
  
public class Ex34 {
 public static void main(String[] args)
 {
 int []arrays = {800,56,500};
 for(int i=arrays.length;--i>=0;)
 {
 for(int j=0;j<i;j++)
 {
 if(arrays[j]>arrays[j+1])
 {
 int temp=arrays[j];
 arrays[j]=arrays[j+1];
 arrays[j+1]=temp;
 }
 }
 }
 for(int n=0;n<arrays.length;n++)
 System.out.println(arrays[n]);
 }

}
【程序35   题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
  
import java.util.*;
public class Ex35 {
public static void main(String[] args) {
int i, min, max, n, temp1, temp2;
int a[];
System.out.println("
输入数组的长度
:");
Scanner keyboard = new Scanner(System.in);
n = keyboard.nextInt();
a = new int[n];
for (i = 0; i < n; i++) {
System.out.print("
输入第" + (i + 1) + "个数据
");
a[i] = keyboard.nextInt();
}
//
以上是输入整个数组

max = 0;
min = 0;
//
设置两个标志,开始都指向第一个数
for (i = 1; i < n; i++) {
if (a[i] > a[max])
max = i; //
遍历数组,如果大于a[max],就把他的数组下标赋给max
if (a[i] < a[min])
min = i; //
同上,如果小于a[min],就把他的数组下标赋给
min
}
//
以上for循环找到最大值和最小值,max是最大值的下标,min是最小值的下标

temp1 = a[0];
temp2 = a[min]; //
这两个temp只是为了在交换时使用

a[0] = a[max];
a[max] = temp1; //
首先交换a[0]和最大值a[max]

if (min != 0) { //如果最小值不是a[0],执行下面
a[min] = a[n - 1];
a[n - 1] = temp2; //
交换a[min]a[n-1]
} else {       //
如果最小值是a[0],执行下面

a[max] = a[n - 1];
a[n - 1] = temp1;
}

for (i = 0; i < n; i++) { //输出数组
System.out.print(a[i] + " ");
}
}
}

【程序37  
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从13报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
  
import java.util.Scanner;
public class Ex37 {
 public static void main(String[] args) {
     Scanner s = new Scanner(System.in);
     int n = s.nextInt();
     boolean[] arr = new boolean[n];
     for(int i=0; i<arr.length; i++) {
      arr[i] = true;//

|<< << < 1 2 > >> >>|
朗沃成都软件培训学校在线咨询         朗沃成都软件培训学校在线报名
作者: 朗沃IT教育 成都中心
原载:朗沃IT教育 成都中心 lovoinfo.com
版权所有,转载时必须以链接形式注明作者和原始出处及本声明
在线客服
在线客服系统