本文共 2072 字,大约阅读时间需要 6 分钟。
1.随机产生5个数,这5个数每个数的范围都在[10,35]之间,5个数的和是定值100,尽量让5个数的概率随机,算法尽量高效,写出算法??
#include#include #include 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)不使用中间变量实现
#includeusing 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;
转载地址:http://csxlf.baihongyu.com/