N = 輸入多少嘗試;(第一行)。
s = 輸入可以增加多少價值;(二、四、六)。
P = 以空格分隔的數字輸入。
Example :
3 ( Input N )
2 ( s 1 )
2 3
3 ( s 2 )
1 2 3
1 ( s 3 )
12
Example :
Read #1: 5 (Output s1 = 2 3)
Read #2: 6 (Output s2 = 1 2 3)
Read #3: 12 (Output s3 = 12)
我一直在尋找和嘗試很長時間,但無法弄清楚如何根據給定的數字、空格以及將所有值添加到變數中來進行 cin 等基本操作。
For example
#include <iostream>
using namespace std;
int main(){
int l, o[l], r, p[r], i;
cin>>l;
for(i = 0; i<l; i ){
cin>>o[l];
r = o[l]; // for every o[0] to o[l]
}
while{cin>>o[l]){
for(i = 0; i<l; i ){
cin>>p[o]>> // for every o[0] to o[l]
// i.e o[l] = 1 then 2 values can be added (because it starts from zero)
// input 1 2
// o[1] = {1, 2}
int example = o[1];
cout<< "Read#2: " << example;
}
}
}
并確保它不起作用,而且永遠不會。然后我找到了getline(),忽略了s,只輸入任何最終將添加到數字中的東西,結果它只適用于char字串。我試過scanf,不知道它是如何作業的。所以我想知道這是否與回圈中的 s(values) x 1(column) 矩陣有關,但仍不確定如何制作它。沒有額外的庫或類似的東西的任何簡單的解決方案?提前致謝。
uj5u.com熱心網友回復:
#include <iostream>
using namespace std;
int main() {
int t; //number of attempts
cin >> t;
while(t--) { // for t attempts
int n, s = 0; //number of values and initial sum
cin >> n;
while (n--) { //for n values
int k; //value to be added
cin >> k;
s = k; //add k to sum
}
cout << s << "\n"; //print the sum and a newline
}
return 0;
}
如果您想添加更多詳細資訊(即在第 n 次嘗試時列印 Read#n),您可以隨時使用
for (int i = 1; i <= n; i )
替換 while(t--) 并在嘗試結束時列印
cout << "Read#" << i << ": " << s << "\n";
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/511332.html
標籤:C
