您的位置:首页 > 编程语言 > Java开发

牛客网PAT乙级真题及java实现样例(真题1-14)

2015-12-27 14:20 447 查看
由于之前在PAT官网的练习场有时间限制,而且限制是针对c的基本上100ms到400ms,由于java需要虚拟机才能运行,因此有些题实在不是因为算法的原因才超时,牛客网的时间限制为1s,而且给我感觉后台服务器也比PAT官网跑的快,一样的程序PAT官网要80ms,牛客网只要20ms,因此我现在把代码提交到了牛客网。下面是牛客网乙级真题的题目和我实现的方法。

1001.A+B和C

题目描述

给定区间[-2的31次方, 2的31次方]内的3个整数A、B和C,请判断A+B是否大于C。

输入描述:

输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。

输出描述:

对每组测试用例,在一行中输出“Case #X: true”如果A+B>C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)。

输入例子:

4

1 2 3

2 3 4

2147483647 0 2147483646

0 -2147483648 -2147483647

输出例子:

Case #1: false

Case #2: true

Case #3: true

Case #4: false

注:这个题目没什么难度,就是考虑整数会超过int长度,可以选择Long类型。代码如下所示:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.*;
/**
*
* @author zjb
*/
public class Main {
public static void main(String[] args){
//System.out.print(Integer.SIZE);
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
sc.nextLine();
List<String> list = new ArrayList();
for(int i=0; i<T; i++){
list.add(sc.nextLine());
}
for(int i=0; i<T; i++){
String[] strarr = list.get(i).split(" ");
long A = Long.parseLong(strarr[0]);
long B = Long.parseLong(strarr[1]);
long C = Long.parseLong(strarr[2]);
//System.out.print(A+" "+B+" "+C+" ");
if(A+B > C)
System.out.println("Case #"+(i+1)+": true");
else
System.out.println("Case #"+(i+1)+": false");
}
}
}
1002.数字分类

题目描述

给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:

A1 = 能被5整除的数字中所有偶数的和;

A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;

A3 = 被5除后余2的数字的个数;

A4 = 被5除后余3的数字的平均数,精确到小数点后1位;

A5 = 被5除后余4的数字中最大数字。

输入描述:

每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的正整数。数字间以空格分隔。

输出描述:

对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

若其中某一类数字不存在,则在相应位置输出“N”。

输入例子:

13 1 2 3 4 5 6 7 8 9 10 20 16 18

输出例子:

30 11 2 9.7 9

注:这个题目也没什么难度就是计算的时候可以考虑一些重复计算的东西,尽量程序中不要出现重复计算的代码。实现代码如下:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.Scanner;

/**
*
* @author zjb
*/
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int A1 = 0;
int A2 = 0;
int flag = 1;
int numA2 = 0;
int A3 = 0;
double A4 = 0;
int numA4 = 0;
//int[] arrA5 = new int[1000];
int A5 = 0;

int index = 0;
int num;
boolean flagA5 = false;
for(int i=0; i<N; i++){
num = sc.nextInt();
if(num%10 == 0)
A1 += num;
else if(num%5 ==1){
A2 = flag*num + A2;
flag *= -1;
numA2++;
}
else if(num%5 == 2 )
A3++;
else if(num%5 == 3 ){
numA4++;
A4 += num;
}
else if(num%5 == 4 )
if(!flagA5){
A5 = num;
flagA5 = true;
}
else
if(num >= A5)
A5 = num;
}

if(A1>0)
System.out.print(A1);
else
System.out.print("N");
if(numA2>0)
System.out.print(" "+A2);
else
System.out.print(" N");
if(A3>0)
System.out.print(" "+A3);
else
System.out.print(" N");
if(numA4 >0){
A4 = A4/numA4;
System.out.printf(" %.1f",A4);
}
else
System.out.print(" N");
if(flagA5){
System.out.print(" "+A5);
}
else
System.out.print(" N");
}
}
1003.数素数

