- C++
A + B Problem 怎么搞啊!!!
- @ 2025-4-3 17:30:45
#include <bits/stdc++.h>
using namespace std;
int main (){
long long a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}
到底哪错了
14 条评论
-
林宝来不是BFS @ 2025-10-24 20:53:36
用python...
-
@ 2025-10-6 21:47:00#include #include #include #include #include #include #include #include #include #include #include <unordered_map> #include <unordered_set> #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include <type_traits> #include #include #include #include #include <forward_list> #include #include #include <initializer_list> #include #include #include #include #include #include #include #include #include #include <condition_variable> #include #include <shared_mutex> #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include <bits/stdc++.h>
template class NumericValue; template class ArithmeticOperation; class InputHandler; class OutputHandler; class CalculationDirector; class CalculationStrategy;
namespace TypeTraits { template struct is_numeric : std::integral_constant<bool, std::is_integral::value || std::is_floating_point::value> {};
template<typename T> constexpr bool is_numeric_v = is_numeric<T>::value; template<typename T, typename = std::enable_if_t<is_numeric_v<T>>> struct NumericLimits { static constexpr T min() { return std::numeric_limits<T>::min(); } static constexpr T max() { return std::numeric_limits<T>::max(); } };}
template class NumericInterface { public: virtual ~NumericInterface() = default; virtual T getValue() const = 0; virtual void setValue(T value) = 0; virtual std::unique_ptr<NumericInterface> clone() const = 0; };
template class NumericValue : public NumericInterface { static_assert(TypeTraits::is_numeric_v, "Type must be numeric");
private: T value_; mutable std::mutex mutex_;
public: NumericValue() : value_(T{}) {} explicit NumericValue(T value) : value_(value) {} NumericValue(const NumericValue& other) { std::lock_guardstd::mutex lock(other.mutex_); value_ = other.value_; }
NumericValue& operator=(const NumericValue& other) { if (this != &other) { std::lock(mutex_, other.mutex_); std::lock_guard<std::mutex> lockThis(mutex_, std::adopt_lock); std::lock_guard<std::mutex> lockOther(other.mutex_, std::adopt_lock); value_ = other.value_; } return *this; } T getValue() const override { std::lock_guard<std::mutex> lock(mutex_); return value_; } void setValue(T value) override { std::lock_guard<std::mutex> lock(mutex_); value_ = value; } std::unique_ptr<NumericInterface<T>> clone() const override { return std::make_unique<NumericValue<T>>(*this); } using Observer = std::function<void(T)>; void addObserver(Observer observer) { std::lock_guard<std::mutex> lock(mutex_); observers_.push_back(observer); }private: std::vector observers_;
void notifyObservers() { T currentValue; { std::lock_guard<std::mutex> lock(mutex_); currentValue = value_; } for (const auto& observer : observers_) { observer(currentValue); } }};
template class OperationInterface { public: virtual ~OperationInterface() = default; virtual T execute(const T& a, const T& b) const = 0; virtual std::string getName() const = 0; virtual std::unique_ptr<OperationInterface> clone() const = 0; };
template class AdditionOperation : public OperationInterface { public: T execute(const T& a, const T& b) const override { if (b >= 0) { T result = a; for (T i = T{}; i < b; ++i) { result += T{1}; } return result; } else { T result = a; for (T i = T{}; i > b; --i) { result -= T{1}; } return result; } }
std::string getName() const override { return "addition"; } std::unique_ptr<OperationInterface<T>> clone() const override { return std::make_unique<AdditionOperation<T>>(*this); }};
class CalculationStrategy { public: virtual ~CalculationStrategy() = default; virtual long long calculate(long long a, long long b) const = 0; virtual std::string getStrategyName() const = 0; };
class SequentialCalculation : public CalculationStrategy { public: long long calculate(long long a, long long b) const override { AdditionOperation addOp; return addOp.execute(a, b); }
std::string getStrategyName() const override { return "sequential"; }};
class ParallelCalculation : public CalculationStrategy { public: long long calculate(long long a, long long b) const override { auto computeA = std::async(std::launch::async, a { std::this_thread::sleep_for(std::chrono::microseconds(1)); return a; });
auto computeB = std::async(std::launch::async, [b]() { std::this_thread::sleep_for(std::chrono::microseconds(1)); return b; }); AdditionOperation<long long> addOp; return addOp.execute(computeA.get(), computeB.get()); } std::string getStrategyName() const override { return "parallel"; }};
class NumberExpression { public: virtual ~NumberExpression() = default; virtual long long interpret() const = 0; virtual std::unique_ptr clone() const = 0; };
class ConstantExpression : public NumberExpression { private: long long value_;
public: explicit ConstantExpression(long long value) : value_(value) {}
long long interpret() const override { return value_; } std::unique_ptr<NumberExpression> clone() const override { return std::make_unique<ConstantExpression>(*this); }};
class StringNumberExpression : public NumberExpression { private: std::string str_;
public: explicit StringNumberExpression(std::string str) : str_(std::move(str)) {}
long long interpret() const override { bool negative = false; size_t pos = 0; if (str_.empty()) { throw std::invalid_argument("Empty string"); } if (str_[0] == '-') { negative = true; pos = 1; } long long result = 0; for (; pos < str_.size(); ++pos) { if (!std::isdigit(static_cast<unsigned char>(str_[pos]))) { throw std::invalid_argument("Invalid character in number string"); } result = result * 10 + (str_[pos] - '0'); } return negative ? -result : result; } std::unique_ptr<NumberExpression> clone() const override { return std::make_unique<StringNumberExpression>(*this); }};
class InputHandler { private: using Token = std::string; std::vector tokens_;
public: void readInput() { std::string line; std::getline(std::cin, line); tokenize(line); }
std::vector<std::unique_ptr<NumberExpression>> parseNumbers() const { std::vector<std::unique_ptr<NumberExpression>> expressions; for (const auto& token : tokens_) { expressions.push_back(std::make_unique<StringNumberExpression>(token)); } if (expressions.size() != 2) { throw std::runtime_error("Expected exactly two numbers"); } return expressions; }private: void tokenize(const std::string& input) { std::istringstream iss(input); Token token;
while (iss >> token) { tokens_.push_back(token); } }};
class OutputHandler { public: template void printResult(const T& result) const { std::cout << result << std::endl; }
template<typename T> void printDebugInfo(const std::string& message, const T& value) const { }};
class CalculationDirector { private: std::unique_ptr strategy_; OutputHandler outputHandler_;
public: CalculationDirector() : strategy_(std::make_unique()) {}
void setStrategy(std::unique_ptr<CalculationStrategy> strategy) { strategy_ = std::move(strategy); } long long performCalculation(long long a, long long b) const { outputHandler_.printDebugInfo("Using strategy", strategy_->getStrategyName()); return strategy_->calculate(a, b); }};
class NumericFactory { public: template static std::unique_ptr<NumericInterface> createNumeric(T value) { return std::make_unique<NumericValue>(value); } };
class ApplicationManager { private: InputHandler inputHandler_; OutputHandler outputHandler_; CalculationDirector calculationDirector_; static std::unique_ptr instance_; static std::once_flag initFlag_;
ApplicationManager() { initialize(); } friend std::unique_ptr<ApplicationManager> std::make_unique<ApplicationManager>(); void initialize() { std::srand(std::time(nullptr)); if (std::rand() % 2 == 0) { calculationDirector_.setStrategy(std::make_unique<SequentialCalculation>()); } else { calculationDirector_.setStrategy(std::make_unique<ParallelCalculation>()); } }public: ApplicationManager(const ApplicationManager&) = delete; ApplicationManager& operator=(const ApplicationManager&) = delete;
static ApplicationManager& getInstance() { std::call_once(initFlag_, []() { instance_ = std::make_unique<ApplicationManager>(); }); return *instance_; } int run() { try { outputHandler_.printDebugInfo("Application started", ""); inputHandler_.readInput(); auto expressions = inputHandler_.parseNumbers(); long long a = expressions[0]->interpret(); long long b = expressions[1]->interpret(); auto numA = NumericFactory::createNumeric(a); auto numB = NumericFactory::createNumeric(b); long long result = calculationDirector_.performCalculation( numA->getValue(), numB->getValue() ); outputHandler_.printResult(result); outputHandler_.printDebugInfo("Application finished successfully", ""); return 0; } catch (const std::exception& e) { outputHandler_.printDebugInfo("Error occurred", e.what()); return 1; } }};
std::unique_ptr ApplicationManager::instance_ = nullptr; std::once_flag ApplicationManager::initFlag_;
int main() { return ApplicationManager::getInstance().run(); }
-
@ 2025-8-27 11:09:49
#include<bits/stdc++.h> using namespace std; int main(){ long long a,b; cin>>a>>b; string s = to_string(a+b); cout<<s; return 0; }
-
@ 2025-8-27 7:47:13是不是因为没有高精度?(a + b > 2 ^ 64)
-
@ 2025-7-29 18:38:44
语言要选 C++ 系列的
-
@ 2025-7-29 14:02:36
这哪道题,我就是用这个方法A的
-
@ 2025-4-25 18:48:49
不是高精度,贝贝们
-
@ 2025-4-19 10:45:03如果是高精度建议用string或数组模拟加法,当然你也可以使用int 128 (我不建议)
-
@ 2025-4-17 15:47:29有没有一种可能,是高精度a+b
-
@ 2025-4-14 20:54:14有没有一种可能,是高精度a+b。。。
-
@ 2025-4-14 20:53:44没错啊
-
@ 2025-4-11 15:49:46
换Python
-
@ 2025-4-6 9:23:10有没有一种可能,是高精度a+b
- 1