--- tags: uva --- # Uva12640 - Largest Sum Game ## 題目大意 題目會給我們一行一個數字序列,要我們找出隨意選擇其中的數相加最大是多少 ## 重點觀念 - getline 與 istringstream 運用 ## 分析 - 此題輸入比較難處理,要用 getline 取得整行的 string 之後再將 string 轉為 istringstream 比較好處理 - 後續處理就一直加與最大比較,遇到 0 則將 sum 定為 0 ## 程式題目碼 ```cpp= #include <iostream> #include <sstream> using namespace std; int main() { string line; while (getline(cin, line)) { istringstream istream(line); int num; int sum = 0; int max_sum = 0; while (istream >> num) { sum += num; if (sum < 0) { sum = 0; } max_sum = max(max_sum, sum); } cout << max_sum << endl; } return 0; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up