题目描述

令Pi表示第i个素数。现任给两个正整数M <= N <= 10000,请输出PM到PN的所有素数。

输入描述:

输入在一行中给出M和N,其间以空格分隔。

输出描述:

输出从PM到PN的所有素数,每10个数字占1行,其间以空格分隔,但行末不得有多余空格。

输入例子:

5 27

输出例子:

11 13 17 19 23 29 31 37 41 43

47 53 59 61 67 71 73 79 83 89

97 101 103

注:这个题目需要注意的地方是在判断素数的时候只要从2开始到sqrt(m)。如果从2到m-1的话,就可能造成超时。代码如下:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author zjb
*/
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int M = sc.nextInt();
int N = sc.nextInt();
int i=1;
int numPrime = 0;
int numPrint = 0;
while(true){
/* if(i>2)
i=i+2;
else*/
i++;
if(isPrime(i)){
numPrime++;
if(numPrime>=M && numPrime<=N){
numPrint ++;
if(numPrint%10 == 1)
System.out.print(i);
else if(numPrint%10 == 0)
System.out.println(" "+i);
else
System.out.print(" "+i);
}
}
if(numPrime > N)
break;

}
}
public static boolean isPrime(int num){
if(num <2)
return false;
if(num ==2)
return true;
for(int i=2; i<Math.sqrt(num)+1; i++){
if(num%i == 0)
return false;
}
return true;
}
}
1004.福尔摩斯的约会

题目描述

大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”。大侦探很

快就明白了,字条上奇怪的乱码实际上就是约会的时间“星期四 14:04”,因为前面两字符串中第1对相同的大写英文字母(大小写有区分)是

第4个字母'D',代表星期四;第2对相同的字符是'E',那是第5个英文字母,代表一天里的第14个钟头(于是一天的0点到23点由数字0到9、

以及大写字母A到N表示);后面两字符串第1对相同的英文字母's'出现在第4个位置(从0开始计数)上,代表第4分钟。现给定两对字符串,

请帮助福尔摩斯解码得到约会的时间。

输入描述:

输入在4行中分别给出4个非空、不包含空格、且长度不超过60的字符串。

输出描述:

在一行中输出约会的时间,格式为“DAY HH:MM”,其中“DAY”是某星期的3字符缩写,即MON表示星期一,TUE表示星期二,WED表示星期三,THU表示星期

四,FRI表示星期五,SAT表示星期六,SUN表示星期日。题目输入保证每个测试存在唯一解。

输入例子:

3485djDkxh4hhGE

2984akDfkkkkggEdsb

s&hgsfdk

d&Hyscvnm

输出例子:

THU 14:04

