/*=====================================================*\ 第6題(數組) 騰訊面試題: 給你10分鐘時間,根據上排給出十個數,在其下排填出對應的十個數 要求下排每一個數都是先前上排那十個數在下排出現的次數。 上排的十個數以下: 【0,1,2,3,4,5,6,7,8,9】ios
舉一個例子, 數值: 0,1,2,3,4,5,6,7,8,9 分配: 6,2,1,0,0,0,1,0,0,0 0在下排出現了6次,1在下排出現了2次, 2在下排出現了1次,3在下排出現了0次.... 以此類推.. \*=====================================================*/面試
#include <iostream> using namespace std; void print(int *a,int length){ for(int i = 0;i < length;++i){ cout << a[i] << " "; } cout << endl; } int scan(int *a,int *b,int length){ int key = 0; for (int index = 0 ; index < length ; ++index) { int sum = 0; for (int i = 0;i < length;++i) { if(b[i]==a[index]) ++sum; //第二行爲對應第一行值的個數 } if (b[index]==sum) //若是此時至相等 { ++key; }else{ //不等則更新 b[index] = sum; } } cout<<key<<endl; return key; } int main(){ int a[10] = {0,1,2,3,4,5,6}; int b[10] = {0,0,0,0,0,0,0}; int length = 7; while(scan(a,b,length)!=length); print(a,length); print(b,length); return 0; }