杭厦地区实习生机考体验测试2015(最大数)

随机给定10个小于10的数字(出现的数字可能重复),从其中取出3个各不相同的数字可组合出多个不同的3位数,请输出这些3位数中最大的那个3位数。

样例输入:1 2 3 4 5 6 7 8 9 0

样例输出:987

 

算法思路:

先随机输出10个整数,范围[0,9];

然后排序,从大到小,用了sort函数,在<algorithm>头文件里;

然后删除数组中重复数字,这一步卡了很久!!基础功啊基础功!

最后取数组前三个数,组成一个三位数,Down!

 

 1 #include "stdafx.h"
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <functional>
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int MyNums[10] = {0};
11     for(int i=0; i<10; ++i)
12     {
13         int temp = rand();
14         if( (temp>=0) && (temp<=9) )
15             MyNums[i] = temp;
16         else --i;
17     }
18 
19     cout<<"10个小于10的数字:"<<endl;
20     for(int i=0; i<10; ++i)
21         cout<<MyNums[i]<<" ";
22     cout<<endl;
23 
24     int* p = MyNums;
25     sort(p,p+10,greater<int>());
26 
27     cout<<"排序后:"<<endl;
28     for(int i=0; i<10; ++i)
29         cout<<MyNums[i]<<" ";
30     cout<<endl;
31 
32     static int j=0;
33     for(int i=0;i<10;i++)
34     {
35         while(MyNums[i] == MyNums[i+1])    
36             i++;            
37         MyNums[j++] = MyNums[i];                    
38     }
39 
40     cout<<"删除后:"<<endl;
41     for(int i=0; i<j; ++i)
42         cout<<MyNums[i]<<" ";
43     cout<<endl;
44 
45     cout<<"最大的三位数:"<<(100*MyNums[0]+10*MyNums[1]+MyNums[2])<<endl;
46 
47     return 0;
48 }

 

posted on 2015-04-01 12:50  夏大兔  阅读(200)  评论(0编辑  收藏  举报

导航