2024 年华为机考最新模拟题

本文提供了2024年华为机考的三道编程模拟题,涉及Words、Vowel字母处理和计算字符串重新排列数的算法实现。题目要求在ACM环境中解决,展示了代码片段及相应的输入输出示例。
摘要由CSDN通过智能技术生成

2024 年华为机考最新模拟题

ACM 环境,没有提示,甚至头文件都要自己加。

题目1:Words

描述:
每个句子由多个单词组成,句子中的每个单词的长度都可能不一样,假设每个单词的长度 Ni 为该单词的重量,你需要做的就是给出整个句子的平均重量 V。

输入:
输入只有一行,包含一个字符串S(长度不会超过100),代表整个句子,句子中只包含大小写的英文字母,每个单词之间有一个空格。

输出:
输出句子 S 的平均重量 V(四舍五入保留两位小数)

代码:

#include <iostream>
#include <string>
#include <sstream>
// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.
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 input;
    getline(cin, input);
    stringstream ss(input);
    string word;
    int sum = 0, cnt = 0;
    while(ss >> word)
    {
        sum += word.length();
        cnt++;
    }
    printf("%.2lf\n", (double)sum/cnt);

    return 0;
}

结果:

在这里插入图片描述

题目2:Vowel

描述:
solo 从小就对英文字母非常感兴趣,尤其是元音字母(a,e,i,o,u,A,E,I,O,U),他在写日记的时候都会把元音字母写成大写的,辅音字母则都写成小写,虽然别人看起来很别扭,但是 solo 却非常熟练。你试试把一个句子翻译成 solo 写日记的习惯吧。

输入:
输入一个字符串S(长度不会超过100,只包含大小写的英文字母和空格)。

输出:
按照 solo 写日记的习惯输出翻译后的字符串 S 。

代码:

#include <iostream>
#include <string>

using namespace std;

bool isvowel(char &c)
{
    if(c == 'a' || c == 'A')
        return true;
     if(c == 'e' || c == 'E')
        return true;
    if(c == 'i' || c == 'I')
        return true;
    if(c == 'o' || c == 'O')
        return true;
    if(c == 'u' || c == 'U')
        return true;
    return false;
}
// 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()
{
    string input;
    getline(cin, input);
    for(char &c:input)
    {
        if(isvowel(c))
	        c = toupper(c);
        else
            c = tolower(c);
    }
    cout<< input <<endl;
    // 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);
    return 0;
}

结果:

在这里插入图片描述

题目3:计算字符串重新排列数

描述:
给定一个只包含大写英文字母的字符串 S,要求给出对 S 重新排列的所有不相同的排列数。
如:S 为 ABA,则不同的排列有 ABA、AAB、BAA 三种。

输入:
输入一个长度不超过10的字符串S,我们确保都是大写的。

输出:
输出S重新排列的所有不相同的排列数(包含自己本身)

代码 1:

#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

// 计算阶乘
int factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

// 计算不同排列数
int countUniquePermutations(string S) {
    // 使用哈希表存储每个字符的出现次数
    unordered_map<char, int> freq;
    for (char ch : S) {
        freq[ch]++;
    }

    // 计算总排列数
    int totalPermutations = factorial(S.length());

    // 对重复字符的排列数进行调整
    for (auto& p : freq) {
        if (p.second > 1) {
            totalPermutations /= factorial(p.second);
        }
    }

    return totalPermutations;
}
// 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()
{
    string s;
    cin>>s;
    cout<<countUniquePermutations(s)<<endl;
    // 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);
    return 0;
}

代码 2:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<bool> visited;
// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.
// 辅函数
void backtrack(string &input, int level, vector<string> &ans, string &seq)
{
	if (level == input.size())
	{
		ans.emplace_back(seq);
		return;
	}
	for (size_t i = 0; i < input.size(); i++)
	{
		if (visited[i] || (i > 0 && input[i] == input[i - 1] && !visited[i - 1]))
		{
			continue;
		}
		seq.push_back(input[i]);
		visited[i] = true;
		backtrack(input, level + 1, ans, seq);
		visited[i] = false;
		seq.pop_back();
	}
}

int main()
{
	string input;
	cin >> input;

	vector<string> ans;
	string seq;
	visited = vector<bool>(input.size(), false);
	sort(input.begin(), input.end());
	backtrack(input, 0, ans, seq);
	cout << ans.size() << endl;
	return 0;
}

结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

UestcXiye

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

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

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

打赏作者

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

抵扣说明:

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

余额充值