第三次过程性考核

码云地址  :https://gitee.com/huangssss/third_procedural_assessment.git

第一题

import java.util.*; 

public class Main { 
public static void main(String[] args) { 
int i, min, max, n, temp1, temp2; 
int a[]; 
Scanner reader = new Scanner(System.in); 
n = reader.nextInt(); 
a = new int[n]; 
for (i = 0; i < n; i++) { 
a[i] = reader.nextInt(); 
}
//以上是输入整个数组
max = 0; 
min = 0; 
//设置两个标志,开始都指向第一个数
for (i = 1; i < n; i++) { 
if (a[i] > a[max]) 
max = i; //遍历数组,如果大于a[max],就把他的数组下标赋给max
if (a[i] < a[min]) 
min = i; //同上,如果小于a[min],就把他的数组下标赋给min
} 
//以上for循环找到最大值和最小值,max是最大值的下标,min是最小值的下标
temp1 = a[0]; 
temp2 = a[max]; //这两个temp只是为了在交换时使用

a[0] = a[min]; 
a[min] = temp1; //首先交换a[0]和最大值a[max]

if (max != 0) { //如果最小值不是a[0],执行下面
a[max] = a[n - 1]; 
a[n - 1] = temp2; //交换a[min]和a[n-1]
} else {       //如果最小值是a[0],执行下面
a[min] = a[n - 1]; 
a[n - 1] = temp1; 
} 
for (i = 0; i < n; i++) { //输出数组
System.out.print(a[i] + " "); 
} 

} 

}

 

 

 

 

 

 

第二题   本题要求编写程序,将一个给定的整数插到原本有序的整数序列中,使结果序列仍然有序。

 先把整个数组的长度加一

然后将数字插入

最后更改数组的下标

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner reader=new Scanner(System.in );
        int N=reader.nextInt();
        int a[]=new int[N+1];
        for(int i=0;i<N;i++){
            a[i]=reader.nextInt();
        }
        a[N]=reader.nextInt();
        Arrays.sort(a);
        for(int i=0;i<N+1;i++){
            System.out.print(a[i]+" ");
        }
    }
}

 

第三题 IP地址转换

 一开始做这道题总是无法输出

由于没有将每八位分段无法一起输出

后来先将二进制每八位分段在进行转换十进制

输出带.点的格式

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner reader=new Scanner(System.in );
        String N=reader.nextLine();
        String a=N.substring(0,8);
        String b=N.substring(8,16);
        String c=N.substring(16,24);
        String d=N.substring(24,32);
        int a1=Integer.parseInt(a,2);
        int b1=Integer.parseInt(b,2);
        int c1=Integer.parseInt(c,2);
        int d1=Integer.parseInt(d,2);
        System.out.print(a1+"."+b1+"."+c1+"."+d1);
    }
}

第四题  说反话-加强版 

import java.util.Scanner;  
public class Main {  
  public static void main(String[] args) {  
    Scanner scanner = new Scanner(System.in 

);  
    String str = scanner.nextLine().trim();  
    String[] strs = str.split(" +");  
    for(int i=strs.length-1;i>=0;--i)  
    {  

        System.out.print(strs[i]);  
        if(i!=0)  
          System.out.print(" ");
    }  

  }  
}

 

学习内容 代码行数 博客字数
第三次过程学考核 90 70
     
posted @ 2018-11-03 18:22  huangssss  阅读(249)  评论(1编辑  收藏  举报