137 条题解
-
-1
何不试一试这种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直接相加的代码吗,这种也许就是标准代码。
好了,这道题的题解到此为止,走过路过,不要错过,点点赞吧~~~么么哒😗🥰
-
-1
Python 压行技巧,详细讲一讲:
split
函数,可以将字符串按一定分隔符分割,放在一个list
中。例如,s
是'abc.abcd'
,那么s.split ('.')
就是['abc', 'abcd']
,如果没有参数,默认为' '
;map
函数,可以将一个序列依次进行某个操作的最终序列。例如,a
是[1, 1, 4, 5, 1, 4]
,func
函数定义如下:
那么def func (int x): return x + 1
map (func, a)
就是[2, 2, 5, 6, 2, 5]
。sum
函数,可以求一个序列的和。例如,按照上面的a
,那么sum (a)
就是16
。
最终代码(注释参考样例一,不含注释一行):
print(sum(map(int,input().split()))) #print(sum(map(int, '1 2' .split()))) #print(sum(map(int, ['1', '2'] ))) #print(sum( [1, 2] ))) #print( 2 )
-
-2
C++ 式普通写法
#include<iostream> using namespace std; int a,b; int main(){ cin >> a >> b; cout << a + b; return 0; }
高精度
#include<iostream> using namespace std; const int MAXN = 1e5 + 10; long long r; int c; char op; struct bigint{ long long len,sz[MAXN] = {0}; long long &operator [] (long long a){ return sz[a]; } void input(){ string s; cin >> s; len = s.size(); for (int i = 0; i < len; i++){ sz[i] = s[len - i - 1] - '0'; } } void output(){ for (; len - 1 > 0 && !sz[len - 1]; len--){ } for (int i = len - 1; i >= 0; i--){ cout << sz[i]; } cout << '\n'; } bigint operator + (const bigint &b){ bigint c; c.len = max(len,b.len) + 1; for (int i = 0; i < c.len; i++){ c[i] = sz[i] + b.sz[i]; } for (int i = 0; i < c.len - 1; i++){ c[i + 1] += c[i] / 10,c[i] %= 10; } return c; } }a,b,ans; int main(){ a.input(),b.input(); ans = a + b; ans.output(); return 0; }
-
-2
**#include** **<**bits**/**stdc**++.**h**>** **using** **namespace** **std**; **/\*** ** **思路:将字符串s所有拆分的可能性都尝试一下 ** **打擂台求出最小的素数 ** **\*/ //素数判断 **bool** **sushu**(**int** **n**)** **{ ** **for** **(**int** **i** **=** **2**;** **i** **<=** **sqrt**(**n**);** **i**++)** **{ ** **if** **(**n** **%** **i** **==** **0**)** **{ ** **return** **false**;** ** **} ** **} ** **if** **(**n** **<=** **1**)** **return** **false**;** ** **return** **true**;** } **int** **main**()** **{ ** **string** **s**,** **s1**,** **s2**;** ** **int** **i**,** **mi** **=** **INT\_MAX**;** **//mi存储最小的素数 ** **cin** **>>** **s**;** ** **int** **x**,** **y**; ** **/\*长度为s.size()的字符串,拆s.size()-1次 ** **12345 ** **i=0** **1** **2345** **s.substr(0,1)** **s.substr(1) ** **i=1** **12** **345** **s.substr(0,2)** **s.substr(2) ** **\*/ ** **//循环拆段的次数 ** **for** **(**i** **=** **0**;** **i** **<** **s**.**size**()** **-** **1**;** **i**++)** **{** ** **s1** **=** **s**.**substr**(**0**,** **i** **+** **1**); ** **s2** **=** **s**.**substr**(**i** **+** **1**);** ** **//cout<<s1<<"** **"<<s2<<endl; ** **x** **=** **atoi**(**s1**.**c\_str**());** ** **y** **=** **atoi**(**s2**.**c\_str**());** ** **if** **(**sushu**(**x** **+** **y**)** **&&** **x** **+** **y** **<** **mi**)** **mi** **=** **x** **+** **y**; ** **} ** **if** **(**mi** **==** **INT\_MAX**)** **cout** **<<** **-**1**; ** **else** **cout** **<<** **mi**;** ** **return** **0**;** }
信息
- ID
- 56
- 时间
- 1000ms
- 内存
- 1024MiB
- 难度
- 1
- 标签
- 递交数
- 8392
- 已通过
- 3776
- 上传者