期末考试试题-快速排序与输入年份输出星期数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);
       
        }
               
    }

}

三个商人和三个随从的Java程序实现与分析非MatlAB

三个商人和三个随从的Java实现与分析

Java程序如下:

import java.util.*;
public class Test4{
/**内部类,用来把保存过河过程中的路线和状态.
* 五个属性:
* to为ture表示去对岸,否则表示从对岸返回。同时true也表示船在本岸,false表示船在对岸.
* busiMansInBoat为在船里的商人数.
* servsInBoat为船里的仆人数.
* residualBusiMans为岸上的商人数,to为true时,表示是本岸.
* residualServants为岸上的仆人数,to为true时,表示是本岸.
*/
static class Path{
boolean to;
int busiMansInBoat;
int servsInBoat;
int residualBusiMans;
int residualServants;
Path(boolean to,int bmib,int sib,int rb,int rs){
this.to=to;
busiMansInBoat=bmib;
servsInBoat=sib;
residualBusiMans=rb;
residualServants=rs;
}
/*比较两个状态是不是相同.
*/
public boolean equals(Path p){
if(to==p.to){
return busiMansInBoat==p.busiMansInBoat&&servsInBoat==p.servsInBoat
&&residualBusiMans==p.residualBusiMans&&residualServants==p.residualServants;
}else if(to){
return busiMansInBoat==p.busiMansInBoat&&servsInBoat==p.servsInBoat
&&residualBusiMans==3-p.residualBusiMans-p.busiMansInBoat&&residualServants==3-p.residualServants-p.servsInBoat;
}else{
return busiMansInBoat==p.busiMansInBoat&&servsInBoat==p.servsInBoat
&&3-residualBusiMans-busiMansInBoat==p.residualBusiMans&&3-residualServants-servsInBoat==p.residualServants;
}
}
public String toString(){
if(to){
return "本岸(商人"+residualBusiMans+"个,仆人"+residualServants+"个)—–>" 
+"[船上:"+busiMansInBoat+"个商人,"+servsInBoat+"个仆人]—–>" 
+"对岸(商人"+(3-residualBusiMans-busiMansInBoat)+"个,仆人"+(3-residualServants-servsInBoat)+"个)";
}else{
return "本岸(商人"+(3-residualBusiMans-busiMansInBoat)+"个,仆人"+(3-residualServants-servsInBoat)+"个)<—–" 
+"[船上:"+busiMansInBoat+"个商人,"+servsInBoat+"个仆人]<—–" 
+"对岸(商人"+residualBusiMans+"个,仆人"+residualServants+"个)";
}
}
}
//保存路线的List 
public static List<Path> pathList=new ArrayList<Path>();
/**过河方法.
* @param businessmans 商人数.
* @param servants 仆人数.
* @param schemes 方案集合.
* @param to 去还是返回,同时也表示是否在对岸.
*
*/
public static void passRiver(int businessmans,int servants,int[][] schemes,boolean to){
if(!to&&businessmans==3&&servants==3){
System.out.println("结果");
for(Path p : pathList){
System.out.println(p);
}
return;
}
int schemesStart=0; //从方案集中的那一种方案开始选择. 
if(to){
schemesStart=2; //如果在本岸,则从第3种方案开始选择. 
}
for(int i=schemesStart;i<schemes.length;i++){
int residualBusiMans=businessmans-schemes[i][0]; //按照方案岸上所剩商人数. 
int residualServants=servants-schemes[i][1]; //按照方案岸上所剩仆人数. 
if(residualBusiMans<0||residualServants<0){ //如果商人数或仆人数小于0,重新选择方案. 
continue;
}
if(residualBusiMans!=0&&residualBusiMans<residualServants){ //如果仆人数大于商人数,重新选择方案. 
continue;
}
if(3-residualBusiMans!=0&&3-residualBusiMans<3-residualServants){ //如果对岸的仆人数大于商人数,重新选择方案.
continue;
}
//按本方案,生成路线. 
Path p=new Path(to,schemes[i][0],schemes[i][1],residualBusiMans,residualServants);
if(!isRepeat(p)){ //如果没有重复. 
pathList.add(p);
passRiver(3-residualBusiMans,3-residualServants,schemes,!to); //从对岸返回. 
pathList.remove(pathList.size()-1); 
}
}
}
/**看看当前的路线是不是和以前的重复了.
*/
public static boolean isRepeat(Path p){
boolean repeat=false;
for(Path temp : pathList){
if(temp.equals(p)){
repeat=true;
break;
}
}
return repeat;
}
public static void main(String[] args)throws Exception{
//过河方案,每一种方案为一个一组数组,比如{1,0}表示船上要坐1个商人和0个仆人。方案可以用一个方法来生成: 
int[][] sckemes={{1,0},{0,1},{1,1},{2,0},{0,2}};
passRiver(3,3,sckemes,true);
}
}

输出结果为:

Continue reading

Java程序员的前景如何

Java的相关方面Quicl之前找过许多的资料,不嫌弃的话大家可以在最上面的编程—>Java中看到。我感觉Java的话普遍薪水不是很高,而基于JVM的另外一种语言Scala是非常优秀的人群在使用。小的说,在中国投行中做数据后台都是Scala在做,用Ruby的现在也向Scala转移,年薪很高哦。大点说,包括世界顶级投行,美国那边更是由Twitter掀起来使用Scala进行大数据处理的风潮。不过对于我们程序员来说,Scala的数学不是难点……难点在于许多的金融方面的知识欠缺……各位找个学金融的好搭档现在开始学习吧!

ACM中为何不选择Java

ACM大多数同学还是在OJ上面刷题,大部分都用的是C语言。可是大家知道ACM为什么不用Java、Python等语言呢?

实际上,OJ是支持这些语言的。在中国大学,几乎每个大学生接触到的第一门编程语言就是C语言了,比如我所在的西安工业大学 计算机系就是这样的。OJ由于服务器的局限性,以及C语言无可比拟的底层性高灵活性使得OJ中以C为主。

随着代码规模的扩大,C不能满足同学们对高效编码的需求使得C++这个面向对象的语言在ACM中广为使用。不论ACM采用什么语言,C++是不可争辩的优秀语言之一。他的方便快捷的开发属性、不失底层语言的特征使得这个外人所称“不伦不类”的语言真的在对时间、空间等要求高的场合应用。ACM是个拼时间、拼效率的赛事。

C++的底层性使得ACM比赛选手可以更大程度的优化代码效率,C++的面向对象特征使得参赛选手更快的完成刷题任务。

不过C++最近今年受到了Java的挑战,Oracle接手Sun的JDK后,发布的JDK7使得其Java代码执行效率大大提高,有媲美C++等直接底层源码的优势。可是,媲美归媲美,性能还是差那么一点点的。

由于Java需要JDK的支持,ACM中许多OJ并没有安装最新版本的JDK,使得OJ中Java的效率总是很低。可是这个情况即将大为改观,作为云计算、物联网的先头兵,Java以其无可比拟的高效开发占据了软件行业的半壁江山。由于搞ACM的大多还要进入社会求职的,所以Java的编码在OJ的刷题工程中有上升趋势。

ACM为何不选择Java?这个是个事实,也就是由于之前JDK的性能影响,JDK在绝大多数ACMer口中被贬低了许多。ACM为何选择Java?是由于Java本身就是一门优秀的语言,由于ACM普及过程中作用非常大的OJ慢慢更新了JDK,所以Java成为了ACM中的潜力股。本来来源于Quicl’sBlog 转载注明出处链接 http://20xue.com/?p=2502