2023年笔试算法-四达时代

目录

      • 1. 二叉树遍历
        • Description
        • Input
        • Output
        • Sample Input 1
        • Sample Output 1
        • Hint
        • 代码
      • 2. 手机号码格式化
        • Description
        • Input
        • Output
        • Sample Input 1
        • Sample Output 1
        • Sample Input 2
        • Sample Output 2
        • Sample Input 3
        • Sample Output 3
        • Sample Input 4
        • Sample Output 4
        • Sample Input 5
        • Sample Output 5
      • 3. 斐波那契数列拆分
        • Description
        • Input
        • Output
        • Sample Input 1
        • Sample Output 1
        • Sample Input 2
        • Sample Output 2
        • Sample Input 3
        • Sample Output 3
        • Sample Input 4
        • Sample Output 4

1. 二叉树遍历

Total 【2434】、 AC rate 【2.10%】

Description

给定一棵满二叉树树先序遍历的输出结果,求该树中序遍历的输出结果。

Input

2^n - 1个数字(n是≤16的正整数),每个数字的取值范围[0, 255], 数字之间用逗号分割。

Output

树的中序遍历结果,数字之间通过逗号分割。

Sample Input 1
1,2,3,4,5,6,7
Sample Output 1
3,2,4,1,6,5,7
Hint
不要有任何多余的输出  C/C++,换行为"\n"
代码
#include 

void mid_reverse(int root[],int length, int idx, int target[], int &count) {
  	if (idx > length) {
      	return;
    }
	mid_reverse(root, length, idx * 2 + 1, target, count);
  	target[count++] = root[idx];
  	mid_reverse(root, length, idx * 2 + 2, target, count);
  
}

void pre_reverse(int root[],int length, int idx, int target[], int &count) {
  	if (idx >= length) {
      	return;
    }
  	target[idx] = root[count++];
	pre_reverse(root, length, idx * 2 + 1, target, count);
  	pre_reverse(root, length, idx * 2 + 2, target, count);
  
}

int main()
{
  // 处理 输入
  char c;
  int i = 0;
  int k = 1;
  int num = -1;
  
  int root[10000] = {0};
  
  
  
  do {
    c = getchar();
  	if(k != 1 && (c == ',' || c == '\n' || c == EOF)) {
    	root[++num] = i;
      	i = 0;
      	k = 1;
    } else {
      i = c - '0' + i * 10;
      k = 0;
    }
  }while(c != '\n' && c != EOF);
  
  
  // 父节点 root -> left 2 i + 1 -> right 2 i + 2 从 0 开始顺序表
  int pre_root[++num] = {0};
  int target[num] = {0};
  int a = 0;
  int idx = 0;
  int count = 0;
  
	
  pre_reverse(root, num, 0, pre_root, count);
  

    
  count = 0;
  
  mid_reverse(pre_root, num, 0, target, count);
  for(int i =1; i < num; i++) {
  	printf("%d,", target[i]);
  }
  printf("%d\n",target[num]);
  

  return 0;
}



2. 手机号码格式化

Total 【914】、 AC rate 【0.00%】

Description

四达在非洲各国运营均涉及到了短信业务,客户输入的手机号存在多样性,我们需要对客户的手机号做规范化处理。我们以尼日、肯尼亚、乌干达、卢旺达的短信业务为例,国际区号对应如下:

尼日    +234

卢旺达 +250

肯尼亚+254

乌干达+256

手机号码位数,要求如下:


尼日10位

卢旺达9位,且手机号不能为0开头

肯尼亚9位

乌干达9

要求:根据上面四国的手机号位数,提取出正确的手机号,然后再格式化输出

Input

第一行输入客户的手机号

如果客户输入+2340794120365,那么我们提取的9位手机号就是794120365,提取的10位手机号是0794120365

如果客户输入+0794120365,那么我们提取的9位手机号就是794120365,提取的10位手机号是0794120365

如果客户输入00000794120365,那么我们提取的9位手机号就是794120365,提取的10位手机号是0794120365,因为我们认为手机号前面可以有0,但也要获取到正确的手机号

如果客户输入+2345abc,那么是无法提取到合法的手机号的

总之,客户输入的是一个20位以内的任意字符,我们需要根据规则提取出有效的手机号

Output

提取到手机号后,按尼日、卢旺达、肯尼亚、乌干达顺序,输出格式化后的手机号

输出格式为:国际区号 + 手机号,其中肯尼亚国际区号不带+号,其他国必需带+号

Sample Input 1
0794120365
Sample Output 1
+2340794120365
+250794120365
254794120365
+256794120365
Sample Input 2
+234794120365
Sample Output 2
+250794120365
254794120365
+256794120365
Sample Input 3
+240794120365
Sample Output 3
无
无
无
无
Sample Input 4
250787678
Sample Output 4
+250250787678
254250787678
+256250787678
Sample Input 5
0012345678
Sample Output 5
+2340012345678254012345678
+256012345678

3. 斐波那契数列拆分

Total 【1339】、 AC rate 【0.60%】

Description

现在有一个无限大的斐波那契数列构成的数组arr, 现给定一个整数N和一个整数K, 将N拆分成K个整数n1, n2,…,nk , (可以拆分成两个相同的数,例如 N= 10 K= 2 就可以拆分成 n1 = 5 , n2 = 5) , 并且n1, n2,…,nk都属于arr , 如果可以满足这种拆分返回Yes, 否则返回No.

Input
输入顺序为:

整数N 整数K,其中整数N和K之间用一个空格分隔,满足条件(1≤N<10^9 ,1≤K≤1000)

Output
输出为Yes 或者No
Sample Input 1
10 2
Sample Output 1
Yes
Sample Input 2
10 1
Sample Output 2
No
Sample Input 3
12 3
Sample Output 3
Yes
Sample Input 4
12 12
Sample Output 4
Yes

你可能感兴趣的:(笔试题,c语言,算法)