-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1323.6-和-9-组成的最大数字.cpp
51 lines (51 loc) · 1.13 KB
/
1323.6-和-9-组成的最大数字.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* @lc app=leetcode.cn id=1323 lang=cpp
*
* [1323] 6 和 9 组成的最大数字
*/
#include <iostream>
#include <vector>
#include <iterator>
#include <stack>
#include <cmath>
using namespace std;
template <class T> std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) {
if (!v.empty()) {
out << '[';
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
out << "]";
}
return out;
}
// @lc code=start
class Solution {
// 100 5 0 6.4;
public:
// 第一个6变成9
int maximum69Number (int num) {
int digits = log10(num) + 1;
int oldNum = num;
stack<int> stk;
do{
stk.push(num % 10);
num = num / 10;
}while(num > 0);
int pos = 0;
while(!stk.empty()){
int n = stk.top();
if(n == 6){
return oldNum + 3 * pow(10, digits-1 - pos);
}
++pos;
stk.pop();
}
return oldNum;
}
};
// @lc code=end
int main(){
Solution s;
vector<int> v = {1, 2};
cout << s.maximum69Number(9669) << endl;
return 0;
}