鹅厂2015实习生笔试题

1、一个商场进行抽奖活动,有两个奖项,A奖项抽中的概率是1/6,B奖项抽中的概率是5/6,用C语言编码实现这个抽奖程序。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

//抽奖函数:1/6的概率返回true,5/6的概率返回false
bool draw(){
	int ran;
	srand( (unsigned)time(NULL) );
	ran = rand() % 6;
	if(0 == ran)
		return true;
	else
		return false;
}

int main(){
	char ret;
	if(draw())
		ret = 'A';
	else
		ret = 'B';
	printf("抽奖结果为: %c\n", ret);
	return 0;
}

2、在不使用sizeof()函数的情况下,取出本机的整形所占位数,如32位、64位等

#include <stdio.h>

int numOfInt(){
	int num1;
	int num2 = 0;
	char *p1 = (char *)&num1;
	char *p2 = (char *)&num2;
	for(int num = 0; num < 8; ++num){
		p1[num] = p2[num];
		if(num1 == num2)
			return ++num;
	}
	return -1;
}

int main(){
	printf("本台计算机整形所占位数为: %d\n", numOfInt() * 8);
	return 0;
}

3、在某个C/S模型中,服务器最大的处理量是每秒10000次,设计一个算法,保证服务器可以正常运行。

#include <time.h>

double Clock_Start = 0;
double Clock_End = 0;
int count = 0;
bool client = false;

void ProtectServer(){
	while(1){   //是服务端一直运行
		if(Clock_End >= 1000){
			/*处理当前等待队列的所有请求*/
			Clock_Start = clock(); //记录当前时间
			count = 0;
		}
		if(client){
			if(count < 10000){
				count++;
			}
			else{
				/*给服务器抛出满载异常*/
			}
		}
		client = false;
		Clock_End = clock() = Clock_Start;
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值