算法相关

2017-09-08  本文已影响0人  李成新

1、给最外层的rootview,把这个根视图下的全部button背景设置成红色,手写代码,不许用递归

void changeAllBtnBGColor(View view, int color) {
if (view == null || !(view instanceof ViewGroup))
return; 
Stack m = new Stack<>();
while (view != null) { 
ViewGroup tmpGroup = (ViewGroup) view; 
int count = tmpGroup.getChildCount(); 
for (int i = 0; i < count; i++){ 
        View child = tmpGroup.getChildAt(i);
        if (child instanceof ViewGroup) m.add(child); 
        else if (child instanceof Button) { child.setBackgroundColor(color);} } 
        if (m.isEmpty()) break; 
        else view = m.pop();
    } 
}

2、.编写一个程序,输入n,求n!(用递归的方式实现)。

public static long fac(int n){
    if(n<=0) return 0;
    else if(n==1)    return 1;
    else return n*fac(n-1);
}
public static void main(String [] args) {
    System.out.println(fac(6));
}

3、编写一个程序,有1,2,3,4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

public static void main(String [] args) {
   int i, j, k;
   int m=0;
   for(i=1;i<=4;i++)
      for(j=1;j<=4;j++)
        for(k=1;k<=4;k++){
           if(i!=j&&k!=j&&i!=k){
             System.out.println(""+i+j+k);
             m++;
           }
        }
    System.out.println("能组成:"+m+"个");
}

4、编写一个程序,将text1.txt文件中的单词与text2.txt文件中的单词交替合并到text3.txt文件中。text1.txt文件中的单词用回车符分隔,text2.txt文件中用回车或空格进行分隔。

import java.io.File;  
import java.io.FileReader;  
import java.io.FileWriter;  
  
public class text{  
public static void main(String[] args) throws Exception{  
    String[] a = getArrayByFile("text1.txt",new char[]{'\n'});  
    String[] b = getArrayByFile("text2.txt",new char[]{'\n',' '});        
    FileWriter c = new FileWriter("text3.txt"); 
    int aIndex=0;
    int bIndex=0;         

    while(aIndex<a.length){  
        c.write(a[aIndex++] + "\n");    
        if(bIndex<b.length)  
            c.write(b[bIndex++] + "\n"); 
    }  
      
    while(bIndex<b.length){  
        c.write(b[bIndex++] +  "\n"); 
    }     
    c.close();  
}  

 public static String[] getArrayByFile(String filename,char[] seperators) throws Exception{  
    File f = new File(filename);  
    FileReader reader = new FileReader(f);  
    char[] buf = new char[(int)f.length()];  
    int len = reader.read(buf);  
    String results = new String(buf,0,len);  
    String regex = null;  
    if(seperators.length >1 ){  
        regex = "" + seperators[0] + "|" + seperators[1];  
    }else{  
        regex = "" + seperators[0];  
    }  
    return  results.split(regex);  
}  
  
}

5、顺序查找

public class Solution {

public static int SequenceSearch(int[] sz, int key) {
    for (int i = 0; i < sz.length; i++) {
        if (sz[i] == key) {
            return i;
        }
    }
    return -1;
}
}

6、折半查找

public class Solution {

public static int BinarySearch(int[] sz,int key){
    int low = 0;
    int high = sz.length - 1;
    
    while (low <= high) {
        int middle = (low + high) / 2;
        if(sz[middle] == key){
            return middle;
        }else if(sz[middle] > key){
            high = middle - 1;
        }else {
            low = middle + 1;
        }
    }
    return -1;
}
}

7、冒泡排序:

8、选择排序:

背景介绍: 选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理如下。首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。 ----来自 wikipedia

算法规则: 将待排序集合(0...n)看成两部分,在起始状态中,一部分为(k..n)的待排序unsorted集合,另一部分为(0...k)的已排序sorted集合,在待排序集合中挑选出最小元素并且记录下标i,若该下标不等于k,那么 unsorted[i] 与 sorted[k]交换 ,一直重复这个过程,直到unsorted集合中元素为空为止。

public void sort(int[] args) 
{
    int len = args.length;
    for (int i = 0,k = 0; i < len; i++,k = i) {
        // 在这一层循环中找最小
        for (int j = i + 1; j < len; j++) {
            // 如果后面的元素比前面的小,那么就交换下标,每一趟都会选择出来一个最小值的下标
            if (args[k] > args[j]) k = j;
        }

        if (i != k) {
            int tmp = args[i];
            args[i] = args[k];
            args[k] = tmp;
        }
    }
}

9、归并排序

背景介绍: 是创建在归并操作上的一种有效的排序算法,效率为O(n log n)。1945年由约翰·冯·诺伊曼首次提出。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用,且各层分治递归可以同时进行

算法规则: 像快速排序一样,由于归并排序也是分治算法,因此可使用分治思想:
1.申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列
2.设定两个指针,最初位置分别为两个已经排序序列的起始位置
3.比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置
4.重复步骤3直到某一指针到达序列尾
5.将另一序列剩下的所有元素直接复制到合并序列尾

      public void mergeSort(int[] ints, int[] merge, int start, int end) 
  {
    if (start >= end) return;
    
    int mid = (end + start) >> 1;
    
    mergeSort(ints, merge, start, mid);
    mergeSort(ints, merge, mid + 1, end);

    merge(ints, merge, start, end, mid);

  }
  
  private void merge(int[] a, int[] merge, int start, int end,int mid) 
  {
    int i = start;
    int j = mid+1;
    int pos = start;
    while( i <= mid || j <= end ){
        if( i > mid ){
            while( j <= end ) merge[pos++] = a[j++];
            break;
        }
        
        if( j > end ){
            while( i <= mid ) merge[pos++] = a[i++];
            break;
        }
        
        merge[pos++] = a[i] >= a[j] ? a[j++] : a[i++];
    }
    
    for (pos = start; pos <= end; pos++)
        a[pos] = merge[pos];
  
  }
上一篇下一篇

猜你喜欢

热点阅读