一道比较水的黑题,被系统左迁到了红题
先给大家看一眼AC记录
接下来话不多说先上代码
// 大数处理工具库 - 用于高精度数值比较和符号判断
#include <bits/stdc++.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <cinttypes>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdalign>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <typeinfo>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
// 去除数值字符串的前导零
// 例如: "00321" → "321", "00.50" → ".50"
std::string removeLeadingZeros(const std::string& num) {
int i = 0;
// 保留最后一个零(防止空字符串)
while (i < num.length() - 1 && num[i] == '0') {
i++;
}
return num.substr(i);
}
// 去除小数部分末尾的零
// 例如: "3.1400" → "3.14", "321.000" → "321"
std::string removeTrailingZeros(const std::string& num) {
if (num.find('.') == std::string::npos) {
return num; // 没有小数部分
}
std::string result = num;
// 从末尾移除连续的零
while (result.back() == '0') {
result.pop_back();
}
// 如果小数点后没有数字,移除小数点
if (result.back() == '.') {
result.pop_back();
}
return result;
}
// 比较两个无符号数的大小
// 返回值: 1(大于), 0(等于), -1(小于)
int compareUnsignedNumbers(const std::string& num1, const std::string& num2) {
std::string n1 = removeLeadingZeros(num1);
std::string n2 = removeLeadingZeros(num2);
// 长度不同时,长度较长的数更大
if (n1.length() != n2.length()) {
return n1.length() > n2.length() ? 1 : -1;
}
// 长度相同时,按字典序比较
return n1.compare(n2);
}
// 分离数值字符串的整数部分和小数部分
// 返回: {整数部分, 小数部分}
std::pair<std::string, std::string> separateIntegerAndFraction(const std::string& num) {
size_t dotPos = num.find('.');
if (dotPos == std::string::npos) {
return {num, ""}; // 没有小数部分
}
return {num.substr(0, dotPos), num.substr(dotPos + 1)};
}
// 补齐两个小数部分的长度(补零)
// 例如: "123" 和 "45" → "123" 和 "450"
void padFraction(std::string& frac1, std::string& frac2) {
size_t len1 = frac1.length();
size_t len2 = frac2.length();
if (len1 < len2) {
frac1.append(len2 - len1, '0'); // 右侧补零
} else if (len2 < len1) {
frac2.append(len1 - len2, '0'); // 右侧补零
}
}
// 比较两个有符号数的大小
// 返回值: 1(大于), 0(等于), -1(小于)
int compareNumbers(const std::string& num1, const std::string& num2) {
bool isNeg1 = num1[0] == '-';
bool isNeg2 = num2[0] == '-';
std::string absNum1 = isNeg1 ? num1.substr(1) : num1;
std::string absNum2 = isNeg2 ? num2.substr(1) : num2;
// 符号不同时,正数大于负数
if (isNeg1 && !isNeg2) {
return -1;
} else if (!isNeg1 && isNeg2) {
return 1;
}
// 符号相同时,比较绝对值
auto [int1, frac1] = separateIntegerAndFraction(absNum1);
auto [int2, frac2] = separateIntegerAndFraction(absNum2);
// 先比较整数部分
int intComp = compareUnsignedNumbers(int1, int2);
if (intComp != 0) {
// 负数比较时,绝对值大的反而小
return isNeg1 ? -intComp : intComp;
}
// 整数部分相同,比较小数部分
padFraction(frac1, frac2);
int fracComp = compareUnsignedNumbers(frac1, frac2);
return isNeg1 ? -fracComp : fracComp;
}
// 判断一个数是正数、负数还是0
// 返回值: 1(正数), -1(负数), 0(零)
int signOfNumber(const std::string& num) {
// 规范化处理:移除前导零和小数末尾的零
std::string trimmed = removeLeadingZeros(num);
trimmed = removeTrailingZeros(trimmed);
// 处理特殊情况:零、负零或空字符串(如"000"或"0.00")
if (trimmed == "0" || trimmed == "-0" || trimmed == "") {
return 0;
}
// 判断符号
if (trimmed[0] == '-') {
return -1;
}
return 1;
}
// 处理输入并输出结果
void processInput(const std::string& input) {
int sign = signOfNumber(input);
if (sign == 1) {
std::cout << "positive" << std::endl;
} else if (sign == -1) {
std::cout << "negative" << std::endl;
} else {
std::cout << "zero" << std::endl;
}
}
// 验证输入是否为有效的数字字符串
// 有效格式:可选符号 + 数字 + 可选小数点 + 可选数字
bool isValidNumber(const std::string& input) {
bool hasDot = false; // 是否出现过小数点
bool hasDigits = false; // 是否包含数字
for (size_t i = 0; i < input.length(); ++i) {
// 允许符号出现在第一位
if (i == 0 && input[i] == '-') {
continue;
}
// 处理小数点
if (input[i] == '.') {
if (hasDot) {
return false; // 不允许多个小数点
}
hasDot = true;
}
// 处理非数字字符
else if (!std::isdigit(input[i])) {
return false;
}
// 记录出现过数字
else {
hasDigits = true;
}
}
return hasDigits; // 至少包含一个数字
}
// 主函数:程序入口点
int main() {
std::string input;
std::cin >> input;
// 验证输入有效性
if (!isValidNumber(input)) {
std::cerr << "Error: Invalid number format" << std::endl;
return 1;
}
processInput(input);
return 0;
}
//一道十分简单的红题,如你所见,AC这道题连250行都用不到awa249行极限AC
关于某些入会说:”
你这个代码纯脱KZ放P,直接输入一个符号不就知道了吗!!!!!!”