注:这个题目需要注意的是当小时和分钟小于10的时候要在前面输出0。代码如下所示:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author zjb
*/
public class Main {
public static void main(String agrs[]){
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();
String str3 = sc.nextLine();
String str4 = sc.nextLine();
char char1 = '0';
char char2 = '0';
int charnum = 0;
for(int i=0; (i<Math.min(str1.length(), str2.length())&&charnum<2); i++){
char ch = str1.charAt(i);
if(charnum == 0 && ch==str2.charAt(i) && ch>='A'&&ch<='G'){
char1 = ch;
charnum++;
continue;
}
if(charnum == 1&&(ch==str2.charAt(i))
&&(ch>='A'&&ch<='N'||ch>='0'&&ch<='9')){
char2 = ch;
charnum++;
continue;
}
}
int index = 0;
for(int i=0; i<Math.min(str3.length(), str4.length());i++){
int ch = str3.charAt(i);
if(ch == str4.charAt(i)&&(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')){
index = i;
break;
}
}
String out = new String();
switch(char1){
case 'A':
out += "MON ";
break;
case 'B':
out += "TUE ";
break;
case 'C':
out += "WED ";
break;
case 'D':
out += "THU ";
break;
case 'E':
out += "FRI ";
break;
case 'F':
out += "SAT ";
break;
case 'G':
out += "SUN ";
break;
default:
break;
}
if(char2<='9')
out += "0"+char2;
else
out += char2 - 'A' +10;
if(index<10)
out +=  ":0"+index;
else
out += ":"+index;
System.out.println(out);
}
}


1005.德才论

题目描述

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之

小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入描述:

输入第1行给出3个正整数,分别为:N(<=105),即考生总数;L(>=60),为录取最低分数线,即德分和才分均不低于L的考生才有资格

被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到

但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于H,但是德分不低于才分的考生属于“才德兼

亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线L的考生也按总分排序,但排在第三类考生之后。

随后N行,每行给出一位考生的信息,包括:准考证号、德分、才分,其中准考证号为8位整数,德才分为区间[0, 100]内的整数。数字间以空格分隔。

输出描述:

输出第1行首先给出达到最低分数线的考生人数M,随后M行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人

总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入例子:

14 60 80

10000001 64 90

10000002 90 60

10000011 85 80

10000003 85 80

10000004 80 85

10000005 82 77

10000006 83 76

10000007 90 78

10000008 75 79

10000009 59 90

10000010 88 45

10000012 80 100

10000013 90 99

10000014 66 60

输出例子:

12

10000013 90 99

10000012 80 100

10000003 85 80

10000011 85 80

10000004 80 85

10000007 90 78

10000006 83 76

10000005 82 77

10000002 90 60

10000014 66 60

10000008 75 79

10000001 64 90

注:这个题目关键是排序,排序的时候自己实现comparator接口需要使用内部类。代码如下

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author zjb
*/
public class Main{
public static class CMP implements Comparator<String>{
public int compare(String str1,String str2){
String[] strarr1 = str1.split(" ");
String[] strarr2 = str2.split(" ");
if(Integer.parseInt(strarr1[1])+Integer.parseInt(strarr1[2]) >
Integer.parseInt(strarr2[1])+Integer.parseInt(strarr2[2]))
return -1;
else if(Integer.parseInt(strarr1[1])+Integer.parseInt(strarr1[2]) <
Integer.parseInt(strarr2[1])+Integer.parseInt(strarr2[2]))
return 1;
else {
if(Integer.parseInt(strarr1[1]) > Integer.parseInt(strarr2[1]))
return -1;
else if(Integer.parseInt(strarr1[1]) < Integer.parseInt(strarr2[1]))
return 1;
else{
if(Integer.parseInt(strarr1[0]) > Integer.parseInt(strarr2[0]))
return 1;
else
return -1;
}
}
}
}

public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int L = sc.nextInt();
int H = sc.nextInt();
//List<String> list = new ArrayList();
List<String> caideqj = new ArrayList();
List<String> deshengc = new ArrayList();
List<String> caidejw = new ArrayList();
List<String> other = new ArrayList();
sc.nextLine();
for(int i=0; i<N; i++){
String str  = sc.nextLine();
String[] strarr = str.split(" ");
if(Integer.parseInt(strarr[1])>=L && Integer.parseInt(strarr[2])>=L){
if(Integer.parseInt(strarr[1])>=H && Integer.parseInt(strarr[2])>=H){
caideqj.add(str);
}
else if(Integer.parseInt(strarr[1])>=H && Integer.parseInt(strarr[2])<H){
deshengc.add(str);
}
else if(Integer.parseInt(strarr[1])<H && Integer.parseInt(strarr[2])<H
&& Integer.parseInt(strarr[1])>=Integer.parseInt(strarr[2])){
caidejw.add(str);
}
else{
other.add(str);
}
}
}
System.out.println(caideqj.size()+deshengc.size()+caidejw.size()+other.size());
CMP cmp = new CMP();
if(caideqj.size()>1)
Collections.sort(caideqj, cmp);
if(deshengc.size()>1)
Collections.sort(deshengc, cmp);
if(caidejw.size()>1)
Collections.sort(caidejw, cmp);
if(other.size()>1)
Collections.sort(other, cmp);
for(int i=0; i<caideqj.size(); i++){
System.out.println(caideqj.get(i));
}
for(int i=0; i<deshengc.size(); i++){
System.out.println(deshengc.get(i));
}
for(int i=0; i<caide
fe20
jw.size(); i++){
System.out.println(caidejw.get(i));
}
for(int i=0; i<other.size(); i++){
System.out.println(other.get(i));
}

}
}

