1. 程式人生 > >網易2018 Android實習生線上程式設計筆試題

網易2018 Android實習生線上程式設計筆試題

第一題

題目描述:牛牛總是睡過頭,所以他定了很多鬧鐘,只有在鬧鐘響的時候他才會醒過來並決定起不起床。從他起床算起他需要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;
}