zoukankan      html  css  js  c++  java
  • 面试题-JAVA算法题

    1.编写一个程序,输入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));
        }
    

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

     1 public static void main(String [] args) {
     2        int i, j, k;
     3        int m=0;
     4        for(i=1;i<=4;i++)
     5           for(j=1;j<=4;j++)
     6             for(k=1;k<=4;k++){
     7                if(i!=j&&k!=j&&i!=k){
     8                  System.out.println(""+i+j+k);
     9                  m++;
    10                }
    11             }
    12         System.out.println("能组成:"+m+"个");
    13 }

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

     1 import java.io.File;  
     2 import java.io.FileReader;  
     3 import java.io.FileWriter;  
     4   
     5 public class text{  
     6     public static void main(String[] args) throws Exception{  
     7         String[] a = getArrayByFile("text1.txt",new char[]{'
    '});  
     8         String[] b = getArrayByFile("text2.txt",new char[]{'
    ',' '});        
     9         FileWriter c = new FileWriter("text3.txt"); 
    10         int aIndex=0;
    11         int bIndex=0;         
    12 
    13         while(aIndex<a.length){  
    14             c.write(a[aIndex++] + "
    ");    
    15             if(bIndex<b.length)  
    16                 c.write(b[bIndex++] + "
    "); 
    17         }  
    18           
    19         while(bIndex<b.length){  
    20             c.write(b[bIndex++] +  "
    "); 
    21         }     
    22         c.close();  
    23     }  
    24 
    25      public static String[] getArrayByFile(String filename,char[] seperators) throws Exception{  
    26         File f = new File(filename);  
    27         FileReader reader = new FileReader(f);  
    28         char[] buf = new char[(int)f.length()];  
    29         int len = reader.read(buf);  
    30         String results = new String(buf,0,len);  
    31         String regex = null;  
    32         if(seperators.length >1 ){  
    33             regex = "" + seperators[0] + "|" + seperators[1];  
    34         }else{  
    35             regex = "" + seperators[0];  
    36         }  
    37         return  results.split(regex);  
    38     }  
    39       
    40 }

    4.给定String,求此字符串的单词数量。字符串不包括标点,大写字母。例如 String str="hello world hello hi";单词数量为3,分别是:hello world hi。

     1 public static void main(String [] args) {
     2             int count = 0;
     3             String str="hello world hello hi";
     4             String newStr="";
     5             HashMap<String,String> m=new HashMap<String,String>();
     6             String [] a=str.split(" ");
     7             for (int i=0;i<a.length;i++){
     8                 if(!m.containsKey(a[i])){
     9                     m.put(a[i],"1");
    10                     count++;
    11                     newStr=newStr+" "+a[i];
    12                 }
    13             }
    14             System.out.println("这段短文单词的个数是:"+count+","+newStr);
    15     }
  • 相关阅读:
    【UVA–11997 K Smallest Sums 】
    【LA 3027 Corporative Network】
    【bzoj3173-最长上升子序列-一题两解】
    【Rain in ACStar HDU-3340】
    【Uva 11280 飞到弗雷德里顿】
    【Uva 10269 马里奥与公主的归途】
    【Uva 11604 编码都有歧义了】
    【RevolC FaeLoN Uva 10972】
    oracle解析xml(增加对9i版本的支持)
    acl操作记录
  • 原文地址:https://www.cnblogs.com/fenghh/p/9605302.html
Copyright © 2011-2022 走看看