1006.部分A+B

题目描述

正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA。例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6。

现给定A、DA、B、DB,请编写程序计算PA + PB。

输入描述:

输入在一行中依次给出A、DA、B、DB,中间以空格分隔,其中0 < A, B < 1010。

输出描述:

在一行中输出PA + PB的值。

输入例子:

3862767 6 13530293 3

输出例子:

399

注:这个题目很简单没有什么需要注意的点,代码如下:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.*;
/**
*
* @author zjb
*/
public class Main {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
int DA = sc.nextInt();
int B = sc.nextInt();
int DB = sc.nextInt();
int PA = 0;
int PB = 0;
while(A>0){
int num = A%10;
if(num == DA)
PA = PA*10 + DA;
A = A/10;
}
while(B>0){
int num = B%10;
if(num == DB)
PB = PB*10 + DB;
B = B/10;
}
System.out.println(PA+PB);

}
}
1007.A除以B

题目描述

本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数。你需要输出商数Q和余数R,使得A = B * Q + R成立。

输入描述:

输入在1行中依次给出A和B,中间以1空格分隔。

输出描述:

在1行中依次输出Q和R,中间以1空格分隔。

输入例子:

123456789050987654321 7

输出例子:

17636684150141093474 3

注:由于题目要求的是不超过1000位的正整数,因此利用数据类型接收输入肯定是不行的,这个时候需要利用整形数组接收输入,然后利用除法的基本原理求解,代码如下:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.*;
/**
*
* @author zjb
*/
public class Main {
public static void main(String[] args){
List<Integer> list = new ArrayList();
Scanner sc = new Scanner(System.in);
String strA = sc.next();
int B = sc.nextInt();
int temp = 0;
for(int i=0; i<strA.length(); i++){
temp = temp*10 + strA.charAt(i)-'0';
if(temp < B)
list.add(0);
else{
list.add(temp/B);
temp = temp % B;
}

}
for(int i=0; i<list.size(); i++){
if(i==0 && list.get(i)==0)
;
else
System.out.print(list.get(i));
}
System.out.println(" "+temp);
}
}
1008.锤子剪刀布

题目描述

大家应该都会玩“锤子剪刀布”的游戏:

现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。

输入描述:

输入第1行给出正整数N(<=105),即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代

表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。

输出描述:

输出第1、2行分别给出甲、乙的胜、平、负次数,数字间以1个空格分隔。第3行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有1个空格。如果解不唯

一,则输出按字母序最小的解。

输入例子:

10

C J

J B

C B

B B

B C

C C

C B

J B

B C

J J

输出例子:

5 3 2

2 3 5

B B

