第三次考核

第一题:字符串逆序

马云:https://gitee.com/xjw-xjw/16012017_3/blob/master/Main

7-1 字符串逆序 (7 分)

输入一个字符串,对该字符串进行逆序,输出逆序后的字符串。

输入格式:

输入在一行中给出一个不超过80个字符长度的、以回车结束的非空字符串。

输出格式:

在一行中输出逆序后的字符串。

输入样例:

Hello World!

输出样例:

!dlroW olleH
代码
import java.util.Scanner;
public class Main{
  public static void main (String args[]){
    Scanner sc=new Scanner(System.in );
    String str1=sc.nextLine();
    String str2="";
    for (int i=str1.length()-1;i>=0;i--){
      char ch=str1.charAt(i);
      str2+=ch;
    }
    System.out.println(str2);
  }
 
}

运行结果:Hello World!
     !dlroW olleH


第二题:输出数组元素

马云:https://gitee.com/xjw-xjw/16012017_3/blob/master/Shu

本题要求编写程序,对顺序读入的n个整数,顺次计算后项减前项之差,并按每行三个元素的格式输出结果。

输入格式:

输入的第一行给出正整数n(1<n10)。随后一行给出n个整数,其间以空格分隔。

输出格式:

顺次计算后项减前项之差,并按每行三个元素的格式输出结果。数字间空一格,行末不得有多余空格。

输入样例:

10
5 1 7 14 6 36 4 28 50 100

输出样例:

-4 6 7
-8 30 -32
24 22 50

代码:
import java.util.Scanner;
public class Main{
  public static void main(String args[]){
     Scanner reader = new Scanner(System.in);
     int n = reader.nextInt();
     int[] a = new int[n];
     int i=0;
     int cnt=0;
     for(i=0;i<n;i++){
       a[i]=reader.nextInt();
     }
     for (i = 0; i < n - 1; i++){
      a[i] = a[i + 1] - a[i];
     }
     for (i = 0; i < n - 1; i++){
      if (i == 0){
       System.out.printf("%d", a[0]);
      }
      else if (cnt == 3){
       System.out.printf("\n");
       System.out.printf("%d", a[i]);
       cnt = 0;
      }
      else{
       System.out.printf(" %d", a[i]);
      }
      cnt++;
     }
   }
 }

运行结果:
10
5 1 7 14 6 36 4 28 50 100
-4 6 7
-8 30 -32
24 22 50


第三题:选择性排序

马云:https://gitee.com/xjw-xjw/16012017_3/blob/master/Pai

本题要求将给定的n个整数从大到小排序后输出。

输入格式:

输入第一行给出一个不超过10的正整数n。第二行给出n个整数,其间以空格分隔。

输出格式:

在一行中输出从大到小有序的数列,相邻数字间有一个空格,行末不得有多余空格。

输入样例:

4
5 1 7 6

输出样例:

7 6 5 1
代码:
import java.util.Scanner;
public class Main{
  public static void main(String args[]){
    Scanner reader = new Scanner(System.in);
   int n = reader.nextInt();
   int[] a = new int[n];
   int x=0;
   for(int i=0;i<n;i++){
     a[i]=reader.nextInt();
   }
   for(int i=0;i<n;i++){
     for(int j=1;j<n;j++){
       if(a[j]>a[j-1]){
         x=a[j];
         a[j]=a[j-1];
         a[j-1]=x;
       }
     }
   }
   for(int i=0;i<n;i++){
     System.out.print(a[i]);
     if(i!=n-1){
       System.out.print(" ");
     }
   }
  }
}

运行结果:
4
5 1 7 6
7 6 5 1

第四题:对不起老师...这题我不会。。。。。



 

posted on 2018-11-03 19:38  徐佳韦  阅读(175)  评论(1编辑  收藏  举报

导航