华为机考模拟题

 一、字符串平均重量

 


// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.

int main()
{
    // please define the C++ input here. For example: int a,b; cin>>a>>b;;
    // please finish the function body here.
    // please define the C++ output here. For example:cout<<____<<endl;
    double total;
    int strNum=0;
    string str;
    while(cin>>str){
        strNum++;
        total+=str.size();
    }
    printf("%.2f",total/strNum);
    return 0;
}

二、solo习惯


// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.

int main()
{
    // please define the C++ input here. For example: int a,b; cin>>a>>b;;
    // please finish the function body here.
    // please define the C++ output here. For example:cout<<____<<endl;
    string str;
    getline(cin,str);
    for(int i=0;i<str.size();i++){
        if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'){
            str[i]=str[i]-('a'-'A');
        }else if(str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U'||str[i]==' '){
           continue;
        }else if(str[i]>='A'&&str[i]<='Z'){
            str[i]=str[i]+('a'-'A'); 
        }
    }
    cout<<str<<endl;
    return 0;
}

三、计算字符串重新排列数

 

 完成67%

#include<algorithm>
#include<string.h>
#include<iostream>
// we have defined the necessary header files here FOR this problem.
// IF additional header files are needed IN your program, please IMPORT here.
int main()
{
    // please define the C input here. FOR EXAMPLE: int n; scanf("%d",&n);
    // please finish the FUNCTION body here.
    // please define the C output here. FOR EXAMPLE: printf("%d",a);
    char num[9];
    cin>>num;
    int n = strlen(num);
    int result=0;
    sort(num,num+n);
    do
    {
     //for(int i=0;i<n;i++){
     //   cout<<num[i]<<" ";
     //   cout<<endl;
     //}
     result++;
    }while(next_permutation(num,num+n));
    
    cout<<result<<endl;
    return 0;
}

 完成97%

//#include <iostream>
//#include <cstring>
// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.
#include<set>
using namespace std;
void AllPermutation(string perm, int from, int to);
int num=0;
set<string> sets;
int main()
{
    // please define the C++ input here. For example: int a,b; cin>>a>>b;;
    // please finish the function body here.
    // please define the C++ output here. For example:cout<<____<<endl;
    string str;
    getline(cin,str);
    AllPermutation(str, 0, str.size()-1);
    cout<<sets.size()<<endl;
    return 0;
}


void AllPermutation(string perm, int from, int to)
{
    if(from > to)
        return;
            
    if(from == to)     //打印当前排列 
    {	
        static int count = 1;    //局部静态变量,用来统计全排列的个数  
        num=count;
        count++;
        sets.insert(perm);
		//cout << count++ << ":" << perm;
        //cout << endl;
    }
    if(from < to)     //用递归实现全排列 
    {
        for(int j = from; j <= to; j++)    //第j个字符分别与它后面的字符交换就能得到新的排列
        {
                swap(perm[j], perm[from]);
            //cout<<0;
            AllPermutation(perm, from + 1, to);
            //cout<<1;
            swap(perm[j], perm[from]);
            //cout<<2;
            
        }
    }
}

100% 


// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
    // please define the C++ input here. For example: int a,b; cin>>a>>b;;
    // please finish the function body here.
    // please define the C++ output here. For example:cout<<____<<endl;

    string str;
    int count=0;
    getline(cin,str);
    //没有sort好像不行
    sort(str.begin(),str.end());
	do{
		//cout<<str<<endl;
		count++;
	}while(next_permutation(str.begin(),str.end()));
    
    cout<<count<<endl;
    return 0;
}



  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南叔先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值