zoukankan      html  css  js  c++  java
  • java经典小算法

    package com.shb.java;
    
    public class Demo4 {
    
    	/**时间有限 先不写文字了 自己随便敲的
    	 * @param args
    	 * @author shaobn
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    //		System.out.println(getDir(10));
    //		getMethod(new int[]{0,15,23,455,45,12});
    		getMethod_2(new int[]{1,45,848,66});
    	}
    	//上台阶问题
    	public static int getDir(int n){
    		if(n==1){
    			return 1;
    		}else if (n==2) {
    			return 2;
    		}else if (n>2) {
    			return getDir(n-1)+getDir(n-2);
    		}
    		return 1;
    			
    	}
    	//将数组每个都往前移动,最后一个为原来数组的第一个
    	public static void getMethod(int[] array){
    		int temp =0;
    		int cur = 0;
    		for(int i=0;i<array.length;i++){
    			if(i==0){
    				temp = array[i];
    				continue;
    			}
    			array[cur] = array[i];
    			cur++;
    			if(i==array.length-1){
    				array[i] = temp;
    			}
    			
    		}
    		for(int i:array){
    			System.out.println(i);
    		}
    		
    		
    	}
    	//把数组倒序输出
    	public static void getMethod_2(int[] array){
    		if(array.length%2==0){
    			int tmp = 0;
    			for(int i=0;i<array.length/2;i++){
    				tmp=array[i];
    				array[i] = array[array.length-i-1];
    				array[array.length-i-1]=tmp;
    			}
    			for(int i:array){
    				System.out.println(i);
    			}
    			
    		}else {
    			int tmp = 0;
    			for(int i=0;i<(array.length-1)/2;i++){
    				tmp = array[i];
    				array[i] = array[array.length-i-1];
    				array[array.length-i-1]=tmp;
    			}
    			for(int i:array){
    				System.out.println(i);
    			}
    		}
    	}
    	
    }
    
  • 相关阅读:
    洛谷P1084 [NOIP2012提高组Day2T3]疫情控制
    洛谷P1083 [NOIP2012提高组Day2T2]借教室
    洛谷P2736 “破锣摇滚”乐队 Raucous Rockers
    POJ1692 Crossed Matchings
    洛谷P1315 [NOIP2011提高组Day2T3] 观光公交
    阅读了几个别人写的轮播源码
    js遍历函数
    解决IE6的PNG透明
    04-树5 Root of AVL Tree
    平衡树实现
  • 原文地址:https://www.cnblogs.com/assassin666/p/5902393.html
Copyright © 2011-2022 走看看