/* * 長度 3* 數組 1 2 3* 注意:元素之間以空格相隔
*/int length = 0;std::cin >> length;getchar();std::vector<int>input_vector{};for (int i = 0; i < length; ++i) {int temp = 0;std::cin >> temp;input_vector.emplace_back(temp);}
- 使用多組?測試程序進行測試,使用for循環作為測試的循環次數,每次更新每組測試數據的長度和對應的存儲?
輸入數據
2 //測試集合的數量
3 //第一個測試集合的長度
1 2 3 //第一個測試集合的具體元素序列
4 //第二個測試集合的長度
1 2 4 5 //第二個測試集合的具體元素序列輸出數據
Yes //第一個測試數據集滿足要求
No //第二個測試數據集不滿足要求
#include<iostream>
#include<vector>
#include <algorithm>//函數 接收vector存儲的數組序列 第二個參數是長度,表示這個數組的長度
bool yes_or_no(std::vector<int>&nums,int length){//將數組進行正序排序std::sort(nums.begin(),nums.end());//對數組是否滿足題目要求進行判定//理論上需要 數組滿足從1開始依次遞增,幅度為1,最大元素為數組的長度for (int i = 1; i <= length; ++i) {if (nums[i-1]!=i){return false;}}return true;
}using namespace std;
int main()
{//num 接收測試數組的個數int num = 0;std::cin >> num;//使用getchar捕獲 輸入長度之后 輸入的 Enter 字符getchar();//定義ans 存儲類型是string,主要目的是存儲 測試數據集是否滿足要求//如果滿足 存儲Yes//如果不滿足 存儲Nostd::vector<std::string>ans{};//依次輸入 測試數據的長度和測試數據集for (int i = 0; i < num; ++i) {int length = 0;std::cin >> length;getchar();std::vector<int>temp_ans{};for (int i = 0; i < length; ++i) {int temp = 0;std::cin >> temp;//每次存儲測試 數據集合temp_ans.push_back(temp);}//每次對測試的數據集合 進行判定//將結果存儲到ans中if (yes_or_no(temp_ans,length)== true){ans.push_back("Yes");} else{ans.push_back("No");}}for (int i = 0; i < num; ++i) {std::cout << ans[i] << std::endl;}return 0;
}
- 如果接收多個參數,比如第一行輸入長度,第二行輸入left,第三行輸入right,第四行輸入 以空格為間隔的元素拼裝而成的數組,需要在第三行和第四行之間使用getchar()函數
- 即,getchar()使用的時候需要考慮正確性,一般使用位置如上述指定的情形,不能使用次數頻繁,比如接收length right left之間使用,會出錯
{
/** 長度 3* 數組 1,2,3* 注意:元素之間以逗號相隔
*/int length = 0;std::cin >> length;getchar();std::string input_string{};getline(std::cin,input_string);istringstream string_turn(input_string);int integer = 0;char temp = 0;std::vector<int>input_vector{};while (string_turn >>integer){input_vector.push_back(integer);string_turn >> temp;}
/** 長度 3* 數組 * 1 2 3* 4 5 6 * 7 8 9* 注意:元素之間以逗號相隔
*/int length = 0;std::cin >> length;getchar();std::vector<std::vector<int>>two_vector{};for (int i = 0; i < length; ++i) {std::vector<int>one_vector{};int temp = 0;for (int j = 0; j < length; ++j) {std::cin >> temp;one_vector.emplace_back(temp);}two_vector.push_back(one_vector);}
第一行是數組,但是不給出數組的長度,第二行是其余的判斷條件
例如
1,2,3,4,5,6,7,8
34int storage[100];
int number;
int main(){int length = 0;for (; ; ++length) {std::cin >>storage[length];if (getchar()=='\n'){break;}}std::cin >> number;length += 1;for (int i = 0; i < length; ++i) {std::cout << storage[i] << " ";}std::cout << "number = "<< number;std::cout << std::endl;std::cout << "the length of storage is "<< length;return 0;
}
給定兩個有序數組A和B,將B合并到數組A中,使得A成為一個有序數組
- 說明:1,初始化A和B的元素數量分別為m和n;2,A具備了足夠的內存空間,空間大小大于或者等于m+n,用于存儲B中的元素;3,默認采用升序的方式
輸入格式m=2,n=2
1,3
2,4
- 使用scanf("m=%d,n=%d",&m,&n);的方式匹配電腦指定的輸入方式
- 輸入數據, data, 作為一個處理單位進行數據的處理,但是最后一個元素只有數據沒有逗號,需要使用 if 進行特殊的處理
#include <iostream>
#include <sstream>
#include <vector>void merge(std::vector<int>& nums1, int m, std::vector<int>& nums2, int n) {int index = m+n-1;m = m-1;n = n-1;while (m >= 0 && n >= 0){if (nums1[m]>=nums2[n]){nums1[index--] = nums1[m--];} else{nums1[index--] = nums2[n--];}}while (m>=0){nums1[index--] = nums1[m--];}while (n>=0){nums1[index--] = nums2[n--];}
}
int main(){int m = 0,n=0;scanf("m=%d,n=%d",&m,&n);std::vector<int>nums1(m+n,0);for (int i = 0; i < m; ++i) {int temp = 0;if(i==m-1){scanf("%d",&temp);} else{scanf("%d,",&temp);}nums1[i] = temp;}// getchar();std::vector<int>nums2(n,0);for (int i = 0; i < n; ++i) {int temp = 0;if(i==n-1){scanf("%d",&temp);} else{scanf("%d,",&temp);}nums2[i] = temp;}merge(nums1,m,nums2,n);for (auto temp : nums1) {std::cout << temp << " ";}std::cout << std::endl;for (auto temp : nums2) {std::cout << temp << " ";}return 0;
}
C++初始化 二維數組
- vector<vector<int>> arr1(cow, vector<int>(column, 0));
- 其中cow表示行 column表示列,初始數值為0
- 參考鏈接
未給出矩陣的行數和列數
有些輸入可能是:
輸入一個矩陣,每行以空格分隔。
3 2 3
1 6 5
7 8 9
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main() {vector<vector<int>> arr;string input;while (getline(cin, input)) {if (input.size() > 0) {stringstream stringin(input);int num;vector<int> a;while (stringin >> num) {a.push_back(num);}arr.push_back(a);}}// 使用自測數據按鈕時調試用,正式提交時要刪掉。cout << "rows: " << arr.size() << ", cols: " << arr[0].size() << endl;for (int i=0; i<arr.size(); i++) {for (int j=0; j<arr[i].size(); j++) {cout << arr[i][j] << " ";}cout << endl;}
}
數組中涵蓋中括號和括號
有些輸入可能是,輸入一個矩陣:
[[3,2,3],[1,6,5],[7,8,9]]
- 對于這種沒有給定矩陣行列數的輸入,而且還包含中括號和逗號的輸入,我們也是只能按照字符串拆分來進行
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
#include <sstream>
using namespace std;
int main() {vector<vector<int>> arr;string input;char *tok;while (getline(cin, input)) {if (input.size() > 0) {vector<int> a;tok = strtok((char *)input.c_str(), " ,[]");while (tok != NULL) {a.push_back(stoi(tok));tok = strtok(NULL, " ,[]");}arr.push_back(a);}}// 使用自測數據按鈕時調試用,正式提交時要刪掉。cout << "rows: " << arr.size() << ", cols: " << arr[0].size() << endl;for (int i=0; i<arr.size(); i++) {for (int j=0; j<arr[i].size(); j++) {cout << arr[i][j] << " ";}cout << endl;}
}
?注意事項
讀取輸入時,建議使用scanf代替cin,因為對于大規模數據時,由于cin的內部實現原理,容易超時。
參考鏈接
- C++如何輸入含空格的字符串
- ACMcoder OJ