旅游规划-Java

改编自http://blog.csdn.net/Phenixfate/article/details/42749613?locationNum=4


墨灿游戏笔试 17.09.06    都是PAT里的题  ,前边的函数题,C++语法忘记好多了啊,编译都错误

编程题3 

输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0~(N-1);M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中间用空格分开,数字均为整数且不超过500。输入保证解的存在
在一行里输出路径的长度和收费总额,数字间以空格分隔,输出结尾不能有多余空格。
输入样例:


4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
输出样例:


3 40
作者: 陈越
单位: 浙江大学
时间限制: 400ms
内存限制: 64MB


import java.util.Scanner;

public class hanshu {
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int N=scan.nextInt();
		int M=scan.nextInt();
		int S=scan.nextInt();
		int D=scan.nextInt(); 
		final int MaxNum=10000;
	    //用邻接矩阵存储图   
	    int[][] len = new int[N][N];                    //储存公路长度  
	    int[][] cost = new int [N][N];                      //储存费用  
		               
	    //初始化  
	    for(int i=0;i<N;i++)  
	    {  
	        for(int j=0;j<N;j++)  
	        {  
	            len[i][j] = MaxNum;  
	            cost[i][j] = MaxNum;  
	        }  
	    }   
	    //构建邻接矩阵,处理输入数据  
	    for(int i=0;i<M;i++)  
	    {  
	    	int c1=scan.nextInt();
			int c2=scan.nextInt();
			int l=scan.nextInt();
			int c=scan.nextInt(); 
	          
	          
	        len[c1][c2] = l;  
	        len[c2][c1] = l;  
	        cost[c1][c2] = c;  
	        cost[c2][c1] = c;  
	    }   
		
	    //Dijastra算法开始  
	    int []dist = new int[N];                     //记录当前路径长度  
	    int []acost = new int[N];                    //记录当前花费  
	    //初始化  
	    for(int i=0;i<N;i++)  
	    {  
	          
	        	dist[i] = MaxNum;  
	            acost[i] = MaxNum;  
	          
	    }    
	    dist[S] = 0;  
	    acost[S] = 0;  
		
	    //进行算法  
	    for(int k=0;k<2;k++)  
	    {  
	        for(int v=0;v<N;v++)  
	        {  
	            for(int w=0;w<N;w++)  
	            {  
	                if(dist[v] != MaxNum)  
	                {  
	                    if(dist[v]+len[v][w] < dist[w])  
	                        dist[w] = dist[v] + len[v][w];                        
	                    else if(dist[v] + len[v][w] == dist[w] && acost[v] != MaxNum && acost[v]+cost[v][w] <acost[w])  
	                        acost[w] = acost[v] + cost[v][w];                             
	                }  
	            }                                     
	        }                                         
	    }
	    System.out.println(dist[D]+" "+acost[D]);	     
	}
}


编程题2

输出GPLT(20 分)
给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符。当然,四种字符(不区分大小写)的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按GPLT的顺序打印,直到所有字符都被输出。

太尴尬了....我居然心急没有看清题目,哭瞎,怪不得解不出来

import java.util.Scanner;
import java.util.Stack;

public class Main {
	public static void main(String[] args){
		Scanner scan=new Scanner(System.in);
		//用regex只留下这四个数
		String str=scan.nextLine().replaceAll("[^gpltGPLT]", "").toUpperCase();
		String s ="";
		//4个数的数组,存储每个字符出现次数
		//int b[]={'G','P','L','T'};
		int a[]=new int[4];
		for (int i = 0; i < str.length(); i++) {
			switch(str.charAt(i)){
			case 'G':
				a[0]++;
				break;
			case 'P':
				a[1]++;
				break;
			case 'L':
				a[2]++;
				break;
			case 'T':
				a[3]++;
				break;
			}
		}
		//System.out.println(a[0]+" "+a[1]+" "+a[2]+" "+a[3]);
		//输出
		for (int i = 0; i < Math.max(Math.max(a[0], a[1]), Math.max(a[2], a[3])); i++) {
			
			if(i<a[0]){
				s+="G";	
			}if(i<a[1]){
				s+="P";
			}if(i<a[2]){
				s+="L";
			}if(i<a[3]){
				s+="T";
			}
		}
		System.out.println(s);
	}
	
}

编程题1 划拳

划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒.........

输入:

1 1
6
8 10 9 12
5 10 5 10
3 8 5 12
12 18 1 13
4 16 12 15
15 1 1 16

输出:

A

1


不知道哪里错了呀,本地IDE是对的,结果交卷是答案错误,难道是我调试好了之后没有复制进去吗    难道我审题漏了几句话?天哪,我的工作啊.....

import java.util.Scanner;
import java.util.Stack;

public class Main {
	public static void main(String[] args){
		Scanner scan=new Scanner(System.in);
		int jiuA=scan.nextInt();
		int jiuB=scan.nextInt();
		int N=scan.nextInt();
		int hanA,huaA, hanB,huaB;
		int numA=0;
		int numB=0;
		
		//Lun a[]=new Lun[N];
		for (int i = 0; i < N; i++) {
			hanA=scan.nextInt();
			huaA=scan.nextInt();
			hanB=scan.nextInt();
			huaB=scan.nextInt();
			if(huaA==(hanA+hanB)){
				numA++;
				if(numA==jiuA){
					System.out.println("A");
					System.out.println(numA);
					return;
				}
					
			}else if(huaB==(hanA+hanB)){
				numB++;
				if(numB==jiuB){
					System.out.println("B");
					System.out.println(numB);
					return;
				}
			}
		}
	}
}



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值