腾讯魔方一面(多年前版)

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

#include <iostream>
#include <string>


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 <iostream>
#include <string>
#include <cassert>

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 <iostream>
#include <map>
#include <shared_mutex>
#include <vector>
#include <future>
#include <thread>
#include <chrono>
#include <random>


class Shard {
public:
	void addOrUpdatePlayer(int playerId, int score) {
		std::unique_lock<std::shared_mutex> 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<std::pair<int, int>> getToPlayer(int topN) {
		std::shared_lock<std::shared_mutex> lock(mtx);
		std::vector<std::pair<int, int>> 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<int, int> playerToScore;
	std::multimap<int, int> 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<std::pair<int, int>> getTopPlayers(int topN) {
		std::vector<std::future<std::vector<std::pair<int, int>>>> 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<std::pair<int, int>> 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<Shard> 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<double> 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值