概述
1. 编程题1:气温采样
分析:找出区间上重复的点。由于low_i>=-50,由于list索引从0开始。我们先加50,最后再减50即可。
可以用ArrayList,本题用数组也可以。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int [] list = new int [101];
int n = sc.nextInt();
int k = sc.nextInt();
for(int i = 0; i < n; i++) {
int start = sc.nextInt();
int end = sc.nextInt();
for(int j = start; j <= end; j++) {
list[j + 50] ++;
}
}
sc.close();
int minIndex = -1;
int maxIndex = -1;
for(int i = 0; i < list.length; i++) {
if(list[i] >= k) {
minIndex = i - 50;
break;
}
}
for(int i = list.length - 1; i >= 0; i--) {
if(list[i] >= k) {
maxIndex = i - 50;
break;
}
}
if(minIndex == -1)
System.out.println("error");
else
System.out.println(minIndex + " " + maxIndex);
}
2. 编程题2:小熊吃包
分析:中间过程排序以后还要按原顺序排序。
方法1:可以先记录id,再按id排序。
方法2:还可以定义一个排序数组。
方法1:
import java.util.*;
public class PingDuoDuo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
List<Cake> list = new ArrayList<Cake>();
for(int i=1;i<=n;i++){
int deli = sc.nextInt();
int deSpeed = sc.nextInt();
list.add(new Cake(i,deli ,deSpeed));
}
int canEat = list.size();
//测试
// Collections.sort(list, new SortDeliANDSpeed());
// for(Cake i:list){
// System.out.println(i.deli+" "+i.deSpeed);
// }
while(canEat>0){
Collections.sort(list, new SortDeliANDSpeed());
if(list.get(0).deli>0){//可以吃,吃第一个
Cake c = list.get(0);
c.deli=-1;
canEat--;
}
if(list.get(1).deli>0){//可以吃,吃第二个
Cake c = list.get(1);
c.deli=-1;
canEat--;
}
//其他的新鲜度下降
for(int i=0;i<list.size();i++){
if (list.get(i).deli>0) {
int t=list.get(i).deli-list.get(i).deSpeed;
if(t<=0){//不能吃了
list.get(i).deli=0;
canEat--;
}else{
list.get(i).deli=t;
}
}
}
if(canEat<m){
break;
}
}
//按id排序
Collections.sort(list,new SortId());
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).deli);
}
}
}
class Cake{
int id;
int deli;
int deSpeed;
public Cake(int id,int deli,int deSpeed) {
this.id=id;
this.deli=deli;
this.deSpeed=deSpeed;
}
}
class SortId implements Comparator<Cake>{
@Override
public int compare(Cake o1, Cake o2) {
return o1.id - o2.id;
}
}
class SortDeliANDSpeed implements Comparator<Cake>{
@Override
public int compare(Cake o1, Cake o2) {
// TODO Auto-generated method stub
if(o1.deli < o2.deli){
return 1;
}else if(o1.deli > o2.deli){
return -1;
}else{
return o2.deSpeed - o1.deSpeed;
}
}
}
方法2:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main15 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
Bun[] buns = new Bun[n]; //记录了元对象的顺序
ArrayList<Bun> list = new ArrayList();
for(int i=0;i<n;i++){
int deli = in.nextInt();
int speed = in.nextInt();
buns[i] = new Bun(deli,speed);//只是做记录顺序用,因为是引用对象
list.add(buns[i]);
}
Collections.sort(list);
int s = n;
while(s>=m){
int l =2;
while (!list.isEmpty()&&l>0&&list.get(0).deli>0){
list.get(0).eat();
list.remove(0);
l--;
s--;
}
for(Bun bun: list){
if(bun.deli>0){
bun.expired();
if(bun.deli<=0){
s--;
}
}
}
Collections.sort(list);
}
for(Bun bun:buns){
System.out.println(bun.deli);
}
}
static class Bun implements Comparable{
int deli;
int speed;
public Bun(int deli,int speed){
this.deli = deli;
this.speed = speed;
}
@Override
public int compareTo(Object o) {
Bun bun =(Bun)o;
if(this.deli>bun.deli){
return -1;
}else if(this.deli==bun.deli){
if(this.speed>bun.speed){
return -1;
}else if(this.speed==bun.speed){
return 0;
}else{
return 1;
}
}else{
return 1;
}
}
@Override
public String toString() {
return "Bun{" +
"deli=" + deli +
", speed=" + speed +
'}';
}
public void eat(){
this.deli = -1;
}
public void expired(){
int k = this.deli - this.speed;
if(k<0){
this.deli = 0;
}else{
this.deli = k;
}
}
}
}
3. 编程题3:整数序列
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] numbers = new int [n];
for(int i = 0 ; i < n; i++)
numbers[i] = sc.nextInt();
sc.close();
int count = 1;
int max = 1 << n;
Set<SubList> set = new HashSet<SubList>();
while(count < max) {
String bits = Integer.toBinaryString(count);
String key = new StringBuffer(bits).reverse().toString();
List<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < key.length(); i++) {
if(key.charAt(i) == '1')
list.add(numbers[i]);
}
SubList temp = new SubList(list);
if(temp.valid()) {
set.add(temp);
/*for(int i = 0; i < temp.nums.size(); i++) {
System.out.print(temp.nums.get(i) + " ");
}
System.out.println();*/
}
count++;
}
System.out.println(set.size());
}
}
class SubList {
public List<Integer> nums;
public SubList(List<Integer> list) {
this.nums = new ArrayList<Integer>(list);
}
public boolean valid() {
if(nums.size() <= 1)
return false;
for(int i = 1; i < nums.size(); i++) {
int i1 = nums.get(i - 1);
int i2 = nums.get(i);
if(i1 >= i2)
return false;
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((nums == null) ? 0 : nums.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
SubList other = (SubList) obj;
for(int i = 0; i < this.nums.size(); i++) {
if(this.nums.get(i) != other.nums.get(i))
return false;
}
return true;
}
}
最后
以上就是开放咖啡豆为你收集整理的【笔试题】拼多多笔试的全部内容,希望文章能够帮你解决【笔试题】拼多多笔试所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复