第一题
题目描述:牛牛总是睡过头,所以他定了很多闹钟,只有在闹钟响的时候他才会醒过来并决定起不起床。从他起床算起他需要X分钟到达教室,上课时间为当天的A时B分,请问他最晚可以什么时间起床。
输入描述:
- 每个输入包含一个测试用例
- 每个测试用例的第一行包含一个正整数,表示闹钟的数量N(N<=100)
- 接下来的N行每行包含两个整数,表示这个闹钟响起的时间为Mi(0<=A<24)时M1(0<=B<60)分
- 接下来的一行包含一个整数,表示从起床算起他需要X(0<=x<=100)分钟到达教室
- 接下来的一行包含两个整数,表示上课时间为A(0<=A<24)时B(0<=B<60)分
- 数据保证至少有一个闹钟可以让牛牛及时到达教室
输出描述:输出两个整数表示牛牛最晚起床时间
示例:
输入:
3
5 0
6 0
7 0
59
6 59
输出:
6 0
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
int clockCount = scanner.nextInt();
ArrayList<Integer> clock = new ArrayList<>();
while(clockCount-->0){
int h = scanner.nextInt();
int m = scanner.nextInt();
clock.add((h - 0) * 60 + m);
}
int x = scanner.nextInt();
int cH = scanner.nextInt();
int cM = scanner.nextInt();
int classTime = (cH - 0) * 60 + cM;
int latestTime = latest(clock, x, classTime);
int hour = ((classTime - latestTime) / 60 + 24) % 24;
int minuter = (classTime - latestTime) % 60;
System.out.println(hour+" "+minuter);
}
}
public static int latest(ArrayList<Integer> clock, int num, int classTime){
int currentMin =Integer.MAX_VALUE;
for(int time : clock){
if(time <= classTime - num){
currentMin = Math.min(currentMin, classTime - time);
}
else{
currentMin = Math.min(currentMin, 24 * 60 -(classTime - time));
}
}
return currentMin;
}
第二题
题目描述:小Q正在给一条长度为n的道路设计路灯安置方案。为了让问题更简单,小Q把道路视为n个方格,需要照亮的地方用'.'表示,不需要照亮的障碍物格子用'X'表示。小Q现在要在道路上设置一些路灯,对于安置在pos位置的路灯,这盏路灯可以照亮pos-1,pos,pos+1这三个位置。小Q希望能安置尽量少的路灯照亮所有'.'区域,希望你能帮他计算一下最少需要多少盏路灯。
输入描述:
- 输入的第一行包含一个正整数t(1<=t<=1000),表示测试用例数
- 接下来每两行一个测试数据,第一行一个整数n(1<=n<=1000),表示道路的长度,第二行一个字符串s表示道路的构造,只包含'.'和'X'
输出描述:对于每个测试用例,输出一个正整数表示最少需要多少盏路灯
示例:
输入:
2
3
.X.
11
...XX....XX
输出:
1
3
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
int t=scanner.nextInt();
int i = 0;
while(i < t)
{
i++;
int length = scanner.nextInt();
String str = scanner.next();
int k;
int count = 0;
for(k = 0; k < length;)
{
if(str.charAt(k) == '.')
{
count++;
k = k+3;
}
else if(str.charAt(k) == 'X')
{
k++;
}
}
System.out.println(count);
}
}
}
}
第三题
题目描述:牛牛准备参加学校组织的春游,出发前牛牛准备往背包里装入一些零食,牛牛的背包容量为w,牛牛家里共有n袋零食,第i袋零食的体积为v[i]。牛牛想知道在总体积不超过背包容量的情况下,他一共有多少种零食方法(总体积为0也是一种放法)。
输入描述:
- 输入包括两行
- 第一行为两个正整数n和w(1<=n<=30, 1<=w<=2*10^9),表示零食的数量和背包容量
- 第二行n个整数v[i](0<=v[i]<=10^9),表示每袋零食的体积
输出描述:
输出一个整数,表示牛牛一共有多少种零食放法
示例:
输入:
3 10
1 2 4
输出:
8
说明:三种零食总体积小于10,于是每种零食有放入和不放入两种情况,一共有2*2*2=8种情况
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
int a[111];
int n, w;
int dfs(int q, int w, long long total) {
if (q < 0){
return 1;
}
int u = 0;
if (w >= total){
return pow(2, q+1);
}
if (w - a[q] >= 0){
u += dfs(q-1, w-a[q], total-a[q]);
}
u += dfs(q-1, w, total-a[q]);
return u;
}
int main() {
cin >> n >> w;
long long total = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
total += a[i];
}
sort(a, a + n);
cout << dfs(n-1, w, total) << endl;
}