1、【京东】编写一段程序,快速计算出2,3,5自由组合的由小到大的第n个数,也包括2,3,5。 运行结果:
代码:
//京东笔试,第n个2 c3 5组成的数
#include<iostream>
#include<vector>
using namespace std;
void fun(int &num, int temp,int n)
{
if (temp <= 0) return;
int sintece = n - temp;
if (temp == 1) sintece = n;
//cout << "temp " << temp << " sintence " << sintece << endl;
if (sintece <= temp && sintece >0)
{
num = num * 10 + 2;
fun(num, temp /= 3, sintece);
}
else if (sintece > temp && sintece <= 2 * temp)
{
num = num * 10 + 3;
fun(num, temp /= 3, sintece);
}
else
{
num = num * 10 + 5;
fun(num, temp /= 3, sintece);
}
}
int kaka(int n)
{
int index = 0, temp = 1;//指数和当前指数值
while(1)
{
index++;
temp *= 3;
if (temp >= n) break;
}
temp /= 3;
int num = 0;
fun(num, temp, n);
return num;
}
int main()
{
int n=0;
while (1)
{
cin >> n;
cout << "查询的数: ";
cout << kaka(n) << endl;
}
system("pause");
return 0;
}
2、B站:
//B站第三题
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
#include<utility>
#include<map>
using namespace std;
void fun(int n)
{
if (n % 2 == 0) return;
int mid = n / 2;
vector<vector<int>> *vec= new vector<vector<int>>(n,vector<int>(n,0));
vector<vector<int>>::iterator it_1 = vec->begin();
vector<vector<int>>::iterator it_2 = vec->end()-1;
int index = 1;
for (int i = 0; i <= mid; i++)
{
int temp = mid - i;
int temp_h = mid + i;
index = 1;
for (int j = 0; j <= mid; j++)
{
if (j >= temp && j <= mid)
{
(*it_1)[j] = index ;
(*it_1)[2*mid - j] = (*it_1)[j];
(*it_2)[j] = (*it_1)[j];
(*it_2)[2 * mid - j] = (*it_1)[j];
index++;
}
else if (j >= temp && j <= mid)
{
}
//cout << (*it_1)[j] << " " << (*it_2)[j] << endl;
}
it_1++;
it_2--;
}
for(vector<vector<int>>::iterator it = vec->begin(); it != vec->end(); it++)
{
for (int i = 0; i < n; i++)
{
if ((*it)[i] == 0)
{
cout << "*";
}else
{
cout << (*it)[i];
}
}
cout << endl;
}
delete vec;
}
int main()
{
cout << "请输入数字:";
int n;
while (cin >> n)
{
if (n % 2 == 0) cout<<"你输入的数字不正确请重新输入:";
fun(n);
}
system("pause");
return 0;
}
3、美团第四题
//java :45正确率
public class Main3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String s1 = sc.nextLine();
int n = Integer.parseInt(s1);//String 转int
int[] nums = new int[n];
String[] s2 = sc.nextLine().split(" ");
for(int i = 0; i < n; i++){
nums[i] = Integer.parseInt(s2[i]);
}
isaTree(nums, n);
}
}
private static void isaTree(int[] nums, int n) {
if(n < 3){
System.out.println("NO");
return;
}
int countLeaf = 0;
int sumRoot = 0;
for(int i = 0; i < n; i++){
if(nums[i] == 2){
System.out.println("NO");
return;
}
if(nums[i] == 1){
countLeaf++;
}else{
sumRoot += (nums[i] - 1);
}
}
if(countLeaf > sumRoot){
System.out.println("NO");
return;
}
if(sumRoot == (n - 1)) {
System.out.println("YES");
return;
}
System.out.println("NO");
}
}
//C++未完整
#include<iostream>
#include<vector>
#include<set>
using namespace std;
int fun()
{
int num = 0;
cin >> num;
int temp;
multiset<int, greater<int>> temp1;
vector<multiset<int,greater<int>>> *title = new vector<multiset<int,greater<int>>>(num,temp1);
for (auto it = title->begin(); it < title->end(); it++)
{
while (cin.peek() == '\n')
{
cin >> temp;
(*it).insert(temp);
}
}
int length = 0,index = 0;
bool ishead;
for (auto it = title->begin(); it!=title->end(); it++)
{
length = (*it).size();
ishead = true;
int index = *((*it).begin());
int index_2 = 0;
bool isused[24];
memset(isused, 0, 24);
int current = 0;
for(auto i = (*it).begin()++; i != it->end(); ++i)
{
current++;
if( ishead ) //判断位置
{
if (*i == index)
{
cout << "NO" << endl;
break;
}
ishead = false;
}
else if (*i % 2 == 0)
{
cout << "NO" << endl;
break;
}
else if (1)
{
isused[current] = true;
int subcur = current;
for (auto dex = i; dex != it->end(); dex++)
{
subcur++;
auto oi = i;
if (!isused[subcur])
{
if (index == *i + *dex)
{
isused[subcur] = true;
break;
}
}
}
}
}
}
return 0;
}
4、搜狗应用开发开发第一题
C++: 55正确率
//第一题
#include<iostream>
#include<set>
using namespace std;
int fun(int a, int b, int c)
{
multiset<int> set;
set.insert(a);
set.insert(b);
set.insert(c);
auto it = set.begin();
a = *it++;
b = *it++;
c = *it++;
cout << a << " " << b << " " << c << endl;
while (a<(b-1))
{
a++;
b--;
c--;
}
cout << a << " " << b << " " << c << endl;
while (a < (c-2))
{
if (b > a)
{
c -= 2;
a++;
}
else if (a < (c - 4))
{
a++;
b++;
c -= 4;
}else
{
break;
}
}
cout << a << " " << b << " " << c << endl;
return a;
}
int main()
{
int a, b, c;
while (1)
{
cin >> a >> b >> c;
cout << fun(a, b, c) << endl;;
}
system("pause");
return 0;
}
JAVA:正确率未知
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] input = new int[3];
String[] strings = scanner.nextLine().split(",");
for (int i = 0; i < 3; i++) {
input[i]=Integer.parseInt(strings[i]);
}
Arrays.sort(input);
while (input[2]-input[1]>2){
input[2]-=2;
input[0]++;
Arrays.sort(input);
}
while (input[1]>input[0]){
input[2]--;
input[1]--;
input[0]++;
Arrays.sort(input);
}
System.out.println(input[0]);
}
5、搜狗第三题
c++代码:
#include<iostream>
#include<vector>
using namespace std;
int fun()
{
cout << "请输入数据: ";
int size;
cin >> size;
vector<int> a;
int temp;
while (cin>>temp)
{
a.push_back(temp);
}
int *ret = new int[a.size()];
bool isdouble = false;
int index = 0;
int num = 0;
int i = 1;
for (auto it = a.begin(); it != a.end(); it++)
{
if (!isdouble)
{
index = *it;
isdouble = true;
}
else
{
ret[num++] = index- (*it )/ 2;
ret[num++] = (*it) / 2 + index;
isdouble = false;
}
}
int result = 2;
for (int i = 1; i < a.size()-1; i++)
{
if (i % 2)
{
continue;
}
else
{
if ((ret[i] - ret[i - 1]) > size)
{
result += 2;
}else if( (ret[i] - ret[i - 1]) == size)
{
result++;
}
}
}
cout <<"建造方式:"<< result << endl;
delete ret;
return result;
}
int main()
{
fun();
system("pause");
return 0;
}
6、华为AI&BG测开第三题:??你说我输出的不是一个数?
#include<iostream>
#include<vector>
#include<set>
#define MAX 30000
using namespace std;
double fun(double *a, int n, int m, int x, int y, double p, double index, double *res, double pcol)
{
cout << "m : " << m << "n : " << "x: " << x << "y: " << y << endl;
//cout << "执行" << endl;
if (x == m && y == n)
{
cout << " 满足 步数"<<index << "当前期望:"<<*res<<endl;
return (*res);
}
if (x > n || y > m) return (*res);
if (index >= 3 * m*n) return (*res);
pcol *= p;
if (index == 0)
{
cout << "执行第一步 " <<res<< endl;
}
fun(a, n, m, x, y + 1, a[3 * y*m + 3 * x], index + 1, res, pcol);
if (index == 0)
{
cout << "执行第二步 " <<res<< endl;
}
fun(a, n, m, x + 1, y, a[3 * y*m + 3 * x + 1], index + 1, res, pcol);
if (index == 0)
{
cout << "执行第三步 " << res<<endl;
}
fun(a, n, m, x, y, a[3 * y*m + 3 * x + 2], index + 1, res, pcol);
return (*res);
}
void start()
{
int n, m;
cin >> n >> m;
double a[MAX];
double temp;
for (int i = 0; i < 3 * n*m; i++)
{
cin >> temp;
a[i]=temp;
};
double res = 0;
fun(a, n, m, 0, 0, 1, 0, &res, 1);
cout << res << endl;
}