使用c++将数据分为100份
时间: 2023-08-07 16:04:05 浏览: 35
这里提供一种使用 C++ 的方式将数据分为 100 份。
假设你有一个长度为 n 的数组 a,可以按照如下步骤将其分为 100 份:
```c++
#include <vector>
std::vector<std::vector<int>> divideInto100Parts(std::vector<int> a) {
std::vector<std::vector<int>> result(100);
int count = a.size() / 100;
int r = a.size() % 100;
int index = 0;
for (int i = 0; i < 100; ++i) {
int size = count + (i < r ? 1 : 0);
result[i].resize(size);
for (int j = 0; j < size; ++j) {
result[i][j] = a[index++];
}
}
return result;
}
```
这个函数会返回一个大小为 100 的 vector,其中每个元素也是一个 vector,表示原始数据 a 被分成的 100 份。如果原始数据 a 的大小不能被 100 整除,那么剩余的部分会被放入前面的一些份中。
示例代码:
```c++
#include <iostream>
#include <vector>
int main() {
std::vector<int> a {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
std::vector<std::vector<int>> parts = divideInto100Parts(a);
for (const auto& v : parts) {
for (const auto& x : v) {
std::cout << x << " ";
}
std::cout << std::endl;
}
return 0;
}
```
输出结果为:
```
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20
...
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)