跳到正文

A1z26_

1030647

afo

在洛谷打开
资料、奖项与版本

资料

编号
1030647
抓取

奖项

2025 CSP-J
一等奖
2025 CSP-S
二等奖
2024 CSP-J
一等奖

florr:A1Z26 lv65 3u ~50kdps Un

C++
#include <iostream>
#include <fstream>
#include <string>
#include <cstdint>
#include <sstream>
#include <iomanip>
#include <vector>

using namespace std;

// 循环右移函数
inline uint32_t rotr(uint32_t x, int n) {
    return (x >> n) | (x << (32 - n));
}

// 选择函数
inline uint32_t ch(uint32_t x, uint32_t y, uint32_t z) {
    return (x & y) ^ (~x & z);
}

//  Majority函数
inline uint32_t maj(uint32_t x, uint32_t y, uint32_t z) {
    return (x & y) ^ (x & z) ^ (y & z);
}

// Sigma0函数
inline uint32_t sigma0(uint32_t x) {
    return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22);
}

// Sigma1函数
inline uint32_t sigma1(uint32_t x) {
    return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25);
}

// 小sigma0函数
inline uint32_t s0(uint32_t x) {
    return rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3);
}

// 小sigma1函数
inline uint32_t s1(uint32_t x) {
    return rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10);
}

// SHA-256常量
const uint32_t k[64] = {
    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};

// 对消息进行填充
vector<uint8_t> pad_message(const vector<uint8_t>& message) {
    vector<uint8_t> padded = message;
    
    // 添加填充位 '1'
    padded.push_back(0x80);
    
    // 添加填充位 '0',直到长度 ≡ 56 mod 64
    while (padded.size() % 64 != 56) {
        padded.push_back(0x00);
    }
    
    // 添加原始消息长度(以位为单位)的64位大端表示
    uint64_t message_len_bits = message.size() * 8;
    for (int i = 7; i >= 0; --i) {
        padded.push_back((message_len_bits >> (8 * i)) & 0xFF);
    }
    
    return padded;
}

// 计算SHA-256哈希值
string sha256(const string& input) {
    // 初始化哈希值
    uint32_t h0 = 0x6a09e667;
    uint32_t h1 = 0xbb67ae85;
    uint32_t h2 = 0x3c6ef372;
    uint32_t h3 = 0xa54ff53a;
    uint32_t h4 = 0x510e527f;
    uint32_t h5 = 0x9b05688c;
    uint32_t h6 = 0x1f83d9ab;
    uint32_t h7 = 0x5be0cd19;
    
    // 将输入字符串转换为字节向量
    vector<uint8_t> message(input.begin(), input.end());
    
    // 填充消息
    vector<uint8_t> padded = pad_message(message);
    
    // 处理每个512位的块
    for (size_t i = 0; i < padded.size(); i += 64) {
        // 将512位块分解为16个32位大端字
        uint32_t w[64] = {0};
        for (int j = 0; j < 16; ++j) {
            w[j] = (padded[i + 4*j] << 24) | 
                   (padded[i + 4*j + 1] << 16) | 
                   (padded[i + 4*j + 2] << 8) | 
                   padded[i + 4*j + 3];
        }
        
        // 扩展为64个32位字
        for (int j = 16; j < 64; ++j) {
            w[j] = s1(w[j-2]) + w[j-7] + s0(w[j-15]) + w[j-16];
        }
        
        // 初始化工作变量
        uint32_t a = h0;
        uint32_t b = h1;
        uint32_t c = h2;
        uint32_t d = h3;
        uint32_t e = h4;
        uint32_t f = h5;
        uint32_t g = h6;
        uint32_t h = h7;
        
        // 主循环
        for (int j = 0; j < 64; ++j) {
            uint32_t t1 = h + sigma1(e) + ch(e, f, g) + k[j] + w[j];
            uint32_t t2 = sigma0(a) + maj(a, b, c);
            h = g;
            g = f;
            f = e;
            e = d + t1;
            d = c;
            c = b;
            b = a;
            a = t1 + t2;
        }
        
        // 更新哈希值
        h0 += a;
        h1 += b;
        h2 += c;
        h3 += d;
        h4 += e;
        h5 += f;
        h6 += g;
        h7 += h;
    }
    
    // 将哈希值转换为十六进制字符串
    stringstream ss;
    ss << hex << setfill('0') 
       << setw(8) << h0 
       << setw(8) << h1 
       << setw(8) << h2 
       << setw(8) << h3 
       << setw(8) << h4 
       << setw(8) << h5 
       << setw(8) << h6 
       << setw(8) << h7;
    
    return ss.str();
}

// 从文件读取内容
string read_file(const string& filename) {
    ifstream file(filename, ios::binary);
    if (!file) {
        cerr << "无法打开文件: " << filename << endl;
        return "";
    }
    
    return string((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        cerr << "用法: " << argv[0] << " <输入字符串或文件名>" << endl;
        cerr << "示例: " << argv[0] << " \"Hello World\"" << endl;
        cerr << "      " << argv[0] << " file.txt" << endl;
        return 1;
    }
    
    string input = argv[1];
    
    // 检查是否是文件
    ifstream file_check(input);
    if (file_check.good()) {
        input = read_file(input);
        if (input.empty()) {
            return 1;
        }
        cout << "文件的SHA-256哈希值: " << endl;
    } else {
        cout << "字符串 \"" << argv[1] << "\" 的SHA-256哈希值: " << endl;
    }
    
    cout << sha256(input) << endl;
    
    return 0;
}
共 1 篇
  1. 休闲·娱乐

    进化之路

    这是一个用C++编写的小型进化模拟游戏,代码共436行,耗时5小时完成。游戏核心机制包括: 玩家系统:支持多玩家参与,每个玩家拥有生物数量、三种生存能力(陆地/海洋/天空)及繁殖成功率属性 进化事件:预设20种生物进化事件(如根、茎、叶等),通过属性卡系统触发,随机改变生存能力值或繁殖成功率 生存竞争:每轮根据玩家生存能力值在陆地/海洋/天空三个环境中进行资源竞争,生物数量按比例动态调整 繁殖机制:玩家可主动提升生存能力,繁殖时根据成功率增加生物数量,存在随机变异和进化效果 胜利条件:当玩家同时达到陆地26、海洋17、天空17的生存能力阈值时,视为完成所有进化地块,游戏结束 游戏通过命令行交互实现,包含事件触发、属性卡查看、生存能力提升等玩法,模拟了生物在不同环境中的进化竞争过程。

    1 赞