【华为OD】C卷真题 200分 100%通过:解密犯罪时间 C/C++代码实现[思路+代码]

 题目描述:

警察在侦破一个案件时,得到了线人给出的可能犯罪时间,形如 “HH:MM” 表示的时刻。

根据警察和线人的约定,为了隐蔽,该时间是修改过的,解密规则为:利用当前出现过的数字,构造下一个距离当前时间最近的时刻,则该时间为可能的犯罪时间。每个出现数字都可以被无限次使用。

输入描述:

形如HH:SS的字符串,表示原始输入

输出描述:

形如HH:SS的字符串,表示推理出来的犯罪时间

示例1输入输出示例仅供调试,后台判题数据一般不包含示例

输入

18:52

输出

18:55

说明

利用数字1, 8, 5, 2构造出来的最近时刻是18:55,是3分钟之后。结果不是18:51因为这个时刻是23小时59分钟之后。

示例2输入输出示例仅供调试,后台判题数据一般不包含示例

输入

23:59

输出

22:22

说明

利用数字2, 3, 5, 9构造出来的最近时刻是22:22。 答案一定是第二天的某一时刻,所以选择可构造的最小时刻为犯罪时间。

备注:

 

1.    可以保证线人给定的字符串一定是合法的。例如,“01:35” 和 “11:08” 是合法的,“1:35” 和 “11:8” 是不合法的。

2.    最近的时刻有可能在第二天。

 

     668                                                         
                                                            
              +---+                                                          
  3            |   |       ++                               +       +---|   
  |           |   | 3      +                6               +  |   +   |        +
  |      +     |   |       +         +                      +    |  +   |       +
  |      +    |   +---+    +        +        +++++          +   |  +   |        +
  |      +    | +      |   +   +----+        |   |          +   |  +   |        +
  |      +  3 | +      |   +   +    +      2 |   |     2    +   |  +   |        +
  |      +    | +      |   +   +    +        |   |          +   |  +   |        +
  |      +---+ |     |    |  |    +    ----+   |   +---+    |  |  +   |         +
  |      |     |     |    |  |    +    |       |   |   |    |  |  +   |         +
  |    1 |     |     | 8  |  |    +  1 |   |    | 1 |   | 1 |   |  +   |        +
  |      |     |     |    |  |    +    |   |    |   |   |   |   |  +   |        +
  |  +---+     |     +---+   |    ++---+    ++   +---+   +---+   |  +   |        +
  |  |         |         |   |    |         ++              |   |  |+   |        +
  |0 |         |         | 0 |  0 |         ++              | 0 |  |+   |        +
  |  |         |         |   |    |         ++              |   |  |+   |        +
  +---+         +          +-------+                       +---+| +|+   |        +
                +                                                    +   |        +
    0   1   2   3   4   5   6   7   8   9  10  11  12 + v:    w  u m    u 1 0 2 4
 

题目描述:

        需要注意的是,推导出最近的时间,可以是当天最近或者隔一天最近,就看哪个时间更近

代码实现:

#include 
#include 
#include 
#include 
using namespace std;

#define Separator ':'


bool ruleTest(const string &keys, string time, string str, int index);

bool ruleTest1(const string &keys, string str, int index);

void replaceAll(string &str, char c) {
	for (auto &i : str) {
		if (i != Separator) {
			i = c;
		}
	}
}

string process(string str)
{
	str.erase(str.find(Separator), 1);
	sort(str.begin(), str.end(), less());
	return str;
}

int main()
{
	string str;
	cin >> str;

	string time(str);
	string number = process(str);

	if (ruleTest(number, time, str, 4)) {
		return 0;
	}

	vector> conf = { { 4 }, {3, 4},{ 1, 3 }, {0, 1} };
	for (const auto & item : conf) {
		for (size_t i = 1; i < item.size(); ++i) {
			str[item[i]] = number[0];
		}
		if (item[0] > 2) {
			if (ruleTest(number, time, str, item[0])) {
				return 0;
			}
		}
		else if (ruleTest1(number, str, item[0])) {
			return 0;
		}
		
	}

	replaceAll(str, number[0]);
	cout << str << endl;
	return 0;
}

bool minuteCompare(const string &a1, const string &a2)
{
	int v2 = stoi(a2.substr(3));
	if (v2 >= 60) {
		return false;
	}

	return stoi(a1.substr(3)) < v2;
}

bool hourCompare(const string &t)
{
	return stoi(t.substr(0, 2)) < 24;
}
 

bool ruleTest(const string &dict, string time, string res, int pos)
{
	for (size_t i = 0; i < dict.size(); ++i) {
		res[pos] = dict[i];
		if (minuteCompare(time, res)) {
			cout << res << "\n";
			return true;
		}
	}

	return false;
}

bool ruleTest1(const string &dict, string str, int pos)
{
	string ss;
	for (size_t i = 0; i < dict.size(); ++i) {
		if (dict[i] > str[pos]) {
			ss = str;
			ss[pos] = dict[i];
			if (!hourCompare(ss)) {
				ss.clear();
			}

			if (!ss.empty()) {
				cout << ss << endl;
				return true;
			}
		}
	}

	return false;
}

7587fb62cfa8459d9e4d88066d23cee1.jpeg

 

你可能感兴趣的:(杂谈,华为od,c语言,c++,javascript,java,python,算法)