【C++】2018阿里巴巴实习生笔试(C/C++研发岗)

/*题目为求组合数字下标的距离之和,纯粹逻辑题,没有难度,不过博主基础不牢,没写完,数字下标之和最小,只要把相同数字放一块即可*/

#include <iostream>

#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;




/******************************开始写代码******************************/


int calcMinKLenSum(char first[256], char second[256]){
int sum=0;
char *ptr1=first, *ptr2=second;
vector<char> vc;
char cArr[10] = {'0','1','2','3','4',
             '5','6','7','8','9'};
for(int i=0; i<10; i++) vc.push_back(cArr[i]);


map<char, int> m;
set<char> exclude(vc.begin(), vc.end());
while (*ptr1!='\0'){
if(exclude.find(*ptr1) != exclude.end())
++m[*ptr1];
ptr1++;
}
while (*ptr2!='\0'){
if(exclude.find(*ptr2) != exclude.end())
++m[*ptr2];
ptr2++;
}


map<char, int>::iterator it;
for(it=m.begin(); it!=m.end(); it++)
sum += it->second -1;


return sum;
}


/******************************结束写代码******************************/


int main() {
char in[256], first[256]={'\0'}, second[256]={'\0'};
char* ptr;
int i=0;
gets_s(in, 256);


ptr=in;
while (*ptr!=' '){
first[i++]=*ptr;
ptr++;
}

ptr++;i=0;
while (*ptr!='\0'){
second[i++]=*ptr;
ptr++;
}


    int sum = calcMinKLenSum(first, second);
    cout << sum << endl;


system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u013346007/article/details/80287007