服务端面试题

1.随机产生5个数,这5个数每个数的范围都在[10,35]之间,5个数的和是定值100,尽量让5个数的概率随机,算法尽量高效,写出算法??

#include <iostream>  
#include <cstdlib>  
#include <ctime>  
using namespace std;
int main()
{ 
	srand((unsigned)time(NULL));
	int a, b, c, d, e;
	a = (rand() % 26) + 10;
	b = (rand() % 26) + 10;
	if (a != 35 || b != 35)
	{
		c = (rand() % 26) + 10;
		while ((100 - a - b - c) / 2<10 || (100 - a - b - c) / 2 > 35)			
			c = (rand() % 26) + 10;
		d = (rand() % 26) + 10;
		while ((100 - a - b - c - d)<10 || (100 - a - b - c - d)>35)
			d = (rand() % 26) + 10;
		e = 100 - a - b - c - d;
	}
	else
	{
		c = d = e = 10;
	}
	cout << a << endl;
	cout << b << endl;
	cout << c << endl;
	cout << d << endl;
	cout << e << endl;
	system("pause");
	return 0;
}


2.不使用库函数实现数组里所有元素求和

1)使用中间变量实现

2)不使用中间变量实现

#include <iostream>
using namespace std;

int sum1(int a[], int len);
int sum2(int a[], int len);

int main()
{
	int a[] = { 1, 2, 3, 4, 5, 6, 7,8,9,10 };
	cout << sum2(a, sizeof(a) / sizeof(a[0])) << endl;
	system("pause");
	return 0;

}

int sum1(int a[], int len)
{
	int sum = 0;
	for (int i = 0; i < len; i++)
	{
		sum += a[i];
	}
	return sum;
}

int sum2(int a[], int len)
{
	if (len == 0)
		return 0;
	if (len == 1)
		return a[0];
	return sum2(a, len - 1) + a[len - 1];
}

3.游戏里面每个玩家有自己的id,名字,等级,职业,所属帮派,而且每个玩家有多只宠物,而每只宠物有自己的名字,等级,种类

1)用SQL语句创建玩家表

2)根据宠物种类统计玩家等级在40-49之间拥有宠物的数量

玩家表:

CREATE TABLE `player_role` (   `id` int(11) NOT NULL,   `name` varchar(25) DEFAULT NULL,   `profession` varchar(10) DEFAULT NULL,   `guild` varchar(25) DEFAULT NULL,   `level` int(11) DEFAULT NULL,   PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT 

宠物表:

  CREATE TABLE `pet` (
  `id` int(11) NOT NULL,
  `pet_name` varchar(25) DEFAULT NULL,
  `level` int(10) DEFAULT NULL,
  `kind` varchar(10) DEFAULT NULL,
  `player_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `player_id` (`player_id`),
  CONSTRAINT `pet_ibfk_1` FOREIGN KEY (`player_id`) REFERENCES `player_role` (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8

统计宠物数量:

select c.kind, count(*) as count from (select b.* from player_role a,pet b where a.id = b.player_id and (a.level >=40 and a.level <=49)) c group by c.kind;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值