腾讯魔方一面(多年前版)
创始人
2024-11-14 17:34:34
0

给一个数,找出满足大于这个数,并且相邻每一位不相同的最小数,比如1121的解是1201,99的解是101

#include  #include    std::string solve(int x) { 	std::string s = std::to_string(x + 1); 	int n = s.length();  	for (int i = 0; i < n; ++i) { 		for (char c = ' '; i < n && c != s[i]; ++i) { 			c = s[i]; 		}  		if (i == n) break; 		std::string str = s.substr(0, i + 1); 		int t = std::stoi(str) + 1; 		n = str.length();  		s.replace(s.begin(), s.begin() + i + 1, std::to_string(t)); 	}  	bool zero = s[n-1] - '0'; 	for (int i = n; i < s.length(); ++i, zero = !zero) { 		s[i] = zero ? '0' : '1'; 	}  	return s; }  int main() { 	int x; 	while (std::cin >> x) { 		std::cout << solve(x) << "\n"; 	}  	return 0; } 
// 字符串版 #include  #include  #include   std::string add(std::string s) { 	int n = s.length(); 	assert(s.length() != 0);  	if (s[0] == '-') { 		if (s == "-1") return "0"; 		for (int i = n - 1; i >= 1; --i) { 			if (s[i] != '0') { 				s[i]--; 				break; 			} 			s[i] = '9'; 		} 		int k = 1; 		while (s[k] == '0') k++; 		return "-" + s.substr(k); 	}  	for (int i = n - 1; i >= 0; --i) { 		if (s[i] != '9') { 			s[i]++; 			return s; 		} 		s[i] = '0'; 	}  	return "1" + s; }  std::string solve(std::string x) { 	std::string s = add(x); 	int n = s.length();  	for (int i = 0; i < n; ++i) { 		for (char c = ' '; i < n && c != s[i]; ++i) { 			c = s[i]; 		}  		if (i == n) break; 		std::string str = s.substr(0, i + 1); 		std::string t = add(str); 		n = str.length();  		s.replace(s.begin(), s.begin() + i + 1, t); 	}  	bool zero = s[n-1] - '0'; 	for (int i = n; i < s.length(); ++i, zero = !zero) { 		s[i] = zero ? '0' : '1'; 	}  	return s; }  int main() { 	std::string x; 	while (std::cin >> x) { 		std::cout << solve(x) << "\n"; 	}  	return 0; } 

系统设计题,设计一个1000个玩家的游戏排行榜,要求满足高并发

// 感觉在分块加锁,和效率之间应该还有不少的提升空间 #include  #include  #include  #include  #include  #include  #include  #include    class Shard { public: 	void addOrUpdatePlayer(int playerId, int score) { 		std::unique_lock lock(mtx); 		auto it = playerToScore.find(playerId); 		if (it != playerToScore.end()) { 			scoreToPlayer.erase(scoreToPlayer.find(it->second)); 		} 		playerToScore[playerId] = score; 		scoreToPlayer.insert({score, playerId}); 	}  	std::vector> getToPlayer(int topN) { 		std::shared_lock lock(mtx); 		std::vector> topPlayer; 		auto it = scoreToPlayer.rbegin(); 		while (it != scoreToPlayer.rend() && topN > 0) { 			topPlayer.emplace_back(it->second, it->first); 			++it; 			--topN; 		} 		 		return topPlayer; 	}  private: 	std::map playerToScore; 	std::multimap scoreToPlayer; 	std::shared_mutex mtx; };  class GameRanking { public: 	GameRanking(int shardCount): shardCount(shardCount), shards(shardCount){ }  	void addOrUpdatePlayer(int playerId, int score) { 		int shardIndex = getShardIndex(playerId); 		std::async(std::launch::async, [this, shardIndex, playerId, score](){ 			shards[shardIndex].addOrUpdatePlayer(playerId, score); 		}).get(); 	}  	std::vector> getTopPlayers(int topN) { 		std::vector>>> futures; 		for (int i = 0; i < shardCount; ++i) { 			futures.push_back(std::async(std::launch::async, [this, i, topN] { 				return shards[i].getToPlayer(topN); 			})); 		}  		std::vector> allTopPlayer; 			for (auto & future : futures) { 				auto shardTopPlayers = future.get(); 				allTopPlayer.insert(allTopPlayer.end(), shardTopPlayers.begin(), shardTopPlayers.end());  			}  			std::sort(allTopPlayer.begin(), allTopPlayer.end(), [] (const auto& a, const auto& b) { 				return b.second < a.second; 			});  			if (allTopPlayer.size() > topN) { 				allTopPlayer.resize(topN); 			}  			return allTopPlayer; 	}  private: 	int shardCount; 	std::vector shards;  	int getShardIndex(int playerId) { 		return playerId % shardCount; 	} };   int main() { 	const int shardcount = 10; 	const int playercount = 1000; 	GameRanking ranking(shardcount);  	std::random_device rd; 	std::mt19937 gen(rd()); 	std::uniform_int_distribution<> dis(1, 10000);  	auto start = std::chrono::high_resolution_clock::now(); 	for (int i = 1; i <= playercount; ++i) { 		int score = dis(gen); 		ranking.addOrUpdatePlayer(i, score); 	} 	auto end = std::chrono::high_resolution_clock::now(); 	std::chrono::duration elapsed = end - start; 	std::cout << "time taken to add 1000 players: " << elapsed.count() << " seconds\n";  	start = std::chrono::high_resolution_clock::now(); 	auto topplayers = ranking.getTopPlayers(10); 	end = std::chrono::high_resolution_clock::now(); 	elapsed = end - start; 	std::cout << "time taken to get top 10 players: " << elapsed.count() << " seconds\n";   	for (const auto& player : topplayers) { 		std::cout << "player id: " << player.first << ", score: " << player.second << std::endl; 	}  	return 0; }  

最后给大家推荐一个LinuxC/C++高级架构系统教程的学习资源与课程,可以帮助你有方向、更细致地学习C/C++后端开发,具体内容请见 https://xxetb.xetslk.com/s/1o04uB

相关内容

热门资讯

终于找到“金花房卡一手货源/新... 新永和是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来...
ia实测“微信炸金花房间怎么创... 新众亿金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡...
金花房卡购买正规渠道/微信链接... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...
微信炸金花链接怎样弄/金花链接... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房...
牛牛链接房卡那里有/微信房卡斗... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
怎样购买微信金花房卡/微信牛牛... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
微信牛牛链接金花房卡/微信链接... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...
给大家讲解“购买斗牛金花房卡联... 悟空大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来...
哪里有卖微信炸金花房卡/微信牛... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房...
金花房卡从哪里购买/炸金花房卡... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
软件炸金花模式创建开房卡/微信... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
终于找到“购买金花房卡联系方式... 新八戒是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享...
一分钟了解“微信牛牛房卡在哪里... 新漫游牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡...
正版金花房卡哪里有卖/炸金花房... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...
微信链接金花房卡如何购买/哪里... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡...
微信链接斗牛房卡开科技/微信斗... 微信斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
金花房卡购买渠道/金花链接房卡... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
正版授权“玩金花牛牛15元10... 乐酷大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来...
可以一起创房的牛牛/炸金花房卡... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
正版授权“金花链接如何创建房间... 新道游是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来...