注:这个题目需要注意的是三者之间的关系,还有甲乙的胜负之间的关系,代码如下:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.Scanner;
/**
*
* @author zjb
*/
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.nextLine();
int winCA = 0;
int winCB = 0;
int winJA = 0;
int winJB = 0;
int winBA = 0;
int winBB = 0;
// System.out.println(N);
for(int i=0; i<N; i++){
String str = sc.nextLine();
//System.out.println(str);
//String strB = sc.nextLine();
char chA = str.charAt(0);
char chB = str.charAt(2);
if(chA == 'C'){
if(chB == 'J')
winCA++;
else if(chB == 'B')
winBB++;
}
else if(chA == 'J'){
if(chB == 'B')
winJA++;
else if(chB == 'C')
winCB++;
}
else if(chA == 'B'){
if(chB == 'C')
winBA++;
else if(chB == 'J')
winJB++;
}
}
int totalwinA = winCA+winJA+winBA;
int totalwinB = winCB+winJB+winBB;
int totalping = N-(winCA+winJA+winBA+winCB+winJB+winBB);
System.out.println(totalwinA+" "+totalping+" "+totalwinB);
System.out.println(totalwinB+" "+totalping+" "+totalwinA);
System.out.println(findmax(winCA,winJA,winBA)+" "+findmax(winCB,winJB,winBB));
}
public static char findmax(int winC, int winJ, int winB){
char max = 'B';
int maxnum = winB;
if(maxnum < winC){
max = 'C';
maxnum = winC;
}
if(maxnum < winJ){
max = 'J';
maxnum = winJ;
}

return max;
}
}
1009.数字黑洞

题目描述

给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到

一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的6174,这个神奇的数字也叫Kaprekar常数。

例如,我们从6767开始,将得到

7766 - 6677 = 1089

9810 - 0189 = 9621

9621 - 1269 = 8352

8532 - 2358 = 6174

7641 - 1467 = 6174

... ...

现给定任意4位正整数,请编写程序演示到达黑洞的过程。

输入描述:

输入给出一个(0, 10000)区间内的正整数N。

输出描述:

如果N的4位数字全相等,则在一行内输出“N - N = 0000”;否则将计算的每一步在一行内输出,直到6174作为差出现,输出格式见样例。注意每个数字按4位数格

式输出。

输入例子:

6767

输出例子:

7766 - 6677 = 1089

9810 - 0189 = 9621

9621 - 1269 = 8352

8532 - 2358 = 6174

注:这个题目需要注意的是当输入不足4位的时候需要用0在前面补齐。代码如下:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @author zjb
*/
public class Main {
public static class CMP implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
if(o1 > o2)
return -1;
else if(o1 < o2)
return 1;
else
return 0;
}
}

public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
Integer a[] = new Integer[4];
if(str.length() == 1){
a[0] = a[1] = a[2] = 0;
a[3] = str.charAt(0) - '0';
}
else if(str.length() == 2){
a[0] = a[1] = 0;
a[2] = str.charAt(0) - '0';
a[3] = str.charAt(1) - '0';
}
else if(str.length() == 3){
a[0] = 0;
a[1] = str.charAt(0) - '0';
a[2] = str.charAt(1) - '0';
a[3] = str.charAt(2) - '0';
}

/* else {
if(str.charAt(0) == str.charAt(1) && str.charAt(1)== str.charAt(2)
&& str.charAt(2) == str.charAt(3))
System.out.println(str+" - "+str+" = 0000");*/
else{
for(int i =0; i<4; i++){
a[i] = str.charAt(i) - '0';
}
}
if(a[0] == a[1] && a[2] == a[1] && a[3] == a[2]){
String out = ""+a[0]+a[1]+a[2]+a[3];
System.out.println(out+" - "+out+" = 0000");
}
else{
int num1 = 0;
int num2 = 0;
int differ = 0;
CMP cmp = new CMP();
// Integer[] temp = new Integer[4];
do{
Arrays.sort(a,cmp);
num1 = 1000*a[0] + 100*a[1] + 10*a[2] + a[3];
for(int i=0; i<4; i++){
System.out.print(a[i]);
}
System.out.print(" - ");
Arrays.sort(a);
num2 = 1000*a[0] + 100*a[1] + 10*a[2] + a[3];
for(int i=0; i<4; i++){
System.out.print(a[i]);
}
differ = num1 - num2;
System.out.println(" = "+differ);
a[0] = differ/1000;
differ = differ%1000;
a[1] = differ/100;
differ = differ%100;
a[2] = differ/10;
differ = differ%10;
a[3] = differ;
// Collections.sort(a);
}while((num1 - num2) != 6174);

}

}
}


