Java经典基础练习31-40

【程序31】
题目:将一个数组逆序输出。   
  1. import java.util.*;  
  2. public class lianxi31 {  
  3.     public static void main(String[] args) {  
  4.         Scanner s = new Scanner(System.in);  
  5.         int a[] = new int[20];  
  6.         System.out.println(”请输入多个正整数(输入-1表示结束):”);  
  7.         int i=0,j;  
  8.         do{  
  9.             a[i]=s.nextInt();  
  10.             i++;  
  11.         }while (a[i-1]!=-1);  
  12.         System.out.println(”你输入的数组为:”);  
  13.         for( j=0; j<i-1; j++) {  
  14.             System.out.print(a[j]+”   ”);  
  15.         }  
  16.         System.out.println(”\n数组逆序输出为:”);  
  17.         for( j=i-2; j>=0; j=j-1) {  
  18.             System.out.print(a[j]+”   ”);  
  19.         }  
  20.     <span style=”white-space:pre”>    </span>}  
  21. }  
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]+"   ");
        }
    <span style="white-space:pre">    </span>}
}


【程序32】   
题目:取一个整数a从右端开始的4~7位。   
  1. import java.util.*;  
  2. public class lianxi32 {  
  3.     public static void main(String[] args) {  
  4.         Scanner s = new Scanner(System.in);  
  5.         System.out.print(”请输入一个7位以上的正整数:”);  
  6.         long a = s.nextLong();  
  7.         String ss = Long.toString(a);  
  8.         char[] ch = ss.toCharArray();  
  9.         int j=ch.length;  
  10.         if (j<7){System.out.println(“输入错误!”);}  
  11.         else {  
  12.             System.out.println(”截取从右端开始的4~7位是:”+ch[j-7]+ch[j-6]+ch[j-5]+ch[j-4]);  
  13.         }  
  14.     }   
  15. }  
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   
…………
  1. public class lianxi33 {  
  2.     public static void main(String[] args) {  
  3.         int[][] a = new int[10][10];  
  4.         for (int i = 0; i < 10; i++) {  
  5.             a[i][i] = 1;  
  6.             a[i][0] = 1;  
  7.         }  
  8.         for (int i = 2; i < 10; i++) {  
  9.             for (int j = 1; j < i; j++) {  
  10.                 a[i][j] = a[i - 1][j - 1] + a[i - 1][j];  
  11.             }  
  12.         }  
  13.         for (int i = 0; i < 10; i++) {  
  14.             for (int k = 0; k < 2 * (10 - i) - 1; k++) {  
  15.                 System.out.print(” ”);  
  16.             }  
  17.             for (int j = 0; j <= i; j++) {  
  18.                 System.out.print(a[i][j] + ”   ”);  
  19.             }  
  20.             System.out.println();  
  21.         }  
  22.     }  
  23. }  
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,按大小顺序输出。   
  1. import java.util.Scanner;  
  2.   
  3. public class lianxi34 {  
  4.     public static void main(String[] args) {  
  5.         Scanner s = new Scanner(System.in);  
  6.         System.out.println(”请输入3个整数:”);  
  7.         int a = s.nextInt();  
  8.         int b = s.nextInt();  
  9.         int c = s.nextInt();  
  10.         if (a < b) {  
  11.             int t = a;  
  12.             a = b;  
  13.             b = t;  
  14.         }  
  15.         if (a < c) {  
  16.             int t = a;  
  17.             a = c;  
  18.             c = t;  
  19.         }  
  20.         if (b < c) {  
  21.             int t = b;  
  22.             b = c;  
  23.             c = t;  
  24.         }  
  25.         System.out.println(”从大到小的顺序输出:”);  
  26.         System.out.println(a + ” ” + b + “ ” + c);  
  27.     }  
  28. }  
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】   
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。   
  1. import java.util.*;  
  2. public class lianxi35 {  
  3.     public static void main(String[] args) {  
  4.         int N = 8;  
  5.         int[] a = new int [N];  
  6.         Scanner s = new Scanner(System.in);  
  7.         int idx1 = 0, idx2 = 0;  
  8.         System.out.println(”请输入8个整数:”);  
  9.         for(int i=0; i<N; i++) {  
  10.             a[i] = s.nextInt();  
  11.         }  
  12.         System.out.println(”你输入的数组为:”);  
  13.         for(int i=0; i<N; i++) {  
  14.             System.out.print(a[i] + ” ”);  
  15.         }  
  16.         int max =a[0], min = a[0];  
  17.         for(int i=0; i<N; i++) {  
  18.             if(a[i] > max) {  
  19.                 max = a[i];  
  20.                 idx1 = i;  
  21.             }   
  22.             if(a[i] < min) {  
  23.                 min = a[i];  
  24.                 idx2 = i;  
  25.             }  
  26.         }   
  27.         if(idx1 != 0) {  
  28.             int temp = a[0];  
  29.             a[0] = a[idx1];  
  30.             a[idx1] = temp;  
  31.         }  
  32.         if(idx2 != N-1) {  
  33.             int temp = a[N-1];  
  34.             a[N-1] = a[idx2];  
  35.             a[idx2] = temp;  
  36.         }  
  37.         System.out.println(”\n交换后的数组为:”);  
  38.         for(int i=0; i<N; i++) {  
  39.             System.out.print(a[i] + ” ”);  
  40.         }  
  41.     }  
  42. }  
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个数   
  1. import java.util.Scanner;  
  2.   
  3. public class lianxi36 {  
  4.     public static void main(String[] args) {  
  5.         int N = 10;  
  6.         int[] a = new int[N];  
  7.         Scanner s = new Scanner(System.in);  
  8.         System.out.println(”请输入10个整数:”);  
  9.         for (int i = 0; i < N; i++) {  
  10.             a[i] = s.nextInt();  
  11.         }  
  12.         System.out.print(”你输入的数组为:”);  
  13.         for (int i = 0; i < N; i++) {  
  14.             System.out.print(a[i] + ” ”);  
  15.         }  
  16.         System.out.print(”\n请输入向后移动的位数:”);  
  17.         int m = s.nextInt();  
  18.         int[] b = new int[m];  
  19.         for (int i = 0; i < m; i++) {  
  20.             b[i] = a[N - m + i];  
  21.         }  
  22.         for (int i = N - 1; i >= m; i–) {  
  23.             a[i] = a[i - m];  
  24.         }  
  25.         for (int i = 0; i < m; i++) {  
  26.             a[i] = b[i];  
  27.         }  
  28.         System.out.print(”位移后的数组是:”);  
  29.         for (int i = 0; i < N; i++) {  
  30.             System.out.print(a[i] + ” ”);  
  31.         }  
  32.     }  
  33. }  
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的人退出圈子,问最后留下的是原来第几号的那位。   
  1. import java.util.Scanner;  
  2.   
  3. public class lianxi37 {  
  4.     public static void main(String[] args) {  
  5.         Scanner s = new Scanner(System.in);  
  6.         System.out.print(”请输入排成一圈的人数:”);  
  7.         int n = s.nextInt();  
  8.         boolean[] arr = new boolean[n];  
  9.         for (int i = 0; i < arr.length; i++) {  
  10.             arr[i] = true;  
  11.         }  
  12.         int leftCount = n;  
  13.         int countNum = 0;  
  14.         int index = 0;  
  15.         while (leftCount > 1) {  
  16.             if (arr[index] == true) {  
  17.                 countNum++;  
  18.                 if (countNum == 3) {  
  19.                     countNum = 0;  
  20.                     arr[index] = false;  
  21.                     leftCount–;  
  22.                 }  
  23.             }  
  24.             index++;  
  25.             if (index == n) {  
  26.                 index = 0;  
  27.             }  
  28.         }  
  29.         for (int i = 0; i < n; i++) {  
  30.             if (arr[i] == true) {  
  31.                 System.out.println(”原排在第” + (i + 1) + “位的人留下了。”);  
  32.             }  
  33.         }  
  34.     }  
  35. }  
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()函数     */
不用函数做不出
【程序39】   
题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+…+1/n,当输入n为奇数时,调用函数1/1+1/3+…+1/n(利用指针函数)   
//没有利用指针函数
  1. import java.util.*;  
  2.   
  3. public class lianxi39 {  
  4.     public static void main(String[] args) {  
  5.         Scanner s = new Scanner(System.in);  
  6.         System.out.print(”请输入一个正整数 n= ”);  
  7.         int n = s.nextInt();  
  8.         System.out.println(”相应数列的和为:” + sum(n));  
  9.     }  
  10.   
  11.     public static double sum(int n) {  
  12.         double res = 0;  
  13.         if (n % 2 == 0) {  
  14.             for (int i = 2; i <= n; i += 2) {  
  15.                 res += (double1 / i;  
  16.             }  
  17.         } else {  
  18.             for (int i = 1; i <= n; i += 2) {  
  19.                 res += (double1 / i;  
  20.             }  
  21.         }  
  22.         return res;  
  23.     }  
  24. }  
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】   
题目:字符串排序。   
  1. public class lianxi40 {  
  2.     public static void main(String[] args) {  
  3.         int N = 5;  
  4.         String temp = null;  
  5.         String[] s = new String[N];  
  6.         s[0] = “matter”;  
  7.         s[1] = “state”;  
  8.         s[2] = “solid”;  
  9.         s[3] = “liquid”;  
  10.         s[4] = “gas”;  
  11.         for (int i = 0; i < N; i++) {  
  12.             for (int j = i + 1; j < N; j++) {  
  13.                 if (compare(s[i], s[j]) == false) {  
  14.                     temp = s[i];  
  15.                     s[i] = s[j];  
  16.                     s[j] = temp;  
  17.                 }  
  18.             }  
  19.         }  
  20.         for (int i = 0; i < N; i++) {  
  21.             System.out.println(s[i]);  
  22.         }  
  23.     }  
  24.   
  25.     static boolean compare(String s1, String s2) {  
  26.         boolean result = true;  
  27.         for (int i = 0; i < s1.length() && i < s2.length(); i++) {  
  28.             if (s1.charAt(i) > s2.charAt(i)) {  
  29.                 result = false;  
  30.                 break;  
  31.             } else if (s1.charAt(i) < s2.charAt(i)) {  
  32.                 result = true;  
  33.                 break;  
  34.             } else {  
  35.                 if (s1.length() < s2.length()) {  
  36.                     result = true;  
  37.                 } else {  
  38.                     result = false;  
  39.                 }  
  40.             }  
  41.         }  
  42.         return result;  
  43.     }  
  44. }  
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;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值