华为4.18号笔试题

这里写图片描述

import java.util.Scanner;

/**
 * Created by ZD on 2018/4/18.
 */
public class Main_H21 {
   public static void main(String[] args){
            Scanner sc=new Scanner(System.in);
            while(sc.hasNext()){
                String str=sc.next();
                str=str.replaceAll("\\d+", "");
                int len=Integer.parseInt(sc.next());
                StringBuffer sb = new StringBuffer();
                for(int i=0,cur=0; i<str.length() && cur<len; i++){
                    char ch = str.charAt(i);
                    if(ch > 255){
                        if(cur+2 > len)
                            break;
                        cur += 2;
                        sb.append(ch);
                    }
                    if(ch >= 0 && ch <= 255){
                        cur++;
                        sb.append(ch);
                    }
                }
                System.out.println(sb.toString());
            }

        }
    }

这里写图片描述

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Scanner;

/**
 * Created by ZD on 2018/4/18.
 */
public class Main_H2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int years = sc.nextInt();
            int weeks = sc.nextInt();
            int count = 0;
            if (weeks > 6 || years > 400 || years < 0 || weeks < 0) {
                System.out.println(-1);
                return;
            }
            if (weeks == 0)
                weeks = 7;
            for (int i = 1; i <= years; i++) {
                for (int j = 1; j <= 12; j++) {
                    LocalDate startDate = LocalDate.of(1900 + i - 1, j, 13);
                    DayOfWeek weekDay = startDate.getDayOfWeek();
                    if (weeks == (weekDay.getValue())) count++;
                }
            }
            System.out.println(count);
        }
    }
}

这里写图片描述

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by ZD on 2018/4/18.
 */
public class Main_H3 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        if (n < 1)
            return;
        int min=n;
        int max=6*n;
        double sum=Math.pow(6,n);
        int row=max-min+1;
        int col=2;
        String arr[][]=new String[row][col];
        int index=0;
        for(int i=min;i<=max;i++,index++){
            arr[index][0]=""+i;
            double p=getSumCount(n,i)/sum;
            String str=String.format("%.5f",p);
            arr[index][1]= str;
        }
       System.out.println(Arrays.deepToString(arr));
    }
    public static int getSumCount(int n,int sum){
        if(n<1||sum<n||sum>6*n){
            return 0;
        }
        if(n==1){
            return 1;
        }
        int resCount=0;
        resCount=getSumCount(n-1,sum-1)+getSumCount(n-1,sum-2)+
                getSumCount(n-1,sum-3)+getSumCount(n-1,sum-4)+
                getSumCount(n-1,sum-5)+getSumCount(n-1,sum-6);
        return resCount;
    }
}

猜你喜欢

转载自blog.csdn.net/demodan/article/details/79997563
今日推荐