华为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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值