#include<iostream> #include<algorithm> #include<string> #include<cmath> using namespace std; struct node { char n = 0; node* next = nullptr; }; node* shuru(string a) { node* head = new node{ 0,NULL}; node* p; int m; m= a.length(); p = head; for(int i=m-1;i>=0;i--) { node* q = new node; p->next = q; q->n = a[i]; p = q; q->next = nullptr; } return head; } void show(node* m) { m = m->next;//head是默认值,不show while (m!=nullptr) { cout << m->n; m = m->next; } } node* add(node* a, node* b) { node* add, * head = new node{ 0,NULL }, * tail; node* p, * q; p = a; q = b; tail = head; if ((a->n == '-' && b->n != '-') || (b->n == '-' && a->n != '-')) { } else if (a->n != '-' && b->n != '-') { int carry = 0; while (1) { int val = 0; if (p) { val += p->n - '0'; p = p->next; } if (q) { val += q->n - '0'; q = q->next; } /*if (p && q == nullptr) { val += p->n - '0'; p = p->next; } if (q && p == nullptr) { val += q->n - '0'; q = q->next; }*/ val = val + carry; if (val == 0) break; add = new node{ val % 10+'0' ,NULL}; carry = val / 10; tail->next = add; tail = add; } return head; } else { add = new node{ '-',NULL }; tail->next = add; } } int main() { int a; cin >> a; for (int i = 0; i <a; i++) { string m, n; cin >> m >> n; show(add(shuru(m), shuru(n))); } }为什么输出很奇怪
时间: 2024-01-19 22:04:06 浏览: 164
这段代码的输出很奇怪是因为在输出结果时,没有换行符。可以在 `show` 函数中添加一个换行符,如下所示:
```c++
void show(node* m) {
m = m->next;
while (m != nullptr) {
cout << m->n;
m = m->next;
}
cout << endl; // 添加换行符
}
```
这样就可以在输出结果后换行,使输出结果更加清晰易读。
相关问题
#include <iostream> #include <fstream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <climits> #include <string> #include <algorithm> #include <vector> #include <map> using namespace std; struct Value { char str[10000]; }; void structSort(Value *a, int n) { @你的代码 } int n; Value a[5000]; int main() { scanf("%d", &n); for (int i = 0; i<n; i++) { scanf("%s", a[i].str); } structSort(a, n); for (int i = 0; i<n; i++) { printf("%s\n", a[i].str); } return 0; }
这段代码给出了一个结构体数组 `a`,其中每个结构体包含一个字符串成员 `str`。接下来有一个函数 `structSort`,它需要你来实现。你需要使用任何一种排序算法对 `a` 数组进行排序,排序的依据是字符串的字典序(即按照字母顺序进行比较)。最后将排序后的字符串数组依次输出即可。
以下是可能的 `structSort` 实现方式之一,使用了标准库中的 `qsort` 函数:
```c++
int compare(const void *a, const void *b) {
return strcmp(((Value *)a)->str, ((Value *)b)->str);
}
void structSort(Value *a, int n) {
qsort(a, n, sizeof(Value), compare);
}
```
其中,`compare` 函数用于比较两个字符串的大小,将其作为参数传递给 `qsort` 函数进行排序。
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<ctime> #include<iostream> #include<algorithm> #include<map> #include<stack> #include<queue> #include<vector> #include<set> #include<string> #define dd double #define ll long long dd PI = acos(-1); using namespace std; const ll MAXN = 1e5 + 5; const ll INF = 1e9 + 5; ll n; struct node { ll x, y; }s[5000]; ll dp[MAXN] = { 0 }; int main() { //ios::sync_with_stdio(false); ll n, m; cin >> n >> m; for (ll i = 1; i <= n; i++) { cin >> s[i].x >> s[i].y; } for (ll i = 1; i <= n; i++) { for (ll j = m; j >= s[i].x; j--) { dp[j] = max(dp[j], dp[j - s[i].x] + s[i].y); } } cout << dp[m] << endl; }
这是一段动态规划求解背包问题的代码,用来求解一个容量为m的背包能够装下的最大价值。具体实现过程为:
1. 定义一个结构体node,用来存储物品的重量和价值。
2. 读入物品数量n和背包容量m,然后读入每个物品的重量和价值。
3. 使用二重循环,遍历每一个物品,然后从大到小枚举背包容量j,更新dp[j]的值,表示容量为j的背包能够装下的最大价值。
4. 最后输出dp[m]的值,即容量为m的背包能够装下的最大价值。
值得注意的是,这段代码中还定义了一些常量和宏定义,如MAXN、INF等,以及一些头文件和命名空间的使用。
阅读全文
相关推荐















