编程题共3道,貌似与其它岗位的小伙伴题目都不一样,本人遇到的难度较低。另外题面包含错别字以及描述不太清晰,值得吐槽。
第一题 最小整数
有一个32位整数n,试找一个最小整数m,使得m的每一位之积等于n,如果找不到这样的整数,输出0
分析可知,整数m的所有位均为2-9的整数,对n做质因数分解变形(每次从9-2取数字做整除),能成功分解证明可以找到合适的整数,然后对分解出来的数字进行排序,从小到大输出,未发现明显trick,1A
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL long long
using namespace std;
LL m ,n;
LL item[1000];
LL cnt;
int yinshufenjie(LL num){
cnt =0;
LL i;
LL temp = num;
do{
temp=num;
for (i = 9;i >=2 ;i--)
{
while (num != i)
{
if (num%i == 0)
{
item[cnt++] = i;
num = num / i;
}
else break;
}
}
}while(temp != num);
if(num<10){
item[cnt++]=num;
return 1;
}
else{
return 0;
}
}
int main()
{
LL m ,n;
cin>>n;
if(yinshufenjie(n)){
sort(item,item+cnt);
for(int i=0;i<cnt;i++){
cout<<item[i];
}
cout<<endl;
}
else{
cout<<"0"<<endl;
}
return 0;
}
第二题 NTES子串判断
水题,判断是否存在目标顺序的字符
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#define LL long long
using namespace std;
int main(){
int t;
char s[101];
char ntes[10]="NTES\0";
int len = strlen(ntes);
cin>>t;
while(t--){
cin>>s;
int cnt =0;
int l = strlen(s);
for(int i=0;i<l;i++){
if(s[i]==ntes[cnt]){
cnt++;
}
if(cnt>len){
break;
}
}
//cout<<cnt<<endl;
if(cnt == len){
cout<<"yes"<<endl;
}
else{
cout<<"no"<<endl;
}
}
return 0;
}
/*
2
STNETEDTS
TSENSTE
*/
第三题 树的深度
给出n 和 n行,n代表树有n个节点,接下来的n行,每一行有两个数字,代表该节点的左右子节点是否存在,1为存在,-1为不存在。节点输入的顺序有序,第一组为根节点的左右子节点,求树的最大深度。
分析:已知节点有序,证明同样深度的节点顺序出现,从子节点信息也可以累加下一层有多少个节点,因此只需要遍历输入,统计当前层和下一层有多少节点,累加深度即可
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#define LL long long
using namespace std;
struct node{
int left;
int right;
}tree[101];
int main(){
int n;
cin>>n;
int father[101];
int left,right;
int cp=1;
int nextp=0;
int dep = 1;
for(int i=0;i<n;i++){
cp--;
cin>>left>>right;
if(left>0){
nextp+=1;
}
if(right>0){
nextp+=1;
}
if(cp==0){
cp=nextp;
if(cp>0){
dep+=1;
}
nextp=0;
}
}
cout<<dep<<endl;
return 0;
}