177 条题解
-
-3
终于可以写一份A+B这么难的题的题解了。 咦?竟然没有人写LCT的题解? Link-Cut Tree 会很伤心的! ORZ为了不让LCT伤心于是我来一份LCT的A+B题解吧! 送上代码: #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); //连边 Link connect(A,B); //断边 Cut cut(A,B); //再连边orz Link again connect(A,B); printf("%d\n",query(A,B)); return 0; }
本蒟蒻第一次写题解,请原谅
-
-3
A+B问题居然没有高精度题解,发一篇吧!
#include <bits/stdc++.h> using namespace std; const int maxn=250; char sa[maxn],sb[maxn];//定义两个字符串 int a[maxn],b[maxn],c[maxn];//a,b位数字,c进位 int main() { scanf ("%d",sa); scanf ("%d",sb); memset (c,0,sizeof(c));//初始化赋值为0 int la=strlen (sa);//取字符串长度 int lb=strlen (sb); for (int i=0;i<la;i++) a[la-i-1]=sa[i]-'0'; for (int i=0;i<lb;i++) b[lb-i-1]=sb[i]-'0';//将两个字符串类型的数转换成整数 int lc=la>lb?la:lb;//三目运算符,取两个数字中数位最长的数字的数位 for (int i=0;i<lc;i++) { c[i] += a[i]+b[i];//加法运算 if (c[i]>=10) { c[i+1]=a[i]/10; c[i]=c[i]%10;//进位 } } if (c[lc]>0) lc++; int cnt=lc-1; while (c[cnt]==0) cnt--; for (int i=cnt;i>=0;i--) printf ("%d",c[i]); return 0; }
-
-3
#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; }
-
-3
何不试一试这种C++代码呢?
#include <iostream> #include <string> // 定义 A+B 的函数 std::string add(std::string num1, std::string num2) { // 将两个数字字符串转换为数字数组 int len1 = num1.length(); int len2 = num2.length(); int maxLen = std::max(len1, len2); int* arr1 = new int[maxLen]; int* arr2 = new int[maxLen]; for (int i = 0; i < maxLen; i++) { if (i < len1) { arr1[i] = num1[len1 - i - 1] - '0'; } else { arr1[i] = 0; } if (i < len2) { arr2[i] = num2[len2 - i - 1] - '0'; } else { arr2[i] = 0; } } // 进行加法运算 int carry = 0; std::string result = ""; for (int i = 0; i < maxLen; i++) { int sum = arr1[i] + arr2[i] + carry; carry = sum / 10; result = std::to_string(sum % 10) + result; } if (carry > 0) { result = std::to_string(carry) + result; } delete[] arr1; delete[] arr2; return result; } int main() { // 读取输入的两个数字 std::string num1, num2; std::cout << "请输入两个数字:" << std::endl; std::cin >> num1 >> num2; // 计算并输出结果 std::string sum = add(num1, num2); std::cout << "结果为:" << sum << std::endl; return 0; }
但愿这种代码不会出现TLE或者是MLE这个代码使用了字符串来表示大数,通过将字符串转换为数字数组,然后进行逐位相加的方式来实现 A+B 的功能。由于使用了字符串和动态数组,代码量较大。但是在实际应用中,这种方式并不是最高效的解决方案。在实际情况下,我们可以使用更简洁和高效的算法来解决这个问题。
我们可以用更简洁的方法做这道题
#include <bits/stdc++.h> using namespace std; int main(){ int a, b; cin >> a >> b; cout << a + b; return 0;
上面的代码是进行变量a和b直接相加的代码吗,这种也许就是标准代码。
好了,这道题的题解到此为止,走过路过,不要错过,点点赞吧~~~么么哒😗🥰
-
-3
很简单,线段树模板题。
#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; }
-
-3
很简单,先用字符串存放数据,在从低位开始算,两两相加,逢十进一。
~代码才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
甚至连变量都不用的快读。
代码如下:
#include<bits/stdc++.h> using namespace std; inline int read() { int x=0; bool flag=1; char c=getchar(); while(c<'0'||c>'9') { if(c=='-') flag=0; c=getchar(); } while(c>='0'&&c<='9') { x=(x<<1)+(x<<3)+c-'0'; c=getchar(); } return (flag?x:~(x-1)); } int main() { cout<<read()+read(); return 0; }
信息
- ID
- 56
- 时间
- 1000ms
- 内存
- 1024MiB
- 难度
- 1
- 标签
- 递交数
- 10369
- 已通过
- 4685
- 上传者