2018.4.18华为在线笔试



在GBK编码下,请编写一个截取字符串的函数, 输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,同时忽略字符串中的数字后输出最终结果。
 
输入描述:
一行字符串和待截取的字节数
输出描述:
单独的一行截取后的字符串
示例 1
输入
华HUA  
4
输出
华HU
备注:
要保证汉字不被截半个,同时忽略字符串中的数字后输出最终结果。
 
import java.util.Scanner;

public class Test0_418 {

    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=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 Test1_418 {        
	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)  

你可能感兴趣的:(java,算法)