166 条题解
-
-2
A+B Problem 题解
#include<bits/stdc++.h> using namespace std; int main(){long long a,b;cin>>a>>b;cout<<a+b;return 0;}
-
-2
本蒟蒻啥都不会,只能贡献一份小小的码风优良的代码```language
#include <iostream> using namespace std; const int MAX = 1000; void change(const string &a1, int a[], int len) { for (int i = 0; i < len; i++) { a[i] = a1[len - i - 1] - '0'; } } int main() { string a1, b1; cin >> a1 >> b1; int lena = a1.size(), lenb = b1.size(); int a[MAX] = {}, b[MAX] = {}, c[MAX] = {}, cf = 0; change(a1, a, lena); change(b1, b, lenb); int lenc = lena; if (lenb > lena) { lenc = lenb; } for (int i = 0; i <= lenc; i++) { c[i] = a[i] + b[i] + cf; if (c[i] >= 10) { cf = 1; c[i] -= 10; } else { cf = 0; } } int i = lenc; if (c[i] == 0) { i--; } for (i; i >= 0; i--) { cout << c[i]; } return 0; }
-
-2
#include<bits/stdc++.h> //头文件 using namespace std;//命名空间 int main(){//主函数,程序从这里开始 //signed main也行 long long a,b; //用long long类型的变量进行存储因为数值范围是10^6,int存不下 cin>>a>>b; /* 这里可以替换为: scanf("%lld%lld",&a,&b); */ long long ans=a+b; cout<<ans<<endl; /* 这里可以替换为: printf("%lld",ans); */ return 0;//一定要写,不然RE }
-
-2
终于可以写一份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; }
本蒟蒻第一次写题解,请原谅
-
-2
何不试一试这种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直接相加的代码吗,这种也许就是标准代码。
好了,这道题的题解到此为止,走过路过,不要错过,点点赞吧~~~么么哒😗🥰
信息
- ID
- 56
- 时间
- 1000ms
- 内存
- 1024MiB
- 难度
- 1
- 标签
- 递交数
- 9734
- 已通过
- 4392
- 上传者