笔试题 (18/8/12)

1. 在一个字符串中找出第一个只出现一次的字符   如输入 abgdab  输出 g

 1 #include <iostream>
 2 #include <map>
 3 #include <string>
 4 
 5 //在一个字符串中找出第一个只出现一次的字符如输入  abaccdeff 输出b
 6 using namespace std;
 7 char find_first(string & str)//使用map会把字母排序
 8 {
 9     map<char,size_t>char_count;
10     for(auto&c:str)
11     ++char_count[c];
12     int i=0;
13     for(auto&c:str)
14     {    
15         cout<<c<<endl;
16         auto iter=char_count.find(c);//根据字符串进行查找
17         if(iter!=char_count.end())
18         if(iter->second==1)
19         return iter->first;
20     }
21 }
22 char find_first1(string&str)
23 {
24     int s[256]={0};
25     int i;
26     for(auto&c:str)
27     {    
28         cout<<c<<endl;
29         s[c-'a']+=1;
30     }
31     cout<<"finding...........\n";
32     for(auto &i:str)
33     {
34         cout<<i<<endl;
35     if(s[i-'a']==1)
36     return i;
37     }
38 }
39 int main(int argc ,char*argv[])
40 {
41     string str(argv[1]);
42     char c=find_first(str);
43     cout<<"the first occurance char is "<<c <<endl;
44     
45     return 0;
46 }
47     

2. 给定+ *()运算符 输入3个数字 找出组合起来和最大的数字 

例如:输入 1 2 3输出为(1+2)*3=9

 1 #include <iostream>
 2 int find_max(int a,int b,int c)
 3 {
 4     int max=a+b+c;
 5     if((a*b*c)>max)
 6     max=a*b*c;
 7     if((a+b)*c>max)
 8     max=(a+b)*c;
 9     if(a*(b+c)>max)
10     max=a*(b+c);
11     if((a+c)*b>max)
12     max=(a+c)*b;
13     return max;
14 }
15 int main()
16 {
17     int a,b,c;
18     std::cin>>a>>b>>c;
19     int max=find_max(a,b,c);
20     std::cout<<"the max result is :"<<max<<endl;
21     return 0;
22 }

猜你喜欢

转载自www.cnblogs.com/zydark/p/9464138.html