【软帝学院】12道java经典入门算法题!

12java经典入门算法题!

【程序1  题目:将一个数组逆序输出。  

程序分析:用第一个与最后一个交换。  

其实,用循环控制变量更简单:

   for(int k=11;k>=1;k--)

  System.out.print(myarr[k]+",");

【程序2  题目:取一个整数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);

  }

  }

【程序3 

题目:打印出杨辉三角形(要求打印出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();

  }

 }

}

【程序4  题目:输入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]);

}

}

【程序5  题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。  

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] + " ");

}

}

}

【程序6  题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数  

import java.util.Scanner;

public class lianxi36 {

public static void main(String[] args) {

   int N =10;

   int[] a = new int[N];

   Scanner s = new Scanner(System.in);

   System.out.println("请输入10个整数:");

   for(int i=0; i<N; i++) {

    a[i] = s.nextInt();

   }

   System.out.print("你输入的数组为:");

   for(int i=0; i<N; i++) {

     System.out.print(a[i] + " ");

   }

   System.out.print("\n请输入向后移动的位数:");

   int m = s.nextInt();

   int[] b = new int[m];

   for(int i=0; i<m; i++) {

    b[i] = a[N-m+i];

   }

   for(int i=N-1; i>=m; i--) {

   a[i] = a[i-m];

   }

   for(int i=0; i<m; i++) {

    a[i] = b[i];

   }

System.out.print("位移后的数组是:");

   for(int i=0; i<N; i++) {

    System.out.print(a[i] + " ");

   }

}

}

【程序7 

题目:有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;//下标为TRUE时说明还在圈里

   }

   int leftCount = n;

   int countNum = 0;

   int index = 0;

   while(leftCount > 1) {

    if(arr[index] == true) {//当在圈里时

     countNum ++; //报数递加

     if(countNum == 3) {//报道3

      countNum =0;//从零开始继续报数

      arr[index] = false;//此人退出圈子

      leftCount --;//剩余人数减一

     }

    }

    index ++;//每报一次数,下标加一

    if(index == n) {//是循环数数,当下标大于n时,说明已经数了一圈,

     index = 0;//将下标设为零重新开始。

    }

   }

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

    if(arr[i] == true) {

     System.out.println(i);

    }

   }

     }

}

【程序8 

题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。  

import java.util.Scanner;

public class Ex38 {

public static void main(String [] args)

{

Scanner s = new Scanner(System.in);

System.out.println("请输入一个字符串");

String mys= s.next();

System.out.println(str_len(mys));

}

  public static int str_len(String x)

  {

  return x.length();

  }

}

题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n

【程序9 

题目:字符串排序。  

import java.util.*;   

public class test{

public   static   void   main(String[]   args)

{   

     ArrayList<String> list=new ArrayList<String>();   

     list.add("010101");   

     list.add("010003");   

    list.add("010201");   

    Collections.sort(list);   

  for(int   i=0;i<list.size();i++){   

  System.out.println(list.get(i));   

  }   

  }   

  }

【程序10  

题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?  

public class Dg {

static int ts=0;//桃子总数

int fs=1;//记录分的次数

static int hs=5;//猴子数...

int tsscope=5000;//桃子数的取值范围.太大容易溢出.

public int fT(int t){

if(t==tsscope){

//当桃子数到了最大的取值范围时取消递归

System.out.println("结束");

return 0;

}

else{

if((t-1)%hs==0 && fs <=hs){

if(fs==hs)

{

System.out.println("桃子数 = "+ts +" 时满足分桃条件");

}

   fs+=1;

   return fT((t-1)/5*4);// 返回猴子拿走一份后的剩下的总数

}

else

{

//没满足条件

fs=1;//分的次数重置为1

return fT(ts+=1);//桃子数加+1

}

}

}

public static void main(String[] args) {

new Dg().fT(0);

}

}

【程序11

java排序算法的比较

import java.util.*;

import java.io.*;

public class SortAlgorithm

{

static Random rand = new Random();

void bubbleSort(int[] numlist) // 冒泡排序算法

{

int temp;

for(int j=1;j<numlist.length;j++)

for(int i=0;i<numlist.length-j;i++)

if(numlist>numlist[i+1])

{

temp = numlist[i+1];

numlist[i+1] = numlist;

numlist = temp;

}

}

void selectionSort (int[] numlist) //选择排序算法

{

int temp;

for(int i=0;i<numlist.length-1;i++)

for(int j=i+1;j<numlist.length;j++)

if(numlist>numlist[j])

{

temp = numlist[j];

numlist[j] = numlist;

numlist = temp;

}

}

void insertSort (int[] numlist) //插入排序算法

{

int temp,in,out;

for(out=1;out<numlist.length;out++)

{

temp=numlist[out];

in=out;

while(in>0 && numlist[in-1]>=temp)

{

numlist[in]=numlist[in-1];

--in;

}

numlist[in]=temp;

}

}

void display (int[] num) // 打印出排序结果

{

for(int i = 0;i<num.length;i++)

System.out.print(num+" ");

System.out.println("");

}

static int pRand(int mod) // 生成随即数组

{

return Math.abs(rand.nextInt())%mod;

}

public static void main(String args[])throws IOException

{

SortAlgorithm sortAlgorithm = new SortAlgorithm();

int[] numList = new int[10];

for(int i = 0;i<numList.length;i++)

numList = pRand(100); //调用pRand方法,把随即生成的数据输入到

// 数组中

System.out.println("随即生成的数组是:");

// 打印出原数组,

for(int j =0;j<numList.length;j++)

System.out.print(numList[j]+" ");

System.out.println("");

long begin = System.currentTimeMillis(); //排序开始时间,调用系统的当前时间

sortAlgorithm.bubbleSort(numList); //执行冒泡排序

long end = System.currentTimeMillis(); //排序结束时间,调用系统当前时间

System.out.println("冒泡排序用时为:" + (end-begin)); //排序用时

System.out.println("排序后的数组为:");

sortAlgorithm.display(numList);

begin = System.currentTimeMillis();

sortAlgorithm.selectionSort(numList);

end = System.currentTimeMillis();

System.out.println("选择排序用时为:" +(end-begin));

System.out.println("排序后的数组为:");

sortAlgorithm.display(numList);

begin = System.currentTimeMillis();

sortAlgorithm.insertSort(numList);

end = System.currentTimeMillis();

System.out.println("插入排序用时为:" + (end-begin));

System.out.println("排序后的数组为:");

sortAlgorithm.display(numList);

}

}

【程序12

题目如下:用122345这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234412345等,要求:"4"不能在第三位,"3""5"不能相连。

static int[] bits = new int[] { 1, 2, 3, 4, 5 };

/**

* @param args

*/

public static void main(String[] args) {

sort("", bits);

}

private static void sort(String prefix, int[] a) {

if (a.length == 1) {

System.out.println(prefix + a[0]);

}

for (int i = 0; i < a.length; i++) {

sort(prefix + a, copy(a, i));

}

}

private static int[] copy(int[] a,int index){

int[] b = new int[a.length-1];

System.arraycopy(a, 0, b, 0, index);

System.arraycopy(a, index+1, b, index, a.length-index-1);

return b;

}

猜你喜欢

转载自www.cnblogs.com/heqingxiaohuo/p/12168451.html