pre.begin()+1,pre.begin()+i+1
时间: 2024-05-26 21:18:20 浏览: 9
这是C++中迭代器的用法,`pre`是一个迭代器对象,`begin()`是返回迭代器指向容器的第一个元素的位置,`+1`表示将迭代器向后移动一个位置。因此,`pre.begin()+1`就是指向容器的第二个元素的位置。
同理,`pre.begin()+i+1`表示将迭代器向后移动`i+1`个位置,指向容器中第`i+2`个元素的位置。其中,`i`应该是一个整型变量或表达式。
相关问题
vector<int>leftpre(pre.begin()+1,pre.begin()+i+1)
这行代码是将一个vector(或者数组)的前i个元素截取下来,然后构造一个新的vector。其中,pre是原始的vector,begin()指向vector的第一个元素,begin()+1指向vector的第二个元素,begin()+i+1指向vector的第i+1个元素。所以,leftpre就是一个包含原vector前i个元素的新vector。
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; string s; int y, n, pre[10000]; int main() { while (cin >> s >> y) { int sum = 0; for (int i = 0; i < s.length(); i++) { sum += s[i] - '0'; } if (sum == y) { cout << s << '\n'; continue; } if (y == 0) { cout << "-1\n"; continue; } if (y > s.length() * 9) { if (y % 9) cout << y % 9; for (int i = 0; i < y / 9; i++) cout << 9; cout << "\n"; continue; } s = '0' + s; n = s.length(); for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1] + s[i - 1] - '0'; } for (int i = n; i >= 1; i--) { if (s[i - 1] == '9') continue; if (pre[i] < y && y - pre[i - 1] <= 9 * (n - i + 1)) { int less = y - pre[i] - 1; for (int j = n - 1; j >= i; j--) { if (less < 9) { s[j] = '0' + less; less = 0; continue; } if (less == 0) { s[j] = '0'; continue; } s[j] = '9'; less -= 9; } s[i - 1] += less + 1; if (s[0] == '0') s.erase(s.begin()); cout << s << '\n'; break; } } } }转化为c语言
以下是将该段代码转化为 C 语言的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char s[10005];
int y, n, pre[10005];
int main() {
while (scanf("%s %d", s, &y) == 2) {
int sum = 0;
for (int i = 0; i < strlen(s); i++) {
sum += s[i] - '0';
}
if (sum == y) {
printf("%s\n", s);
continue;
}
if (y == 0) {
printf("-1\n");
continue;
}
if (y > strlen(s) * 9) {
if (y % 9)
printf("%d", y % 9);
for (int i = 0; i < y / 9; i++)
printf("9");
printf("\n");
continue;
}
n = strlen(s);
for (int i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + s[i - 1] - '0';
}
for (int i = n; i >= 1; i--) {
if (s[i - 1] == '9') {
continue;
}
if (pre[i] < y && y - pre[i - 1] <= 9 * (n - i + 1)) {
int less = y - pre[i] - 1;
for (int j = n - 1; j >= i; j--) {
if (less < 9) {
s[j] = '0' + less;
less = 0;
continue;
}
if (less == 0) {
s[j] = '0';
continue;
}
s[j] = '9';
less -= 9;
}
s[i - 1] += less + 1;
if (s[0] == '0') {
memmove(s, s + 1, strlen(s));
}
printf("%s\n", s);
break;
}
}
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)