1. Write a program, enter N, ask for n! (implemented in a recursive way).
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. Write a program that has 1,2,3,4 numbers, and how many three digits of a distinct and no repetition number can be formed? How much are they?
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 ("Can Compose:" +m+ ");}
3. Write a program that alternately merges the words in the Text1.txt file with the words in the Text2.txt file into the Text3.txt file. The words in the Text1.txt file are separated by a carriage return, separated by a carriage return or a space in the Text2.txt file.
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 ("Tex T1.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 F Ile (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); } }
4.639172 the number on each digit is different, and the sum of all digits of the number after the square does not appear the number that makes up its own. (639172*639172=408540845584), a few 6-digit numbers like 639172? What's the difference?
The HASHMAP structure used in this question can be judged with or without repetition, or by an array of the following questions.
public void Selectnum () {for (long n = 100000; n <= 999999;n++) {if (Isselfrepeat (n)) Have the same number, then skip continue; else if (Ispingfangrepeat (n*n,n)) {///the number of squares in which there is a number equal to that number continue; } else{//Meet the criteria, then print System.out.println (n); }}} public boolean isselfrepeat (long N) {hashmap<long,string> m=new Hashmap<long,strin G> (); When storing, determine whether there is a duplicate value while (n!=0) {if (M.containskey (n%10)) {return true; } else{M.put (n%10, "1"); } N=N/10; } return false; } public boolean ispingfangrepeat (long Pingfang,long N) {hashmap<long,string> m=new hashmap<long,string > (); while (n!=0) {m.put (n%10, "1"); N=N/10; } while (pingfang!=0) {iF (M.containskey (pingfang%10)) {return true; } PINGFANG=PINGFANG/10; } return false; } public static void Main (String args[]) {new test (). Selectnum (); }
5. For example, 968548+968545=321732732 its answer does not have the first two numbers in the number, how many such 6 digits.
public void Selectnum () {for (int n = ten; n <= 99;n++) {for (int m = ten; M <= 99;m++) { if (isrepeat (n,m)) { c5/>continue; } else{ System.out.println ("combination is" +n+ "," +m ") ; }}} public boolean isrepeat (int n,int m) { int[] a={0,0,0,0,0,0,0,0,0,0}; int s=n+m; while (n!=0) { a[n%10]=1; N=N/10; } while (m!=0) { a[m%10]=1; M=M/10; } while (s!=0) { if (a[s%10]==1) { return true; } S=S/10; } return false; } public static void Main (String args[]) { new Test (). Selectnum ();
6. Given a string, find the number of words in this string. The string does not include punctuation, uppercase letters. For example String str= "Hello World Hello hi"; the number of words is 3, respectively: Hello World hi.
public static void Main (String [] args) { int count = 0; String str= "Hello World Hello hi"; String newstr= ""; Hashmap<string,string> m=new hashmap<string,string> (); String [] A=str.split (""); for (int i=0;i<a.length;i++) { if (!m.containskey (A[i])) { m.put (a[i], "1"); count++; Newstr=newstr+ "" +a[i]; } } System.out.println ("The number of words in this passage is:" +count+ "," +newstr "); }
7. Write the results of the program operation.
public class Test1 { private static void Test (Int[]arr) {for (int i = 0; i < arr.length; i++) { try { If (Arr[i]% 2 = = 0) { throw new NullPointerException (); } else { System.out.print (i);} } catch (Exception e) { System.out.print ("a"); } finally { System.out.print ("B"); }}} public static void Main (String[]args) { try { test (new int[] {0, 1, 2, 3, 4, 5}), } catch (Exception e) {
system.out.print ("C");}}}
Operating Result: A B 1b a B 3b a B 5b
public class Test1 { private static void Test (Int[]arr) {for (int i = 0; i < arr.length; i++) { try { If (Arr[i]% 2 = = 0) { throw new NullPointerException (); } else { System.out.print (i);} } finally { System.out.print ("B"); }}} public static void Main (String[]args) { try { test (new int[] {0, 1, 2, 3, 4, 5}), } catch (Exception e) {
system.out.print ("C");}}}
Run Result: b C
8. Number of words
Count the total number of different words in a story.
Input
There are multiple sets of data, each group of rows, each group is a small article. Each small article is composed of lowercase letters and spaces, no punctuation, and when you encounter #, the input ends.
Output
Each set of values outputs an integer that represents the total number of different words in a single article.
Sample Input
You are my friend
#
Sample Output
4
public static void Main (String [] args) { list<integer> countlist=new arraylist<integer> (); int count; Hashmap<string,string> m; String str; Read one line of keyboard input (end input with carriage return change) string[] A; Scanner in=new Scanner (system.in); while (!) ( Str=in.nextline ()). Equals ("#")) { a=str.split (""); M=new hashmap<string,string> (); Count = 0; for (int i=0;i<a.length;i++) { if (!m.containskey (A[i]) && (!a[i].equals ("))) { m.put (a[i]," 1 ") ; count++; } } Countlist.add (count); } s for (int c:countlist) System.out.println (c); }
Java Classic algorithm Problem _2