盛大游戏2018校园招聘笔试题
9月10号 盛大游戏的最后一题 当时没有写出来
再后来写出来了
今天(9月11号) 我看不懂我的代码了
垃圾代码,毁我青春
跪求高质量代码(题目和代码如下),另外由于没法提交了 也不知道自己写的代码还有没有其他问题
希望有人能看看
最后,给大佬递茶
question.txt
盛大游戏 2018校招 游戏研发客户端试卷在线考试
编程题 | 20.0分 3/3
找出最接近的对称数字
时间限制:C/C++语言 1000MS;其他语言 3000MS
内存限制:C/C++语言 65536KB;其他语言 589824KB
题目描述:
找出最接近的对称数字
输入一个正整数的字符串,输出与它最接近的对称数字(不包括它自己)的字符串
注1: 输入字符串的长度最多不会超过18
注2: 当大于输入数字和小于输入数字的对称数字与输入数字距离相同时,取小的数字作为答案
输入
输入为一个正整数的字符串
输出
输出为与输入数字最接近的对称数字(不包括输入本身)的字符串
样例输入
"123"
样例输出
"121"
温馨提示
请尽量在全场考试结束10分钟前调试程序,否则由于密集排队提交,可能查询不到编译结果
点击“调试”亦可保存代码
编程题可以使用本地编译器,此页面不记录跳出次数
#include <iostream>
#include <vector>
#include <numeric>
#include <limits>
#include <math.h>
using namespace std;
bool isNotaba(string n) {
int sz = n.size();
for (int i = sz / 2; i >= 0; i--) {
if (n[i] != n[sz - i - 1])
return true;
}
return false;
}
string min(string a, string b, string n) {
long long low, high, com;
sscanf(a.c_str(), "%lld", &low);
sscanf(b.c_str(), "%lld", &high);
sscanf(n.c_str(), "%lld", &com);
long long d1 = llabs(low - com), d2 = llabs(high - com);
if(d1 == d2) {
if(low <= high) return a;
else return b;
} else if (d1 > d2) {
return b;
} else {
return a;
}
return NULL;
}
string find(string n, bool higher) {
//tow special case: 9 and 11
if(n == "9") {
if(higher) return "11";
}
if(n == "11") {
if(!higher) return "9";
}
int sz = n.length();
long long prevHalf;
sscanf(n.substr(0, (sz + 1) / 2).c_str(), "%lld", &prevHalf);
// cout << "prev half is " << prevHalf << endl;
if (higher)
prevHalf++;
else
prevHalf--;
char ph[10];
snprintf(ph, 10, "%lld", prevHalf);
// cout << "ph is " << ph << endl;
int ph_len = strlen(ph);
// cout << "sz=" << sz << ",ph_len=" << ph_len << endl;
int d = (sz + 1) / 2 - ph_len; //产生进位或者退位
int s = 0;
char b = '9';
char fill_char = '#';
if (d < 0) {
s = sz + 1;
} else if (d > 0) {
s = sz - 1;
} else if (sz & 0x1) {
s = 2 * ph_len - 1;
} else {
s = 2 * ph_len;
}
// cout << "s= " << s << endl;
string res(s, fill_char);
// cout<<"res="<<res<<endl;
for (int i = 0; i < s; i++) {
if (i < ph_len)
res[i] = ph[i];
else
res[i] = res[s - 1 - i];
}
if(res[ph_len] == fill_char) {
res[ph_len] = b;
}
return res;
}
string NearestNum(string n) {
if (n.size() == 0)
return NULL;
if (isNotaba(n)) {
int sz = n.length();
for (int i = sz / 2; i < sz; i++) {
n[i] = n[sz - 1 - i];
}
} else {
string low, high;
low = find(n, false);
high = find(n, true);
// cout << "low=" << low << ",high=" << high << endl;
n = min(low, high, n);
}
return n;
}
int main() {
string res;
string _n;
getline(cin, _n);
res = NearestNum(_n);
cout << res << endl;
return 0;
}#Java工程师##C++工程师#
查看20道真题和解析