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

给一个数,找出满足大于这个数,并且相邻每一位不相同的最小数,比如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

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...