由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 写了一个find kth number in 2 sorted arrays的code 请大牛看
相关主题
背包问题T problem
问题:从电话号码打出所有单词C++ Q 99-102
c++ 程序一问two sigma 的online code test 的问题
这题你肯定见过,但有准备coding么?你们看过programming pearls (2nd edition English) or 正在看的同学们
问一道kth smallest element的题目one c++ question
Interview questions, BloombergC的argc问题
黑客rank Stock Maximize为什么我这段简单的程序segment fault
How to find the size of an array? Thanks.bloomberg assessment的机经,c语言的(20道题)
相关话题的讨论汇总
话题: std话题: numbers2话题: numbers1话题: endl话题: cout
进入JobHunting版参与讨论
1 (共1页)
C***U
发帖数: 2406
1
#include
#include
#include
#define MAX 10000
int main(int argc, char *argv[]){
std::vector numbers1;
std::vector numbers2;
int k = atoi(argv[3]);
if(argc < 4)
std::cout << "we need 2 files and a number." << std::endl;
std::cout << "we need find " << k << "th number" << std::endl;
std::cout << "reading first array of numbers ..." << std::endl;
std::ifstream f1(argv[1]);
if(!f1){
std::cout << "cannot open the file of first array of numbers ...
" <
< std::endl;
exit(-1);
}
int i = 0;
double num;
while(!f1.eof()){
f1 >> num;
numbers1.push_back(num);
i++;
}
f1.close();
std::cout << "reading second array of numbers ..." << std::endl;
std::ifstream f2(argv[2]);
if(!f2){
std::cout << "cannot open the file of second array of numbers
... "
<< std::endl;
exit(-1);
}
i = 0;
while(!f2.eof()){
f2 >> num;
numbers2.push_back(num);
i++;
}
f2.close();

if(k > numbers1.size() + numbers2.size()){
std::cout << "there are no " << k << "th number..." <<
std::endl;
exit(-1);
}
numbers1.push_back(MAX);
numbers2.push_back(MAX);
int s, j, t;
if(k - 2 > numbers1.size() - 1){
s = numbers1.size() -1;
t = k - 2 - s;
j = k / 2 + k % 2;
}
else{
s = k - 2, t = 0;
j = k / 2 + k % 2;
}

while(numbers1[s] > numbers2[t + 1] || numbers2[t] > numbers1[s +
1]){
if(numbers1[s] < numbers2[t]){
if(t <= 0)
break;
else {
if(s + j >= numbers1.size() - 1)
j = j / 2 + j % 2;
else{
s = s + j;
t = t - j;
j = j / 2 + j % 2;
}
}
}
else if(numbers1[s] == numbers2[t])
break;
else {
if(s <= 0)
break;
else {
if(t + j >= numbers2.size() - 1)
j = j / 2 + j % 2;
else{
s = s - j;
t = t + j;
j = j / 2 + j % 2;
}
}
}
}
std::cout << ((numbers1[s] > numbers2[t]) ? numbers1[s] :
numbers2[t]) <
< std::endl;
return 0;
}
C***U
发帖数: 2406
2
自己顶
有大牛帮忙看看
对不对 有啥需要改进的?

【在 C***U 的大作中提到】
: #include
: #include
: #include
: #define MAX 10000
: int main(int argc, char *argv[]){
: std::vector numbers1;
: std::vector numbers2;
: int k = atoi(argv[3]);
: if(argc < 4)
: std::cout << "we need 2 files and a number." << std::endl;

1 (共1页)
进入JobHunting版参与讨论
相关主题
bloomberg assessment的机经,c语言的(20道题)问一道kth smallest element的题目
C++ online Test 又一题Interview questions, Bloomberg
C++ 一题黑客rank Stock Maximize
这题哪错了?How to find the size of an array? Thanks.
背包问题T problem
问题:从电话号码打出所有单词C++ Q 99-102
c++ 程序一问two sigma 的online code test 的问题
这题你肯定见过,但有准备coding么?你们看过programming pearls (2nd edition English) or 正在看的同学们
相关话题的讨论汇总
话题: std话题: numbers2话题: numbers1话题: endl话题: cout