admin管理员组文章数量:1621658
其他题解收录都在这了👇👇👇
PAT 乙级(Basic Level) Practice 题解合集(全AC版)
目录
1016、部分A+B(分数 15)
1017、A除以B(分数 20)
1018、锤子剪刀布(分数 20)
1019、数字黑洞(分数 20)
1020、月饼(分数 25)
1016、部分A+B(分数 15)
题目描述:
代码实现:
import java.util.Scanner;
public class Main{
public static int getP(int n, int count){
if(count == 0) return 0;
int res = 0;
while(count != 0){
res = res * 10 + n;
count --;
}
return res;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String A = in.next();
int DA = in.nextInt();
String B = in.next();
int DB = in.nextInt();
int countA = 0, countB = 0;
for(int i = 0;i < A.length();i ++){
if(A.charAt(i) == (DA + '0')) countA ++;
}
for(int i = 0;i < B.length();i ++){
if(B.charAt(i) == (DB + '0')) countB ++;
}
System.out.println(getP(DA,countA) + getP(DB,countB));
}
}
结果:
1017、A除以B(分数 20)
题目描述:
代码实现:
'''
看到大数可以无脑Python或者Java的BigInteger类处理
这题Java似乎会超时(javac)
'''
A, B = map(int, input().split(' '))
print(A // B,A % B)
结果:
1018、锤子剪刀布(分数 20)
题目描述:
代码实现:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
int AWin[3] = {0}; //B0,C1,J2
int BWin[3] = {0};
int win = 0, lose = 0, peace = 0; //对于A
char ways[] = {'B','C','J'};
int N;
char A, B;
cin >> N;
while(N --){
getchar();
scanf("%c %c", &A, &B);
//锤子情况
if(A == 'C' && B == 'C'){
peace ++;
}
else if(A == 'C' && B == 'B'){
lose ++;
BWin[0] ++;
}
else if(A == 'C' && B == 'J'){
win ++;
AWin[1] ++;
}
//布情况
else if(A == 'B' && B == 'C'){
win ++;
AWin[0] ++;
}
else if(A == 'B' && B == 'B'){
peace ++;
}
else if(A == 'B' && B == 'J'){
lose ++;
BWin[2] ++;
}
//剪刀情况
else if(A == 'J' && B == 'C'){
lose ++;
BWin[1] ++;
}
else if(A == 'J' && B == 'B'){
win ++;
AWin[2] ++;
}
else if(A == 'J' && B == 'J'){
peace ++;
}
}
cout << win << " " << peace << " " << lose << endl;
cout << lose << " " << peace << " " << win << endl;
if(win == lose){
cout << "B B" << endl;
}
else{
int max1 = 0, max2 = 0;
for(int i = 0;i < 3;i ++){
if(AWin[i] > AWin[max1]) max1 = i;
if(BWin[i] > BWin[max2]) max2 = i;
}
cout << ways[max1] << " ";
cout << ways[max2] << endl;
}
return 0;
}
结果:
1019、数字黑洞(分数 20)
题目描述:
代码实现:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
br.close();
int[] nums = new int[4];
int res = 0;
if (N % 1111 == 0){
System.out.println(N + " - " + N + " = " + "0000");
return;
}
do{
int best = 0, worst = 0;
int p2 = 1000, p1 = 1;
nums[0] = N / 1000;
nums[1] = (N % 1000) / 100;
nums[2] = (N % 100) / 10;
nums[3] = N % 10;
Arrays.sort(nums);
for(int i = 0;i < 4;i ++){
best += nums[i] * p1;
worst += nums[i] * p2;
p1 *= 10;
p2 /= 10;
}
res = best - worst;
System.out.printf("%04d - %04d = %04d\n", best, worst, res);
if(res != 6767) N = res;
}while (res != 6174);
}
}
结果:
1020、月饼(分数 25)
题目描述:
代码实现:
import java.io.*;
import java.util.*;
class MoonCake{
public double storage;
public double price;
public double perPrice;
}
public class Main{
public static void main(String[] args) throws IOException{
//BufferedReader 这个流实在太长了 读个数据是真累啊!!T_T
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] info1 = bf.readLine().split(" ");
String[] info2 = bf.readLine().split(" ");
String[] info3 = bf.readLine().split(" ");
bf.close();
int N = Integer.parseInt(info1[0]);
int D = Integer.parseInt(info1[1]);
MoonCake[] cakes = new MoonCake[N];
double sell = 0.0;
for(int i =0;i < N;i ++){
cakes[i] = new MoonCake();
cakes[i].storage = Double.parseDouble(info2[i]);
}
for(int i =0;i < N;i ++){
cakes[i].price = Double.parseDouble(info3[i]);
cakes[i].perPrice = cakes[i].price / cakes[i].storage;
}
Arrays.sort(cakes, new Comparator<>(){
@Override
public int compare(MoonCake a, MoonCake b){
if(a.perPrice == b.perPrice) return 0;
else if(a.perPrice > b.perPrice) return -1;
else return 1;
}
});
for(int i = 0;i < N;i ++){
if(cakes[i].storage <= D){
sell += cakes[i].price; //月饼[i]可以全部供给
D -= cakes[i].storage;
}
else{ //月饼[i]供给D部分
sell += D * cakes[i].perPrice;
break;
}
}
System.out.printf("%.2f\n", sell);
}
}
结果:
版权声明:本文标题:浙大PAT 乙级(题号1016~1020)自解全AC Java | Python | C++实现 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1728850034a1176557.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论