校招算法笔面试 | 华为机试-句子逆序

题目## 题目## 题目

题目链接

解题思路

  1. 读取输入的句子
  2. 将句子按空格分割成单词列表
  3. 反转单词列表
  4. 将反转后的单词列表重新组合成字符串
  5. 输出结果

代码

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

int main() {
    string line;
    getline(cin, line);
    
    istringstream iss(line);
    vector<string> words;
    string word;
    
    // 分割单词
    while (iss >> word) {
        words.push_back(word);
    }
    
    // 反转单词列表
    reverse(words.begin(), words.end());
    
    // 输出结果
    for (size_t i = 0; i < words.size(); ++i) {
        if (i > 0) cout << " ";
        cout << words[i];
    }
    cout << endl;
    
    return 0;
}
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        
        // 分割单词
        String[] words = line.split(" ");
        
        // 反转单词列表
        Collections.reverse(Arrays.asList(words));
        
        // 输出结果
        System.out.println(String.join(" ", words));
    }
}
# 读取输入
s = input()

# 分割单词并反转
words = s.split()
words.reverse()

# 输出结果
print(" ".join(words))

算法及复杂度

  • 算法:字符串分割、列表反转、字符串拼接
  • 时间复杂度: O ( n ) \mathcal{O}(n) O(n) - 其中 n n n为字符串长度
  • 空间复杂度: O ( n ) \mathcal{O}(n) O(n) - 需要存储分割后的单词列表

题目链接

解题思路

  1. 读取浮点数输入
  2. 获取小数部分:
    • 可以通过减去整数部分得到
    • 或使用语言内置函数
  3. 判断小数部分:
    • 如果大于等于0.5,向上取整
    • 如果小于0.5,向下取整
  4. 输出结果

代码

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double num;
    cin >> num;
    
    cout << round(num) << endl;
    
    return 0;
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double num = sc.nextDouble();
        
        System.out.println(Math.round(num));
    }
}
num = float(input())
print(int(num+0.5))

算法及复杂度

  • 算法:简单的条件判断或使用语言内置的四舍五入函数
  • 时间复杂度: O ( 1 ) \mathcal{O}(1) O(1) - 只需要常数时间完成计算
  • 空间复杂度: O ( 1 ) \mathcal{O}(1) O(1) - 只需要常数级额外空间

题目链接

解题思路

  1. 题目要求:

    • 解析类似xcopy命令的参数
    • 参数之间用空格分隔
    • 对于带引号的参数,需要作为一个完整参数处理
    • 输出参数个数和每个参数值
  2. 解题方法:

    • 使用状态机处理字符串
    • 特别处理引号内的内容
    • 按规则分割参数

代码

def parse_command(command):
    params = []
    current_param = []
    in_quotes = False
    
    for char in command:
        if char == '"':  # 处理引号
            in_quotes = not in_quotes
        elif char == ' ' and not in_quotes:  # 空格且不在引号内
            if current_param:
                params.append(''.join(current_param))
                current_param = []
        else:  # 普通字符
            current_param.append(char)
    
    # 处理最后一个参数
    if current_param:
        params.append(''.join(current_param))
    
    # 输出结果
    print(len(params))
    for param in params:
        print(param)

while True:
    try:
        command = input().strip()
        parse_command(command)
    except:
        break
import java.util.*;

public class Main {
    public static void parseCommand(String command) {
        List<String> params = new ArrayList<>();
        StringBuilder currentParam = new StringBuilder();
        boolean inQuotes = false;
        
        for (char c : command.toCharArray()) {
            if (c == '"') {  // 处理引号
                inQuotes = !inQuotes;
            } else if (c == ' ' && !inQuotes) {  // 空格且不在引号内
                if (currentParam.length() > 0) {
                    params.add(currentParam.toString());
                    currentParam = new StringBuilder();
                }
            } else {  // 普通字符
                currentParam.append(c);
            }
        }
        
        // 处理最后一个参数
        if (currentParam.length() > 0) {
            params.add(currentParam.toString());
        }
        
        // 输出结果
        System.out.println(params.size());
        for (String param : params) {
            System.out.println(param);
        }
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String command = sc.nextLine().trim();
            parseCommand(command);
        }
    }
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void parseCommand(string command) {
    vector<string> params;
    string currentParam;
    bool inQuotes = false;
    
    for (char c : command) {
        if (c == '"') {  // 处理引号
            inQuotes = !inQuotes;
        } else if (c == ' ' && !inQuotes) {  // 空格且不在引号内
            if (!currentParam.empty()) {
                params.push_back(currentParam);
                currentParam.clear();
            }
        } else {  // 普通字符
            currentParam += c;
        }
    }
    
    // 处理最后一个参数
    if (!currentParam.empty()) {
        params.push_back(currentParam);
    }
    
    // 输出结果
    cout << params.size() << endl;
    for (const string& param : params) {
        cout << param << endl;
    }
}

int main() {
    string command;
    while (getline(cin, command)) {
        parseCommand(command);
    }
    return 0;
}

算法分析

  • 算法:状态机
  • 时间复杂度: O ( n ) \mathcal{O}(n) O(n),其中 n n n 为输入字符串长度
  • 空间复杂度: O ( n ) \mathcal{O}(n) O(n),需要存储解析后的参数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值