1010.月饼

题目描述

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需

求量,请你计算可以获得的最大收益是多少。

注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有3种月饼,其库存量分别为18、15、10万吨,总售价分别为75、

72、45亿元。如果市场的最大需求量只有20万吨,那么我们最大收益策略应该是卖出全部15万吨第2种月饼、以及5万吨第3种月饼,获得

72 + 45/2 = 94.5(亿元)。

输入描述:

每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N表示月饼的种类数、以及不超过500(以万吨为单位)的正整数

D表示市场最大需求量。随后一行给出N个正数表示每种月饼的库存量(以万吨为单位);最后一行给出N个正数表示每种月饼的总售价(以亿

元为单位)。数字间以空格分隔。

输出描述:

对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后2位。

输入例子:

3 20

18 15 10

75 72 45

输出例子:

94.50

注:这个题目其实很简单,主要意思就是单价,单价高的卖出的自然越多越好,明白这点题目就很简单了。代码如下:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package com.pat.basiclevel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

/**
*
* @author zjb
*/
public class B1020 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
List<yuebing> list = new ArrayList();
int N = sc.nextInt();
int D = sc.nextInt();
sc.nextLine();
double sum = 0;
String[] arrsize = sc.nextLine().split(" ");
String[] arrtotal = sc.nextLine().split(" ");
for(int i=0; i<N; i++){
yuebing yb = new yuebing();
yb.size = Integer.parseInt(arrsize[i]);
yb.totalprice = Integer.parseInt(arrtotal[i]);
yb.price = yb.totalprice*1.0/yb.size;
list.add(yb);
}
if(N == 1){
if(list.get(0).size <= D)
System.out.printf("%.2f\n",(double)list.get(0).totalprice);
else
System.out.printf("%.2f\n", (double)(list.get(0).price*D));
}
else{
CMP cmp = new CMP();
Collections.sort(list, cmp);
for(int i=0; i<N&&D>0; i++){
if(list.get(i).size <= D){
sum += list.get(i).totalprice;
D = D - list.get(i).size;
}
else{
sum += list.get(i).price*D;
D = 0;
}
}
System.out.printf("%.2f\n",sum);
}
}
private static class yuebing{
public int size;
public int totalprice;
public double price;
}

public static class CMP implements Comparator<yuebing>{
@Override
public int compare(yuebing yb1, yuebing yb2){
if(yb1.price > yb2.price)
return -1;
else if(yb1.price < yb2.price)
return 1;
else
return 0;
}
}

}

1011. 个位数统计

题目描述

给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定N = 100311,则有2个0,3个1,和1个3。

输入描述:

每个输入包含1个测试用例,即一个不超过1000位的正整数N。

输出描述:

对N中每一种不同的个位数字,以D:M的格式在一行中输出该位数字D及其在N中出现的次数M。要求按D的升序输出。

输入例子:

100311

输出例子:

0:2

1:3

3:1

注:这个题目其实很简单,我这里使用了String,所以在运行效率上可能存在点问题,会有点低,如果利用整数除10分解效率还会更高,代码如下:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.Scanner;
/**
*
* @author zjb
*/
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int[] num = new int[10];
for(int i=0; i<str.length();i++){
char ch = str.charAt(i);
switch(ch){
case '0': num[0]++;
break;
case '1': num[1]++;
break;
case '2': num[2]++;
break;
case '3': num[3]++;
break;
case '4': num[4]++;
break;
case '5': num[5]++;
break;
case '6': num[6]++;
break;
case '7': num[7]++;
break;
case '8': num[8]++;
break;
case '9': num[9]++;
break;
}
}
for(int i=0; i<10; i++){
if(num[i] > 0)
System.out.println(i+":"+num[i]);
}
}
}


1012.D进制的A+B

题目描述

