137 条题解
-
-2
很简单,线段树模板题。
#include <bits/stdc++.h> #define int long long using namespace std; int x, y, a[100005]; struct node { int l, r, w, tag; } tr[400005]; inline bool inrange(int l1, int r1, int l2, int r2) { return (l1 >= l2) && (r1 <= r2); } inline bool outofrange(int l1, int r1, int l2, int r2) { return (l1 > r2) || (l2 > r1); } inline void pushup(int u) { tr[u].w = tr[u * 2].w + tr[u * 2 + 1].w; } inline void build(int u, int l, int r) { if (l == r) { tr[u].w = a[l]; return; } int mid = l + r >> 1; build(u * 2, l, mid); build(u * 2 + 1, mid + 1, r); pushup(u); } inline int query1(int u, int l, int r, int p) { if (l == r) { return tr[u].w; } else { int mid = l + r >> 1; if (p <= mid) return query1(u * 2, l, mid, p); else return query1(u * 2 + 1, mid + 1, r, p); } } inline void update1(int u, int l, int r, int p, int x) { if (l == r) { tr[u].w += x; } else { int mid = l + r >> 1; if (mid >= p) update1(u * 2, l, mid, p, x); else update1(u * 2 + 1, mid + 1, r, p, x); } } inline void maketag(int u, int len, int x) { tr[u].tag += x; tr[u].w += len * x; } inline void pushdown(int u, int l, int r) { int mid = l + r >> 1; maketag(u * 2, mid - l + 1, tr[u].tag); maketag(u * 2 + 1, r - mid, tr[u].tag); tr[u].tag = 0; } inline int query(int u, int l1, int r1, int l2, int r2) { if (inrange(l1, r1, l2, r2)) { return tr[u].w; } else if (!outofrange(l1, r1, l2, r2)) { int mid = l1 + r1 >> 1; pushdown(u, l1, r1); return query(u * 2, l1, mid, l2, r2) + query(u * 2 + 1, mid + 1, r1, l2, r2); } } inline void update(int u, int l1, int r1, int l2, int r2, int x) { if (inrange(l1, r1, l2, r2)) { maketag(u, r1 - l1 + 1, x); } else if (!outofrange(l1, r1, l2, r2)) { int mid = l1 + r1 >> 1; pushdown(u, l1, r1); update(u * 2, l1, mid, l2, r2, x); update(u * 2 + 1, mid + 1, r1, l2, r2, x); pushup(u); } } signed main() { cin >> x >> y; build(1, 1, 1); update1(1, 1, 1, 1, x); update1(1, 1, 1, 1, y); cout << query1(1, 1, 1, 1); return 0; }
-
-2
很简单,先用字符串存放数据,在从低位开始算,两两相加,逢十进一。
~代码才40行,不多~
#include<bits/stdc++.h> using namespace std; int a[1000001],b[1000001],c[1000001],j; bool x=false; char s[1000001],ss[1000001]; int main() { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); scanf("%s%s",s,ss); a[0]=strlen(s); b[0]=strlen(ss); for(int i=1; i<=a[0]; i++) a[i]=s[a[0]-i]-'0'; for(int i=1; i<=b[0]; i++) b[i]=ss[b[0]-i]-'0'; for(j=1; j<=max(a[0],b[0])+1; j++) { c[j]=a[j]+b[j]; if(c[j]>=10) { c[j]%=10; a[j+1]++; } } c[0]=j; if(c[j+1]>0) c[0]++; for(int i=c[0]; i>=1; i--) { if(x==false&&c[i]==0) continue; x=true; cout<<c[i]; } if(x==false) cout<<0; printf("\n"); return 0; }
管理员大大求过QAQ
彩蛋
文言版
#及充窦融之女孙侍中垒兴矣。 用命名为帝喾咨d 甲戌,武军二千人[71]以五十吏二千人,吏二千人五十余年; <; 甲戌,封府库二千二千余家,帝喾二千余家; < { 更老女喜刑名。帝喾寿梦有子; 更典帝喾嘉伯喈①,吾寿梦有子②; 更典帝喾嘉伯喈。 帝喾八月壬戌岂可怒哉!帝喾聿怀金玉,为帝喾次妃。 [47]陈民典诰。 [47]民义并行。 猫一窦秋开二水中; [88]唐赛明之后[82]--][57] 猫一窦秋开二水中; [88]唐广明年金涂海伯萧][57]- 刘保好心,好梦回漠北;无如之何。 { [88]刘宇深,[88]; 吾乃龌龊。 { [88]徐卢=; [88]唐武爱吾; } } [88] [88]聚野草花,疏刑部伍胥靡]; 了窦融一窦通漾场; { 若孝明旦唱为帝喾做成何人, 引兵久之。 蜥蜴_然; 功名吟泽,无片云; } 若(旦旦唱) co mán)42 30。 乐则灵惨凄部(lya n. 还。 }
-
-3
这里估计大部分都是写了c++有一定基础的,那么我就用链表实现a+b问题吧
#include <iostream> struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(nullptr) {} }; ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode *dummy = new ListNode(0); ListNode *current = dummy; int carry = 0; while (l1 || l2 || carry) { int sum = carry; if (l1) { sum += l1->val; l1 = l1->next; } if (l2) { sum += l2->val; l2 = l2->next; } carry = sum / 10; sum = sum % 10; current->next = new ListNode(sum); current = current->next; } return dummy->next; } ListNode *createLinkedList(int arr[], int n) { if (n == 0) { return nullptr; } ListNode *head = new ListNode(arr[0]); ListNode *current = head; for (int i = 1; i < n; i++) { current->next = new ListNode(arr[i]); current = current->next; } return head; } void printLinkedList(ListNode *head) { ListNode *current = head; while (current) { std::cout << current->val << " "; current = current->next; } std::cout << std::endl; } int main() { int arr1[1]; int arr2[1]; std::cin >> arr1[0]; std::cin >> arr2[0]; int n1 = sizeof(arr1) / sizeof(arr1[0]); int n2 = sizeof(arr2) / sizeof(arr2[0]); ListNode *l1 = createLinkedList(arr1, n1); ListNode *l2 = createLinkedList(arr2, n2); ListNode *sum = addTwoNumbers(l1, l2); printLinkedList(sum); return 0; }
信息
- ID
- 56
- 时间
- 1000ms
- 内存
- 1024MiB
- 难度
- 1
- 标签
- 递交数
- 8392
- 已通过
- 3776
- 上传者