面试心经-笔试编程整理

最近在找实习,作为技术渣,一直被虐,不得已只能拼命刷题攒人品,下面是遇到的一些编程题,整理如下,如有雷同,纯属抄袭~

360java语言编程:

数学期望


题目描述:

小明同学最近学习了概率论,他了解到数学期望的定义:设X为一个随机变量,X可以取n种不同的取值x1,x2,x3,…,xn。取x1的概率为p1,取x2的概率为p2,以此类推。定义随机变量X的数学期望为:E[X]=x1*p1+x2*p2+…+xn*pn。

小明回到家中,他想编程计算数学期望,你能帮助他么?

输入

输入第一行一个数n(1<=n<=100),接下来有n行,第i行有两个数xi和pi,xi和pi都是整数,-100<=xi<=100, 0<=pi<=100。表示随机变量X取值为xi的概率是pi/100。输入保证p1+p2+p3+…+pn=100。

输出

输出一个数,随机变量X的数学期望E[x],小数点后四舍五入保留3位。

样例输入

3

0 50

1 20

2 30

样例输出

0.800

 

代码:

package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class wcctest {
	public static void main(String[] args)
    {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        double sum = 0;
        int n = 0;
		try {
			str = br.readLine().trim();
			n = Integer.parseInt(str);
			for(int i=0;i<n;i++){
				String temp = br.readLine().trim();
				int begin = temp.trim().indexOf(" ");
				int end = temp.trim().lastIndexOf(" ");
				String str1= temp.substring(0, begin);
				String str2 = temp.substring(end+1, temp.length());
				int a = Integer.parseInt(str1);
				int b = Integer.parseInt(str2);
				double c = ((double)b/100);
			    sum += a*c;
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		System.out.println(sum);		
    }
}

偶串

时间限制:C/C++语言 2000MS;其他语言 4000MS
内存限制:C/C++语言 65536KB;其他语言 589824KB

题目描述:

一个字符串S是偶串当且仅当S中的每一个字符都出现了偶数次。如字符串”aabccb”是一个偶串,因为字符a,b,c都出现了两次。而字符串”abbcc”不是偶串,因为字符a出现了一次。

现在给出一个长度为n的字符串T=t1,t2,t3,…,tn。字符串的子串为其中任意连续一段。T长度为1的子串有n个,长度为2的子串有n-1个,以此类推,T一共有n(n+1)/2个子串。给定T,你能算出它有多少个子串是偶串吗?

输入

输入一个字符串T,T中只有小写字母。T的长度不超过100000。

输出

输出一个数,T的所有子串中偶串的个数。

样例输入

abbc

样例输出

1


代码:

package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class wcctest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
		try {
			str = br.readLine().trim();				
			int sum = GetEvenStringSum(str);
			System.out.print("\n"+sum);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static int GetEvenStringSum(String str) {
		// TODO Auto-generated method stub
		int count = 0;
		for(int i = 0;i<str.length();i++)
			for(int j=0;j+i<str.length();j++)
			{
				int sublen = i+1;
				String subString = str.substring(j,j+sublen);
				if(isEvenString(subString))
					count++;
			}		
		return count;
	}

	public static boolean isEvenString(String str)
	{
		boolean ret = true;
		char[] arr = str.toCharArray();//String.toCharArray 方法 ,作用:将字符串转换为字符数组。
		HashSet<Character> set = new HashSet<Character>();
		Map<Character,Integer> map = new HashMap<Character,Integer>();
		for(char c:arr)
		{
			if (!set.contains(c)) {
				set.add(c);
				map.put(c, 1);
			}else
			{
				map.put(c, map.get(c)+1);
			}			
		}
		Iterator ite = map.entrySet().iterator();
		while(ite.hasNext())
		{
			Map.Entry<Character,Integer> entry = (Entry<Character, Integer>) ite.next();
			int num = entry.getValue();
			if(num%2!=0)
			{
				ret = false;
				break;
			}
		}		
		return ret;
	}
}

华为编程模拟题:

a+b

描述:计算a+b的和

每行包括两个整数a和b

对于每行输入对应输出一行a和b的和

输入

1  5

输出

6

代码:

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class wcctest {

	public static void main(String[] args)
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        try {
        	str = br.readLine().trim();
        	int begin = str.trim().indexOf(" ");
        	int end = str.trim().lastIndexOf(" ");
        	String str1= str.substring(0, begin);
        	String str2 = str.substring(end+1, str.length());
        	int a = Integer.parseInt(str1);
        	int b = Integer.parseInt(str2);
        	System.out.print(a+b);
        	} catch (IOException e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();
        		}     	
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经,又称“般若经”,是佛教中最重要的经典之一。下面是经的英文翻译: Heart Sutra Avalokiteshvara Bodhisattva, when practicing deeply the Prajnaparamita, clearly saw that all five skandhas are empty and thus relieved all suffering. Shariputra, form does not differ from emptiness, emptiness does not differ from form. Form itself is emptiness, emptiness itself form. Sensations, perceptions, formations, and consciousness are also like this. Shariputra, all dharmas are empty of characteristics. They are not produced, not destroyed, not defiled, not pure; and they neither increase nor diminish. Therefore, in emptiness there is no form, no sensation, perception, formation, or consciousness; no eyes, ears, nose, tongue, body, or mind; no sight, sound, smell, taste, touch, or object of mind; no realm of sight... no realm of mind consciousness. There is no ignorance, no extinction of ignorance, and so forth, up to no old age and death, and no extinction of old age and death. There is no suffering, no cause of suffering, no cessation, no path, no wisdom, and no attainment. The bodhisattva relies on Prajnaparamita and the mind is no hindrance; without hindrance no fears exist. Far beyond deluded thoughts, one reaches ultimate Nirvana. All buddhas of the past, present, and future rely on Prajnaparamita and thereby attain supreme enlightenment. Therefore, know the Prajnaparamita as the great spell, the spell of great knowledge, the utmost spell, the unequaled spell, allayer of all suffering, in truth - for what could go wrong? By the Prajnaparamita has this spell been spoken. Gone, gone, gone beyond, gone altogether beyond, enlightenment hail!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值