50個java編程程序之四

【程序31
題目:將一個數組逆序輸出。   
import java.util.*;
public class lianxi31 {
public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
   int a[] = new int[20];
System.out.println("
請輸入多個正整數(輸入-1表示結束):");
   int i=0,j;
   do{
      a[i]=s.nextInt();
      i++;
   }while (a[i-1]!=-1);
   System.out.println("
你輸入的數組爲:");
   for( j=0; j<i-1; j++) {
    System.out.print(a[j]+"   ");
}
   System.out.println("\n
數組逆序輸出爲:");
   for( j=i-2; j>=0; j=j-1) {
    System.out.print(a[j]+"   ");
}
    }
   }
【程序32   
題目:取一個整數a從右端開始的47位。   
import java.util.*;
public class lianxi32 {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("
請輸入一個7位以上的正整數:");
    long a = s.nextLong();
    String ss = Long.toString(a);
    char[] ch = ss.toCharArray();
    int j=ch.length;
    if (j<7){System.out.println("
輸入錯誤!");}
    else {
     System.out.println("
截取從右端開始的47位是:"+ch[j-7]+ch[j-6]+ch[j-5]+ch[j-4]);
     }
    }
    }
【程序33  
題目:打印出楊輝三角形(要求打印出10行如下圖)      
           1   
          1   1   
        1   2    1   
      1    3   3    1   
    1    4    6   4    1   
1    5    10   10    5    1   
…………
public class lianxi33 {
public static void main(String[] args) {
    int[][] a = new int[10][10];
   for(int i=0; i<10; i++) {
    a[i][i] = 1;
    a[i][0] = 1;
   }
   for(int i=2; i<10; i++) {
    for(int j=1; j<i; j++) {
     a[i][j] = a[i-1][j-1] + a[i-1][j];
    }
   }
     for(int i=0; i<10; i++) {
    for(int k=0; k<2*(10-i)-1; k++) {
     System.out.print(" ");
    }
    for(int j=0; j<=i; j++) {
     System.out.print(a[i][j] + "   ");
    }
    System.out.println();
   }
}
}
【程序34   
題目:輸入3個數a,b,c,按大小順序輸出。   
import java.util.Scanner;
public class lianxi34 {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("
請輸入3個整數:");
    int a = s.nextInt();
    int b = s.nextInt();
    int c = s.nextInt();
      if(a < b) {
     int t = a;
     a = b;
     b = t;
    }
      if(a < c) {
     int t = a;
     a = c;
     c = t;
    }
     if(b < c) {
     int t = b;
     b = c;
     c = t;
    }
    System.out.println("
從大到小的順序輸出:");
    System.out.println(a + " " + b + " " +c);
}
}
【程序35   
題目:輸入數組,最大的與第一個元素交換,最小的與最後一個元素交換,輸出數組。   
import java.util.*;
public class lianxi35 {
public static void main(String[] args) {
   int N = 8;
   int[] a = new int [N];
   Scanner s = new Scanner(System.in);
   int idx1 = 0, idx2 = 0;
   System.out.println("
請輸入8個整數:");
   for(int i=0; i<N; i++) {
    a[i] = s.nextInt();
}
   System.out.println("
你輸入的數組爲:");
   for(int i=0; i<N; i++) {
     System.out.print(a[i] + " ");
   }
   int max =a[0], min = a[0];
   for(int i=0; i<N; i++) {
    if(a[i] > max) {
     max = a[i];
     idx1 = i;
    }
    if(a[i] < min) {
     min = a[i];
     idx2 = i;
    }
   }
   if(idx1 != 0) {
    int temp = a[0];
    a[0] = a[idx1];
    a[idx1] = temp;
   }
    if(idx2 != N-1) {
    int temp = a[N-1];
    a[N-1] = a[idx2];
    a[idx2] = temp;
   }
   System.out.println("\n
交換後的數組爲:");
   for(int i=0; i<N; i++) {
    System.out.print(a[i] + " ");
   }
}
}
【程序36   
題目:有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] + " ");
   }
}
}
【程序37   
題目:有n個人圍成一圈,順序排號。從第一個人開始報數(從13報數),凡報到3的人退出圈子,問最後留下的是原來第幾號的那位。   
import java.util.Scanner;
public class lianxi37 {
public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
   System.out.print("
請輸入排成一圈的人數:");
   int n = s.nextInt();
   boolean[] arr = new boolean[n];
   for(int i=0; i<arr.length; i++) {
    arr[i] = true;
   }
   int leftCount = n;
   int countNum = 0;
   int index = 0;
   while(leftCount > 1) {
    if(arr[index] == true) {
     countNum ++;
     if(countNum == 3) {
      countNum =0;
      arr[index] = false;
      leftCount --;
     }
    }
     index ++;
     if(index == n) {
     index = 0;
    }
   }
    for(int i=0; i<n; i++) {
    if(arr[i] == true) {
     System.out.println("
原排在第"+(i+1)+"位的人留下了。");
    }
   }
}
}
【程序38   
題目:寫一個函數,求一個字符串的長度,在main函數中輸入字符串,並輸出其長度。   
/*………………
*……
題目意思似乎不能用length()函數     */
import java.util.*;
public class lianxi38 {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("
請輸入一個字符串:");
    String str = s.nextLine();
     System.out.println("
字符串的長度是:"+str.length());
    }
    }
【程序39   
題目:編寫一個函數,輸入n爲偶數時,調用函數求1/2+1/4+...+1/n,當輸入n爲奇數時,調用函數1/1+1/3+...+1/n(利用指針函數)   
//
沒有利用指針函數
import java.util.*;
public class lianxi39 {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("
請輸入一個正整數 n= ");
    int n = s.nextInt();
    System.out.println("
相應數列的和爲:" + sum(n));
   }
public static double sum(int n) {
    double res = 0;
    if(n % 2 == 0) {
     for(int i=2; i<=n; i+=2) {
      res += (double)1 / i;
     }
    } else {
     for(int i=1; i<=n; i+=2) {
      res += (double)1 / i ;
     }
    }
    return res;
}
}
【程序40   
題目:字符串排序。   
public class lianxi40 {
public static void main(String[] args) {
   int N=5;
   String temp = null;
   String[] s = new String[N];
   s[0] = "matter";
   s[1] = "state";
   s[2] = "solid";
   s[3] = "liquid";
   s[4] = "gas";
   for(int i=0; i<N; i++) {
    for(int j=i+1; j<N; j++) {
     if(compare(s[i], s[j]) == false) {
      temp = s[i];
      s[i] = s[j];
      s[j] = temp;
     }
    }
   }
    for(int i=0; i<N; i++) {
    System.out.println(s[i]);
   }
}
static boolean compare(String s1, String s2) {
   boolean result = true;
   for(int i=0; i<s1.length() && i<s2.length(); i++) {
    if(s1.charAt(i) > s2.charAt(i)) {
     result = false;
     break;
    } else if(s1.charAt(i) <s2.charAt(i)) {
     result = true;
     break;
    } else {
     if(s1.length() < s2.length()) {
      result = true;
     } else {
      result = false;
     }
    }
   }
   return result;
}
}

發佈了15 篇原創文章 · 獲贊 32 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章