137 条题解
-
0
LCT解法
#include <iostream> #include <cstring> #include <cstdio> #include <cstring> using namespace std; struct node { int data,rev,sum; node *son[2],*pre; bool judge(); bool isroot(); void pushdown(); void update(); void setson(node *child,int lr); }lct[233]; int top,a,b; node *getnew(int x) { node *now=lct+ ++top; now->data=x; now->pre=now->son[1]=now->son[0]=lct; now->sum=0; now->rev=0; return now; } bool node::judge(){return pre->son[1]==this;} bool node::isroot() { if(pre==lct)return true; return !(pre->son[1]==this||pre->son[0]==this); } void node::pushdown() { if(this==lct||!rev)return; swap(son[0],son[1]); son[0]->rev^=1; son[1]->rev^=1; rev=0; } void node::update(){sum=son[1]->sum+son[0]->sum+data;} void node::setson(node *child,int lr) { this->pushdown(); child->pre=this; son[lr]=child; this->update(); } void rotate(node *now) { node *father=now->pre,*grandfa=father->pre; if(!father->isroot()) grandfa->pushdown(); father->pushdown();now->pushdown(); int lr=now->judge(); father->setson(now->son[lr^1],lr); if(father->isroot()) now->pre=grandfa; else grandfa->setson(now,father->judge()); now->setson(father,lr^1); father->update();now->update(); if(grandfa!=lct) grandfa->update(); } void splay(node *now) { if(now->isroot())return; for(;!now->isroot();rotate(now)) if(!now->pre->isroot()) now->judge()==now->pre->judge()?rotate(now->pre):rotate(now); } node *access(node *now) { node *last=lct; for(;now!=lct;last=now,now=now->pre) { splay(now); now->setson(last,1); } return last; } void changeroot(node *now) { access(now)->rev^=1; splay(now); } void connect(node *x,node *y) { changeroot(x); x->pre=y; access(x); } void cut(node *x,node *y) { changeroot(x); access(y); splay(x); x->pushdown(); x->son[1]=y->pre=lct; x->update(); } int query(node *x,node *y) { changeroot(x); node *now=access(y); return now->sum; } int main() { scanf("%d%d",&a,&b); node *A=getnew(a); node *B=getnew(b); connect(A,B); cut(A,B); connect(A,B); printf("%d\n",query(A,B)); return 0; }
-
0
#include<bits/stdc++.h> using namespace std; const int N=100001; int a[N],b[N],ml; void add(int a[],int b[]) { int cnt=0; for(int i=1;i<=ml+1;i++) { cnt+=a[i]+b[i]; a[i]=cnt%10; cnt/=10; } return; } void print(int a[]) { int i; for(i=ml+1;i>0&&!a[i];i--); if(!i) { cout<<0; return; } while(i) { cout<<a[i--]; } } int main() { string sa,sb; cin>>sa>>sb; int la=sa.size(),lb=sb.size(); ml=la>lb?la:lb; for(int i=1;i<=la;i++) { a[i]=sa[la-i]-'0'; } for(int i=1;i<=lb;i++) { b[i]=sb[lb-i]-'0'; } add(a,b); print(a); return 0; }
-
0
A+B Problem
题解+思路
题解
#include <iostream> using namespace std; struct Node { int data; Node *prev; Node *next; Node(int val) : data(val), prev(nullptr), next(nullptr) {} }; Node *createList(int num) { Node *head = nullptr; Node *tail = nullptr; while (num > 0) { int digit = num % 10; Node *newNode = new Node(digit); if (head == nullptr) { head = newNode; tail = newNode; } else { newNode->next = head; head->prev = newNode; head = newNode; } num /= 10; } return head; } Node *addTwoNumbers(Node *num1, Node *num2) { Node *result = nullptr; Node *current = nullptr; int carry = 0; while (num1 != nullptr || num2 != nullptr || carry != 0) { int sum = carry; if (num1 != nullptr) { sum += num1->data; num1 = num1->next; } if (num2 != nullptr) { sum += num2->data; num2 = num2->next; } carry = sum / 10; sum %= 10; Node *newNode = new Node(sum); if (result == nullptr) { result = newNode; current = newNode; } else { current->prev = newNode; newNode->next = current; current = newNode; } } return result; } void printList(Node *head) { if (head == nullptr) { cout << "Empty list" << endl; return; } while (head != nullptr) { cout << head->data; head = head->next; } cout << endl; } void deleteList(Node *head) { while (head != nullptr) { Node *temp = head; head = head->next; delete temp; } } int main() { int num1 = 12345; int num2 = 6789; Node *list1 = createList(num1); Node *list2 = createList(num2); cout << "Number 1: "; printList(list1); cout << "Number 2: "; printList(list2); Node *sumList = addTwoNumbers(list1, list2); cout << "Sum: "; printList(sumList); deleteList(list1); deleteList(list2); deleteList(sumList); return 0; }
思路
- 首先,定义一个双向链表的节点结构体,包含一个整数值和两个指针,分别指向前一个节点和后一个节点。
- 创建两个双向链表,分别表示要相加的两个数字。可以通过遍历输入的整数,从个位开始,逐个创建节点并将其插入链表中。
- 定义一个变量来表示进位,初始值为0。
- 从链表的最后一个节点开始,逐位相加,并将结果存储在新的链表中。具体步骤如下: - 从两个链表的最后一个节点开始,分别取出对应的值,并加上进位。 - 将相加的结果对10取模得到当前位的值,同时更新进位为相加结果除以10的商。 - 创建一个新的节点,将当前位的值存储在该节点中,并将该节点插入到结果链表的头部。 - 分别将两个链表的指针指向上一个节点,继续下一位的相加操作。 - 如果其中一个链表已经遍历完,但另一个链表还有剩余位数,则将剩余位数与进位相加,并将结果插入到结果链表中
- 最后,得到的结果链表即为相加后的结果。
信息
- ID
- 56
- 时间
- 1000ms
- 内存
- 1024MiB
- 难度
- 1
- 标签
- 递交数
- 8392
- 已通过
- 3776
- 上传者