百度笔试题收集
baidu笔试题:
#include <iostream>
using namespace std;
int foo(int a)
{
int x, y, z;
x = a/100; //百位
y = (a - x*100)/10; //十位
z = a - x*100 - y*10; //个位
//cout << a << " " << x << " " << y << " " << z << " " << a+x+y+z << "\n";
return a+x+y+z;
}
int main()
{
cout << "wait for output:\n";
for(int i = 1; i < 1000; i++)
{
int j = (i -27) > 0 ? (i -27) : 1;
for(; j < i; j++)
{
if(i == foo(j))
{
break;
}
}
if(j == i)
{
cout << i << "\n";
}
}
return 0;
}
using namespace std;
int foo(int a)
{
int x, y, z;
x = a/100; //百位
y = (a - x*100)/10; //十位
z = a - x*100 - y*10; //个位
//cout << a << " " << x << " " << y << " " << z << " " << a+x+y+z << "\n";
return a+x+y+z;
}
int main()
{
cout << "wait for output:\n";
for(int i = 1; i < 1000; i++)
{
int j = (i -27) > 0 ? (i -27) : 1;
for(; j < i; j++)
{
if(i == foo(j))
{
break;
}
}
if(j == i)
{
cout << i << "\n";
}
}
return 0;
}
#include <iostream>
using namespace std;
const int MAX_ARR=100;
int * compare(int a[], int size_a, int b[], int size_b)
{
int *result = new int[MAX_ARR];
int i = 0, j = 0, k = 0;
while(i < size_a && j < size_b)
{
if(a[i] > b[j])
{
j++;
}
else if(a[i] < b[j])
{
i++;
}
else
{
result[k++] = a[i];
i++; j++;
}
}
result[k] = 0;
return result;
}
int main()
{
int a[10] = {5, 12, 25, 31, 42, 45, 64, 65, 84, 98 };
int b[15] = {12, 42, 46, 65, 67, 81, 86, 88, 89, 96, 98, 102, 123, 145, 485};
int *arr = compare(a, 10, b, 15);
for(int i = 0; i < MAX_ARR && arr[i] != 0; i++)
{
cout << arr[i] << " ";
}
return 0;
}
using namespace std;
const int MAX_ARR=100;
int * compare(int a[], int size_a, int b[], int size_b)
{
int *result = new int[MAX_ARR];
int i = 0, j = 0, k = 0;
while(i < size_a && j < size_b)
{
if(a[i] > b[j])
{
j++;
}
else if(a[i] < b[j])
{
i++;
}
else
{
result[k++] = a[i];
i++; j++;
}
}
result[k] = 0;
return result;
}
int main()
{
int a[10] = {5, 12, 25, 31, 42, 45, 64, 65, 84, 98 };
int b[15] = {12, 42, 46, 65, 67, 81, 86, 88, 89, 96, 98, 102, 123, 145, 485};
int *arr = compare(a, 10, b, 15);
for(int i = 0; i < MAX_ARR && arr[i] != 0; i++)
{
cout << arr[i] << " ";
}
return 0;
}