爱奇艺2018秋季校招算法工程师(第三场)编程题
程序员文章站
2022-06-03 11:38:14
...
题目
代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string s;
int findIndex(string s)
{
for (int i = 0; i < s.length(); i++)
{
if (s[i] == '(' && s[i + 1] == ')')
{
return i;
}
}
return -1;
}
size_t solve(string s)
{
int tmp;
while (true)
{
tmp = findIndex(s);
if (tmp == -1)
{
break;
}
while (tmp >= 0 && s[tmp] == '(' && s[tmp + 1] == ')')
{
s.erase(tmp, 2);
tmp--;
}
}
return s.length();
}
int main()
{
cin >> s;
cout << solve(s) << endl;
return 0;
}
题目
代码
#include <iostream>
using namespace std;
long long sum;
long long get_sum(long long x)
{
long long sum = 0;
while (x != 0)
{
sum += x;
x /= 10;
}
return sum;
}
int main(int argc, const char * argv[])
{
cin >> sum;
long long l = 0, r = sum, m;
while (l < r)
{
m = (l + r) >> 1;
long long tmp = get_sum(m);
if (tmp == sum)
{
cout << m << '\n';
return 0;
}
else if (tmp < sum)
{
l = m + 1;
}
else
{
r = m - 1;
}
}
if (get_sum(l) == sum)
{
cout << l << endl;
}
else
{
cout << -1 << endl;
}
return 0;
}
上一篇: 最长回文子串
下一篇: Servlet的生命周期