2018.4.18华为在线笔试题目

在GBK编码下,请编写一个截取字符串的函数, 输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,同时忽略字符串中的数字后输出最终结果。

输入描述:
一行字符串和待截取的字节数
输出描述:
单独的一行截取后的字符串
示例1
输入
华HUA  
4
输出
华HU
备注:
要保证汉字不被截半个,同时忽略字符串中的数字后输出最终结果。

import java.util.Scanner;

public class 华为1 {

    public static void main(String[] args) throws Exception {  Scanner sc=new Scanner(System.in);  String s=sc.nextLine();  int num=sc.nextInt();    StringBuffer sb=new StringBuffer();  //System.out.println('1');  for(int i=0;i<s.length();i++)  if(s.charAt(i)>=48&&s.charAt(i)<=57)  {   }else  sb.append(s.charAt(i));//去掉数字  String r=splitString(sb.toString(), num);
        System.out.println(r);
    }

    public static String splitString(String str, int length)
            throws Exception {
        //无效输入
        if (str == null || str.length() < 1 || length < 1) {
            return "";
        }
        
        //用于统计这个字符串中有几个中文字符
        int wordCount = 0;
        //统一按照gbk编码来得到他的字节数组,因为不同的编码字节数组是不一样的。
        byte[] gbks = str.getBytes("GBK");

        //gbks中,汉字是两个负数表示
        for (int i = 0; i < length; i++) {
            int val = gbks[i];
            if (val < 0) {
                //汉字个数++  System.out.println(val);
                wordCount++;
            }
            System.out.println("wordcount "+wordCount);
        }

        //完整的汉字
        if (wordCount % 2 == 0) {
            return str.substring(0, (length - (wordCount / 2)));
        }
        //半个汉字 所以  -1
        return str.substring(0, (length - (wordCount / 2) - 1));

    }
}
第二题
13号又恰好是星期五真的很特殊吗?也就是说,13号出现在星期五的几率比出现在其它周日的几率大吗?要回答这个问题, 写一个程序计算13日出现在某个星期的次数(在给定的N年时间中)。这个时间段为1900年1月1日到1900+N-1年12月31日。 N为非负整数,不超过400。(1900年1月1日是星期一) 输入描述: 1 0 第一个参数为years,表示距离1900年1月1日的年数 第二个参数为weeks,表示星期数(分别用0——6代表星期日到星期六) 输出描述: 13日出现在星期数为weeks的次数,若异常失败输出-1 示例1 输入 1 0 输出 1
import java.util.Scanner; public class 华为2 { static int[] dayl = { 12, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 }; public static void main(String[] args){ Scanner sc = new Scanner(System.in ); int a = sc.nextInt(); int b = sc.nextInt(); int c = Result(a, b); if(c>0) System.out.println(c); else System.out.println(-1); } private static int Result(int year, int weeks){ int count = 0; int days = 0; try{ if(weeks <=6 && weeks>=0 && year>=0 && year<=400){ for(int i=1900; i<1900+year; i++){ days += i==1900?0:(runnian(i-1)?366:365); int day = days; for(int j=1; j<=12; j++){ days += getDay(i,j); if((days-(weeks-1))%7==0){ count++; } } days = day; } if(weeks<0 || weeks>6 || year<0 || year>400) return -1; }}catch (Exception e){ return -1; } return count; } private static int getDay(int i, int j) { if(!runnian(i)){ return dayl[j-1]; } return  j == 3? 29 : dayl[j-1]; } private static boolean runnian(int i) { return (i%4 == 0 && i % 100 !=0)||i%400 == 0; } }
第三题用的python
有N个骰子,同时投掷出去,向上面的数字之和为 A。 那么输入为N个筛子,请计算出A,和他出现的概率。 概率值,小数点保留5位。 输入描述: N,骰子数目 输出描述: [[1, 0.16667], [2, 0.16667], [3, 0.16667], [4, 0.16667], [5, 0.16667], [6, 0.16667]] 输出为二维数组。每一个值,第一个表示数字, 第二个表示概率。 示例1 输入 1 输出 [[1, 0.16667], [2, 0.16667], [3, 0.16667], [4, 0.16667], [5, 0.16667], [6, 0.16667]]
#encoding=UTF8 class Solution:     def dicesSum(self, n):         if n == 0 : return None         result = [                 [1,1,1,1,1,1],             ]         for i in range(1,n):             x = 5*(i+1)+1             result.append([0 for _ in range(x)])                           for j in range(x):                 if j < 6:                     result[i][j] = (sum(result[i-1][0:j+1]))                 elif 6 <= j <= 3*i+2:                     result[i][j] = (sum(result[i-1][j-5:j+1]))                 else:                     break             left = 0             right = len(result[i]) - 1             while left <= right:                 result[i][right] = result[i][left]                 left += 1                 right -= 1                   res = result[-1]         all = float(sum(res))         other = []         for i,item in enumerate(res):             pro = round(item/all,5)             other.append([n+i,pro])         return other           n = int(raw_input().strip()) s = Solution() print s.dicesSum(n)

全部评论
为什么第二题你会判断输出 !(c > 0) 时 输出 -1?为什么不是c >= 0
点赞 回复 分享
发布于 2018-04-18 21:57
题目要求异常输出-1
点赞 回复 分享
发布于 2018-04-19 08:49
友塔游戏
校招火热招聘中
官网直投
请问下楼主,c++下如何打印第三题对应的二维数组形式呢
点赞 回复 分享
发布于 2018-04-19 09:45
gg,看到的太晚了😂
点赞 回复 分享
发布于 2018-04-21 10:27
挖坟问一下,第二题如果不加那些异常处理可以过么?
点赞 回复 分享
发布于 2018-04-21 15:18
#include<iostream> using namespace std; void Week(int(&d)[12],int (&c)[7],int &week,int &daycount) {  int k = 0,m=0;  for (k = 0;k<12;k++)  {   for (m = 0;m<d[k];m++)   {    if (m == 13)    {     week = daycount % 7;     switch (week)     {     case 0:      c[0]++;      break;     case 1:      c[1]++;      break;     case 2:      c[2]++;      break;     case 3:      c[3]++;      break;     case 4:      c[4]++;      break;     case 5:      c[5]++;      break;     case 6:      c[6]++;      break;     }    }    daycount++;   }  } } int main() {  int i = 0, j = 0, k = 0, m = 0, week = 0, daycount = 0, count = 0;  int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };  int b[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };  int n = 0, z = 0;  int c[7] = { 0 };  int year = 1900;  cin >> n;  cin >> z;  i = 0;  if (n<=0 ||n>400|| z<0 || z>6)  {   cout << -1 << endl;   return 0;  }  while (i < n)  {   if ((1900 + n) % 4 == 0)   {       Week(b, c, week, daycount);   }   else   {    Week(a, c, week, daycount);   }   i++;  }  count = (z + 6) % 7;  cout << c[count];  return 0; } 用c++写第二题只有75%通过
点赞 回复 分享
发布于 2018-04-21 20:35
要保证汉字不被截半个,同时忽略字符串中的数字后输出最终结果。 这句话怎么理解后半句
点赞 回复 分享
发布于 2018-05-05 01:08

相关推荐

litbisc:你先说会,然后去速成课自学一个月,出门在外,身份都是自己给的
点赞 评论 收藏
分享
点赞 21 评论
分享
牛客网
牛客企业服务