输入两个非负10进制整数A和B(<=230-1),输出A+B的D (1 < D <= 10)进制数。

输入描述:

输入在一行中依次给出3个整数A、B和D。

输出描述:

输出A+B的D进制数。

输入例子:

123 456 8

输出例子:

1103

注:这个题目只要按照进制转换的计算过程压入栈就好了。代码如下

import java.util.Scanner;
import java.util.Stack;
/**
*
* @author zjb
*/
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
int B = sc.nextInt();
int D = sc.nextInt();
Stack<Integer> mystack = new Stack();
int num = A + B;
while(num>0){
mystack.push(num%D);
//System.out.println(num%D);
num = num/D;
}
if(mystack.empty())
System.out.println(0);
else{
// System.out.println(mystack.size());
while(!mystack.empty())
System.out.print(mystack.pop());
}
}
}


1013.组个最小数
题目描述

给定数字0-9各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意0不能做首位)。例如:

给定两个0,两个1,三个5,一个8,我们得到的最小的数就是10015558。

现给定数字,请编写程序输出能够组成的最小的数。

输入描述:

每个输入包含1个测试用例。每个测试用例在一行中给出10个非负整数,顺序表示我们拥有数字0、数字1、……数字9的个数。整数间用一个空

格分隔。10个数字的总个数不超过50,且至少拥有1个非0的数字。

输出描述:

在一行中输出能够组成的最小的数。

输入例子:

2 2 0 0 0 3 0 0 1 0

输出例子:

10015558

注:利用一个10个元素的整形数组表示每个数字的个数,然后第一位肯定需要非零的数,之后就按照从小到大输出就好了。代码如下:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.Scanner;

/**
*
* @author zjb
*/
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String out = new String();
int[] num = new int[10];
for(int i=0; i<10; i++){
num[i] = sc.nextInt();
}
for(int i=1; i<10; i++){
if(num[i] > 0){
out += i;
num[i]--;
break;
}
}
for(int i=0; i<10; i++){
while(num[i] > 0){
out += i;
num[i]--;
}
}
System.out.println(out);

}
}


1014.科学计数法

题目描述

科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分

只有1位,小数部分至少有1位,该数字及其指数部分的正负号即使对正数也必定明确给出。

现以科学计数法的格式给出实数A,请编写程序按普通数字表示法输出A,并保证所有有效位都被保留。

输入描述:

每个输入包含1个测试用例,即一个以科学计数法表示的实数A。该数字的存储长度不超过9999字节,且其指数的绝对值不超过9999。

输出描述:

对每个测试用例,在一行中按普通数字表示法输出A,并保证所有有效位都被保留,包括末尾的0。

输入例子:

+1.23400E-03

输出例子:

0.00123400

注:本题主要分三类处理1、在有效数字前面添零,2、在有效数字后面添零,3、只需要移动小数点,不需要添零。代码如下:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
/**
*
* @author zjb
*/
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String strnum = new String();
int num = 0;
int e =0;
//String stre = new String();
String  out = new String();
int index = str.indexOf('E');
String flag = str.substring(0,1);
if(flag.equals("-")){
strnum = str.substring(1,2)+str.substring(3, index);
}
else{
strnum = str.substring(1,2)+str.substring(3, index);
flag = "";
}
num = Integer.parseInt(strnum);
// System.out.println(num);
e = Integer.parseInt(str.substring(index+1));
//System.out.println(e);
//int e = Integer.parseInt(stre);
if(e<0){
out += "0.";
for(int i=0; i<(-e)-1; i++){
out += "0";
}
out += num;
}
else if(e<strnum.length()-3){
out += strnum.substring(1, 2+e);
out +=".";
out += strnum.substring(2+e);
}
else{
out += num;
for(int i=0; i<e+1-strnum.length();i++)
out +="0";
}
System.out.println(flag+out);
//System.out.println(e);
//System.out.println(Double.parseDouble(strnum)*e);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息