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;
|
|