【程序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從右端開始的4~7位。
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("截取從右端開始的4~7位是:"+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個人圍成一圈,順序排號。從第一個人開始報數(從1到3報數),凡報到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;
}
}
【程序41】
題目:海灘上有一堆桃子,五隻猴子來分。第一隻猴子把這堆桃子憑據分爲五份,多了一個,這隻猴子把多的一個扔入海中,拿走了一份。第二隻猴子把剩下的桃子又平均分成五份,又多了一個,它同樣把多的一個扔入海中,拿走了一份,第三、第四、第五隻猴子都是這樣做的,問海灘上原來最少有多少個桃子?
public class lianxi41 {
public static void main (String[] args) {
int i,m,j=0,k,count;
for(i=4;i<10000;i+=4)
{ count=0;
m=i;
for(k=0;k<5;k++)
{
j=i/4*5+1;
i=j;
if(j%4==0)
count++;
else break;
}
i=m;
if(count==4)
{System.out.println("原有桃子 "+j+" 個");
break;}
}
}
}
【程序42】
題目:809*??=800*??+9*??+1 其中??代表的兩位數,8*??的結果爲兩位數,9*??的結果爲3位數。求??代表的兩位數,及809*??後的結果。
//題目錯了!809x=800x+9x+1 這樣的方程無解。去掉那個1就有解了。
public class lianxi42 {
public static void main (String[] args) {
int a=809,b,i;
for(i=10;i<13;i++)
{b=i*a ;
if(8*i<100&&9*i>=100)
System.out.println("809*"+i+"="+"800*"+i+"+"+"9*"+i+"="+b);}
}
}
【程序43】
題目:求0—7所能組成的奇數個數。
//組成1位數是4個。
//組成2位數是7*4個。
//組成3位數是7*8*4個。
//組成4位數是7*8*8*4個。
//......
public class lianxi43 {
public static void main (String[] args) {
int sum=4;
int j;
System.out.println("組成1位數是"+sum+" 個");
sum=sum*7;
System.out.println("組成2位數是"+sum+" 個");
for(j=3;j<=9;j++){
sum=sum*8;
System.out.println("組成"+j+"位數是 "+sum+" 個");
}
}
}
【程序44】
題目:一個偶數總能表示爲兩個素數之和。
//由於用除sqrt(n)的方法求出的素數不包括2和3,
//因此在判斷是否是素數程序中人爲添加了一個3。
import java.util.*;
public class lianxi44 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n,i;
do{
System.out.print("請輸入一個大於等於6的偶數:");
n = s.nextInt();
} while(n<6||n%2!=0); //判斷輸入是否是>=6偶數,不是,重新輸入
fun fc = new fun();
for(i=2;i<=n/2;i++){
if((fc.fun(i))==1&&(fc.fun(n-i)==1))
{int j=n-i;
System.out.println(n+" = "+i+" +"+j);
} //輸出所有可能的素數對
}
}
}
class fun{
public int fun (int a) //判斷是否是素數的函數
{
int i,flag=0;
if(a==3){flag=1;return(flag);}
for(i=2;i<=Math.sqrt(a);i++){
if(a%i==0) {flag=0;break;}
else flag=1;}
return (flag) ;//不是素數,返回0,是素數,返回1
}
}
//解法二
import java.util.*;
public class lianxi44 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n;
do{
System.out.print("請輸入一個大於等於6的偶數:");
n = s.nextInt();
} while(n<6||n%2!=0); //判斷輸入是否是>=6偶數,不是,重新輸入
for(int i=3;i<=n/2;i+=2){
if(fun(i)&&fun(n-i)) {
System.out.println(n+" = "+i+" +"+(n-i));
} //輸出所有可能的素數對
}
}
static boolean fun (int a){ //判斷是否是素數的函數
boolean flag=false;
if(a==3){flag=true;return(flag);}
for(int i=2;i<=Math.sqrt(a);i++){
if(a%i==0) {flag=false;break;}
else flag=true;}
return (flag) ;
}
}
【程序45】
題目:判斷一個素數能被幾個9整除
//題目錯了吧?能被9整除的就不是素數了!所以改成整數了。
import java.util.*;
public class lianxi45 {
public static void main (String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("請輸入一個整數:");
int num = s.nextInt();
int tmp = num;
int count = 0;
for(int i = 0 ; tmp%9 == 0 ;){
tmp = tmp/9;
count ++;
}
System.out.println(num+" 能夠被 "+count+" 個9整除。");
}
}
【程序46】
題目:兩個字符串連接程序
import java.util.*;
public class lianxi46 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("請輸入一個字符串:");
String str1 = s.nextLine();
System.out.print("請再輸入一個字符串:");
String str2 = s.nextLine();
String str = str1+str2;
System.out.println("連接後的字符串是:"+str);
}
}
【程序47】
題目:讀取7個數(1—50)的整數值,每讀取一個值,程序打印出該值個數的*。
import java.util.*;
public class lianxi47 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n=1,num;
while(n<=7){
do{
System.out.print("請輸入一個1--50之間的整數:");
num=s.nextInt();
}while(num<1||num>50);
for(int i=1;i<=num;i++)
{System.out.print("*");
}
System.out.println();
n ++;
}
}
}
【程序48】
題目:某個公司採用公用電話傳遞數據,數據是四位的整數,在傳遞過程中是加密的,加密規則如下:每位數字都加上5,然後用和除以10的餘數代替該數字,再將第一位和第四位交換,第二位和第三位交換。
import java.util.*;
public class lianxi48 {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int num=0,temp;
do{
System.out.print("請輸入一個4位正整數:");
num = s.nextInt();
}while (num<1000||num>9999);
int a[]=new int[4];
a[0] = num/1000; //取千位的數字
a[1] = (num/100)%10; //取百位的數字
a[2] = (num/10)%10; //取十位的數字
a[3] = num%10; //取個位的數字
for(int j=0;j<4;j++)
{
a[j]+=5;
a[j]%=10;
}
for(int j=0;j<=1;j++)
{
temp = a[j];
a[j] = a[3-j];
a[3-j] =temp;
}
System.out.print("加密後的數字爲:");
for(int j=0;j<4;j++)
System.out.print(a[j]);
}
}
【程序49】
題目:計算字符串中子串出現的次數
import java.util.*;
public class lianxi49 {
public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.print("請輸入字符串:");
String str1 = s.nextLine();
System.out.print("請輸入子串:");
String str2 = s.nextLine();
int count=0;
if(str1.equals("")||str2.equals(""))
{
System.out.println("你沒有輸入字符串或子串,無法比較!");
System.exit(0);
}
else
{
for(int i=0;i<=str1.length()-str2.length();i++)
{
if(str2.equals(str1.substring(i, str2.length()+i)))
//這種比法有問題,會把"aaa"看成有2個"aa"子串。
count++;
}
System.out.println("子串在字符串中出現: "+count+"次");
}
}
}
【程序50】
題目:有五個學生,每個學生有3門課的成績,從鍵盤輸入以上數據(包括學生號,姓名,三門課成績),計算出平均成績,把原有的數據和計算出的平均分數存放在磁盤文件 "stud "中。
import java.io.*;
import java.util.*;
public class lianxi50 {
public static void main(String[] args){
Scanner ss = new Scanner(System.in);
String [][] a = new String[5][6];
for(int i=1; i<6; i++) {
System.out.print("請輸入第"+i+"個學生的學號:");
a[i-1][0] = ss.nextLine();
System.out.print("請輸入第"+i+"個學生的姓名:");
a[i-1][1] = ss.nextLine();
for(int j=1; j<4; j++) {
System.out.print("請輸入該學生的第"+j+"個成績:");
a[i-1][j+1] = ss.nextLine();
}
System.out.println("\n");
}
//以下計算平均分
float avg;
int sum;
for(int i=0; i<5; i++) {
sum=0;
for(int j=2; j<5; j++) {
sum=sum+ Integer.parseInt(a[i][j]);
}
avg= (float)sum/3;
a[i][5]=String.valueOf(avg);
}
//以下寫磁盤文件
String s1;
try {
File f = new File("C:\\stud");
if(f.exists()){
System.out.println("文件存在");
}else{
System.out.println("文件不存在,正在創建文件");
f.createNewFile();//不存在則創建
}
BufferedWriter output = new BufferedWriter(new FileWriter(f));
for(int i=0; i<5; i++) {
for(int j=0; j<6; j++) {
s1=a[i][j]+"\r\n";
output.write(s1);
}
}
output.close();
System.out.println("數據已寫入c盤文件stud中!");
} catch (Exception e) {
e.printStackTrace();
}
}
}