期末考试试题-快速排序与输入年份输出星期数Java程序备份

import java.util.Scanner;

public class swap {

public static int dealLength;

static int[] dealArrayInt;

/*

* 我在写程序时候发现需要有两个全局变量,所以不假思索就添加上了

* 编写完后想看看能不能不用全局

* 可惜,没有发现不用全局能做出来的方法

* 下面是主函数

*/

public static void main(String[]args){

Scanner scan = new Scanner(System.in); 

String dealing = scan.next();  //输入dealing 待处理内容

char[] dealArray = dealing.toCharArray();

dealLength = dealing.length();

int dealBegin = 0;

int dealEnd = dealLength – 1;

int[] dealArrayInt = new int[dealLength];

//各种变量设置……都是临时加上去的,发现变量不够

for(int i=0;i <dealLength;i++){

dealArrayInt[i] =dealArray[i];

}

quickSort(dealArrayInt,dealBegin,dealEnd);

for(int i = dealLength – 1;i >= 0; i –)

        {

dealArray[i] =(char) dealArrayInt[i];

            System.out.print(dealArray[i]);

            System.out.print(" ");

        }

}

/*

* 上面是主函数

* 下面通通为我们的快速排序算法Java实现

*/

public static void quickSort( int[] dealArray , int begin , int end ){ 

if(begin < end){ 

int Position = Partition(dealArray,begin,end); 

quickSort( dealArray , begin , Position – 1); 

quickSort( dealArray , Position + 1 , end ); 

        } 

}

private static int Partition( int[] dealArray , int begin , int end ){

int transit = dealArray[begin];

while(begin < end){ 

while(end > begin && compare( transit , dealArray[end] ) <= 0 ){

end — ; 

dealArray[begin] = dealArray[end] ; 

while(begin < end && compare( transit , dealArray[begin] ) >= 0){ 

begin ++ ; 

dealArray[end] = dealArray[begin]; 

dealArray[begin] = transit ; 

return begin; 

private static int compare(int begin, int end) {

return begin – end ; 

/*

* 快速排序算法结束

*/

}

 

 

 

import java.util.Scanner;  //加入输入包

public class enterdatatime{

    public static void main(String args[]) {
       
        System.out.println("请输入您所要查询的日期格式如:20120608,输入完成后按回车得到结果");  //提示
        System.out.print("您输入的数据为:");
        

        Scanner dt = new Scanner(System.in); 
        String datatime = dt.next();  //输入dt的时间

        String year;
        String moon;
        String day;
        String center;
       
       
        center = datatime.substring(0,2); //截取字符串为center
        year = datatime.substring(2,4);   //截取字符串为year
        moon = datatime.substring(4,6);   //截取字符串为moon
        day = datatime.substring(6,8);    //截取字符串为day
       
        int preweek;  //设置中转变量
        int week;     //设置最终输出变量
        preweek = 5 – Integer.parseInt(center) + Integer.parseInt(year) + Integer.parseInt(year) / 4 + (13*Integer.parseInt(moon + 1) / 5) + Integer.parseInt(day) – 1 + 700;
        /*
        根据蔡勒公式进行运算
        使用Integer.parseInt()进行字符串到整形的转换
        +700避免负数的出现
        **/
        week = preweek%7 + 1;
       
        //System.out.println(Integer.parseInt(year));
       
        System.out.println("您好,您所查询的日期为星期"+week);
       
        }
               
    }

}