50道JAVA基础编程练习题4

【程序30】题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。

import java.util.Scanner;
public class Prog30{
       public
static void main(String[] args){
              int
[] a= {1,2,3,4,5,6,7,8,9};
              Scanner
sc=new Scanner(System.in);
              System.out.println(“请输入一个数:”);
              int
n=sc.nextInt();
              System.out.println(“插入后的数组为:”);
              int
[] b=new int[a.length+1];
              insert(a,b,n);
              for(int
i=0;i<b.length;i++) {
                     System.out.print(b[i]+"
");
              }
       }
       public
static void insert(int [] a,int [] b,int n) {
              int
index = 0;
              for(int
i=0;i<a.length;i++) {
                     b[i]=a[i];
              }
              for(int
j=0;j<b.length;j++) {
                     if(n<b[j])
{
                            index=j;
                            break;
                     }
              }
              for(int
x=b.length-1;x>index;x–) {
                     b[x]=b[x-1];
              }
              b[index]=n;
       }
}

【程序31】题目:将一个数组逆序输出。

public class Prog31{
       public
static void main(String[] args){
              int
[] a= {1,2,5,3,4,5,6,2,8,9};
              for(int
i=0;i<a.length/2;i++) {
                     int
temp=a[i];
                     a[i]=a[a.length-i-1];
                     a[a.length-i-1]=temp;
              }
              for(int
i=0;i<a.length;i++) {
                     System.out.print(a[i]+"
");
              }
       }
}

【程序32】题目:取一个整数a从右端开始的4~7位。

import java.util.Scanner;
public class Prog32{
       public
static void main(String[] args){
              Scanner
sc=new Scanner(System.in);
              System.out.println(“输入一个数字”);
              long
i=sc.nextInt();
              String
s=Long.toString(i);
              char[]
chs=s.toCharArray();
              int
n=chs.length;
              if(n<7)
{
                     System.out.println(“输入的数小于7位”);
              }else
{
                     System.out.println(“截取的数字为”+chs[n-7]+chs[n-6]+chs[n-5]+chs[n-4]);
              }
       }
}

【程序33】题目:打印出杨辉三角形(要求打印出10行如下图)

public class Prog33{
       public
static void main(String[] args) {
              System.out.print(“请输入一个正整数:”);
              Scanner
in = new Scanner(System.in);
              int
n = in.nextInt();
              System.out.println(“对应的杨辉三角为:”);
              triangle(n);
       }
       public
static void triangle(int n) {
              int[][]
arr = new int[n][n];
              for
(int i = 0; i < n; i++) {
                     for
(int j = 0; j <= i; j++) {
                            if
(i == 0 || i == j || j == 0) {
                                   arr[i][j]
= 1;
                            }
else {
                                   arr[i][j]
= arr[i - 1][j - 1] + arr[i - 1][j];
                            }
                     }
              }
              for
(int i = 0; i < n; i++) {
                     for
(int j = i; j < n - 1; j++) {
                            System.out.print("
");
                     }
                     for
(int k = 0; k <= i; k++) {
                            System.out.print(arr[i][k]

  • " ");
                         }
                         System.out.println();
                  }
           }
    }

【程序34】题目:输入3个数a,b,c,按大小顺序输出。

import java.util.Scanner;
public class Prog34{
       public
static void main(String[] args){
              Scanner
sc=new Scanner(System.in);
              System.out.println(“请输入三个数字:”);
              int
a=sc.nextInt();
              int
b=sc.nextInt();
              int
c=sc.nextInt();
              int
t;
              if(a>b)
{
                     t=a;
                     a=b;
                     b=t;
              }
              if(a>c)
{
                     t=a;
                     a=c;
                     c=t;
              }
              if(b>c)
{
                     t=b;
                     b=c;
                     c=t;
              }
              System.out.println(a+"
“+b+” "+c);
       }
}

【程序35】输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

import java.util.Scanner;
public class Prog35{
public static void main(String[] args)
{
              int[]
a = {4,3,2,1,6,4,3,2,18,98};
              int
min = 0;
              int
max = 0;
              for
(int i = 0; i < a.length; i++) {
                     if
(a[i] > a[max]) {
                            max
= i;
                     }
                     if
(a[i] < a[min]) {
                            min
= i;
                     }
              }
              int
t = a[max];
              a[max]
= a[0];
              a[0]
= t;
              int
r = a[min];
              a[min]
= a[a.length - 1];
              a[a.length

  • 1] = r;
                  for
    (int i = 0; i < a.length; i++) {
                         System.out.print(a[i]
  • " ");
                  }
           }
    }

【程序36】有n个整数,前面各数顺序向后移m个位置,最后m个数变成最前面的m个数

import java.util.Scanner;
public class Prog36{
public static void main(String[] args)
{
              int[]
a = {4,3,2,1,6,4};
              int
[] b=new int[a.length];
              Scanner
sc=new Scanner(System.in);
              System.out.println(“移动多少个位置:”);
              int
n=sc.nextInt();
              move(a,b,n);
              System.out.println(“移动后的数组为:”);
              for(int
i=0;i<b.length;i++) {
                     System.out.print(b[i]+"
");
              }
       }
       public
static void move(int[] a,int [] b,int n){
              int
i,k=0;
              for(i=a.length-n;i<a.length;i++)
{
                     b[k]=a[i];
                     k++;
              }
              for(i=0;i<a.length-n;i++)
{
                     b[k]=a[i];
                     k++;
              }
       }
}

【程序37】题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

import java.util.Scanner;
public class Prog37{
       public
static void main(String[] args){
              System.out.print(“请输入一个整数:”);
              Scanner
sc = new Scanner(System.in);
              int
n = sc.nextInt();
              boolean[]
In = new boolean[n];
              for(int
i=0;i<In.length;i++)
                In[i] = true;
              int
count = n;
              int
num = 0;
              int
index = 0;
              while(count>1){
                     if(In[index]){
                            num++;
                            if(num3){
                                   num
= 0;
                                   In[index]
= false;
                                   count–;
                            }
                     }
                     index++;
                     if(index
n)
                       index = 0;
              }
              for(int
i=0;i<n;i++)
                if(In[i])
                  System.out.println(“留下的是:”+(i+1));
       }
}

【程序38】写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

import java.util.Scanner;
public class Prog38{
       public
static void main(String[] args){
              Scanner
sc=new Scanner(System.in);
              System.out.println(“输入一个字符串:”);
              String
s=sc.nextLine();
              char[]
chs=s.toCharArray();
              System.out.println(chs.length);
       }
}

【程序39】题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+…+1/n,当输入n为奇数时,调用函数1/1+1/3+…+1/n(利用指针函数)

import java.util.Scanner;
public class Prog39{
       public
static void main(String[] args){
              Scanner
sc=new Scanner(System.in);
              System.out.println(“输入一个整数:”);
              int
n=sc.nextInt();
              if(n%2==0)
{
                     System.out.println(even(n));
              }else
{
                     System.out.println(odd(n));
              }
       }
       public
static double even(int n) {
              double
s=0;
              for(int
i=2;i<n+1;i+=2) {
                     s+=1.0/i;
              }
              return
s;
       }
       public
static double odd(int n) {
              double
s=0;
              for(int
i=1;i<n+1;i+=2) {
                     s+=1.0/i;
              }
              return
s;
       }
}

【程序40】题目:字符串排序。

public class Prog40{
       public
static void main(String[] args){
              String[]
str= {“abc”,“cad”,“m”,“fa”,“f”};
              for(int
i=str.length-1;i>=1;i–) {
                     for(int
j=0;j<=i-1;j++) {
                            if(str[j].compareTo(str[j+1])>0)
{
                                   String
temp=str[j];
                                   str[j]=str[j+1];
                                   str[j+1]=temp;
                            }
                     }
              }
              for(String
sub:str) {
                     System.out.print(sub+"
");
              }
       }
}

发布了10 篇原创文章 · 获赞 0 · 访问量 192

猜你喜欢

转载自blog.csdn.net/gj55678/article/details/105460685