#1.Hello World 题解 审核通过

CPP 刷题王 2024-03-21 17:36:21 2024-03-21 17:37:44 46

解题思路

一、直接输出

这道题目就是顺序结构的经典例题,这里不再赘述。

CODE:

#include <stdio.h>
int main() {
    puts("Hello World");
}

但是假如这样写,大家一定会觉得太简单了,不屑于去做,于是作者 为了让告诉广大的读者即使是简单题目也是可以钻研的 便给出了另一种解法。

二、用 ASCII 表

存储每个字符对应的 ASCII 编号,然后再统一输出。

CODE:

#include <stdio.h>
int a[] = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 };  //含有恶臭数字
int main() {
    for (int i = 0; i < 11; i++) printf("%c", char(a[i]));
    return 0;
}

但是,这样的算法也没有难度,于是作者又给出了新的解法。

三、用三标符

CODE:

??=include<stdio.h>
%:include<stdio.h>
int main()??<
	char* a??(1??);
	a??(0??)=<%"Hello World"%>;
	printf("%s??/n",a<:0:>);
	return 0;
??>

四、字典树

CODE:

#include <iostream>
using namespace std;

struct TrieNode {
    TrieNode* children[26];
    bool isEndOfWord;
};

TrieNode* getNode() {
    TrieNode* node = new TrieNode;
    for (int i = 0; i < 26; i++) {
        node->children[i] = nullptr;
    }
    node->isEndOfWord = false;
    return node;
}

void insertWord(TrieNode* root, string word) {
    TrieNode* current = root;
    for (char c : word) {
        int index = c - 'a';
        if (current->children[index] == nullptr) {
            current->children[index] = getNode();
        }
        current = current->children[index];
    }
    current->isEndOfWord = true;
}

void printTrie(TrieNode* root) {
    if (root->isEndOfWord) {
        cout << "Hello World" << endl;
        return;
    }
    for (int i = 0; i < 26; i++) {
        if (root->children[i] != nullptr) {
            printTrie(root->children[i]);
        }
    }
}

int main() {
    TrieNode* root = getNode();
    insertWord(root, "helloworld");
    printTrie(root);
    return 0;
}
{{ vote && vote.total.up }}

共 2 条回复

jiangshuhao14

用你的方法做了一下,还真对了

kj123

666