#40619: cpp(建議複製到IDE界面來看,不然會很醜)


1121228@stu.wghs.tp.edu.tw (jhhc)

學校 : 臺北市私立薇閣高級中學
編號 : 266561
來源 : [60.248.154.139]
最後登入時間 :
2024-10-23 13:00:04
k950. easy math | From: [218.172.19.249] | 發表日期 : 2024-06-01 14:33


#include <iostream>//提供輸入輸出功能。
#include <vector>//提供動態陣列功能。
#include <sstream>//提供字符串流功能,用於處理和分割輸入字符串。
#include <cmath>//提供數學函數,例如 pow 用於乘方運算。
using namespace std;//使用標準命名空間,以簡化代碼。

int main (){
  string input;
  getline(cin, input);//讀取整行輸入
  stringstream ss(input);//將輸入字符串轉換為字符串流,便於逐個提取數字
  vector<long long int> numbers;
  long long int number;

  while (ss >> number) {
      numbers.push_back(number);
  }//從字符串流中提取數字,並存儲到 numbers 向量中

  if (numbers.size() < 3) {
      cerr << "Invalid input. At least two operands and one result are required." << endl;
      return 1;
  }//如果輸入數字少於三個,輸出錯誤信息並結束程序

  long long int result = numbers.back();//將輸入的最後一個數字作為結果存儲在 result 中
  numbers.pop_back();//使用 pop_back() 將最後一個數字從 numbers 向量中移除,剩下的數字即為操作數

  vector<string> operators = {"+", "-", "*", "/", "**"};//向量存儲所有可能的操作符
  vector<string> found_ops;//向量將存儲符合條件的操作符

  if (numbers.size() == 2) {
      long long int a = numbers[0], b = numbers[1];
      if (a + b == result) {
          found_ops.push_back("+");
      }
      if (a - b == result) {
          found_ops.push_back("-");
      }
      if (a * b == result) {
          found_ops.push_back("*");
      }
      if (b != 0 && a / b == result) {
          found_ops.push_back("/");
      }
      if (pow(a, b) == result) {
          found_ops.push_back("**");
      }
  }//檢查兩個數字的情況,分別對應加、減、乘、除和乘方操作符進行判斷並將符合條件的操作符添加到 found_ops 中

  if (numbers.size() == 3) {
      long long int a = numbers[0], b = numbers[1], c = numbers[2];
      if (a + b + c == result) {
          found_ops.push_back("+");
      }
      if (a - b - c == result) {
          found_ops.push_back("-");
      }
      if (a * b * c == result) {
          found_ops.push_back("*");
      }
      if (b != 0 && c != 0 && a / b / c == result) {
          found_ops.push_back("/");
      }
      if (pow(pow(a, b), c) == result) {
          found_ops.push_back("**");
      }
  }//現在換三個數字

  for (const string &op : found_ops) {
      cout << op << endl;
  }//將所有符合條件的操作符輸出

  return 0;
}

 
#40636: Re: cpp(建議複製到IDE界面來看,不然會很醜)


1121226@stu.wghs.tp.edu.tw (Arthur1121226)

學校 : 臺北市私立薇閣高級中學
編號 : 252772
來源 : [60.248.154.139]
最後登入時間 :
2024-10-23 12:49:34
k950. easy math | From: [60.248.154.143] | 發表日期 : 2024-06-03 11:15

👍
#include //提供輸入輸出功能。
#include //提供動態陣列功能。
#include //提供字符串流功能,用於處理和分割輸入字符串。
#include //提供數學函數,例如 pow 用於乘方運算。
using namespace std;//使用標準命名空間,以簡化代碼。

int main (){
  string input;
  getline(cin, input);//讀取整行輸入
  stringstream ss(input);//將輸入字符串轉換為字符串流,便於逐個提取數字
  vector numbers;
  long long int number;

  while (ss >> number) {
      numbers.push_back(number);
  }//從字符串流中提取數字,並存儲到 numbers 向量中

  if (numbers.size() < 3) {
      cerr << "Invalid input. At least two operands and one result are required." << endl;
      return 1;
  }//如果輸入數字少於三個,輸出錯誤信息並結束程序

  long long int result = numbers.back();//將輸入的最後一個數字作為結果存儲在 result 中
  numbers.pop_back();//使用 pop_back() 將最後一個數字從 numbers 向量中移除,剩下的數字即為操作數

  vector operators = {"+", "-", "*", "/", "**"};//向量存儲所有可能的操作符
  vector found_ops;//向量將存儲符合條件的操作符

  if (numbers.size() == 2) {
      long long int a = numbers[0], b = numbers[1];
      if (a + b == result) {
          found_ops.push_back("+");
      }
      if (a - b == result) {
          found_ops.push_back("-");
      }
      if (a * b == result) {
          found_ops.push_back("*");
      }
      if (b != 0 && a / b == result) {
          found_ops.push_back("/");
      }
      if (pow(a, b) == result) {
          found_ops.push_back("**");
      }
  }//檢查兩個數字的情況,分別對應加、減、乘、除和乘方操作符進行判斷並將符合條件的操作符添加到 found_ops 中

  if (numbers.size() == 3) {
      long long int a = numbers[0], b = numbers[1], c = numbers[2];
      if (a + b + c == result) {
          found_ops.push_back("+");
      }
      if (a - b - c == result) {
          found_ops.push_back("-");
      }
      if (a * b * c == result) {
          found_ops.push_back("*");
      }
      if (b != 0 && c != 0 && a / b / c == result) {
          found_ops.push_back("/");
      }
      if (pow(pow(a, b), c) == result) {
          found_ops.push_back("**");
      }
  }//現在換三個數字

  for (const string &op : found_ops) {
      cout << op << endl;
  }//將所有符合條件的操作符輸出

  return 0;
}



 
ZeroJudge Forum