跳到正文

题解

水题杯 2024/8/19 题解

封禁用户约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年8月9日
篇幅
约 3 分钟
获赞
2

版本

  1. v1当前

第一题 A + B 1

C++
#include <iostream>
using namespace std;
#define endl '\n'
int a,b;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>a>>b;
	cout<<a + b;
	return 0;
}

思路:无

第二题 A + B 2

C++
#include <iostream>
using namespace std;
#define endl '\n'
long long a,b;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>a>>b;
	cout<<a + b;
	return 0;
}

思路:观察样例可发现数据范围超过int,所以应使用long long。

第三题 A + B 3

C++
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 86;
char a[N],b[N];
int a1[N],b1[N],c1[N];
int main(){
	int lenc=0,x=0;
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>a>>b;
	int lena = strlen(a),lenb = strlen(b);
	for(int i = 0;i < lena;i++){
		a1[i] = a[lena - 1 - i] - 48;
	}
	for(int i = 0;i < lenb;i++){
		b1[i] = b[lenb - 1 - i] - 48;
	}
	int maxlen = max(lena,lenb);
	while(lenc < maxlen){
		c1[lenc] = a1[lenc] + b1[lenc] + x;
		x = c1[lenc] / 10;
		c1[lenc] = c1[lenc] % 10;
		lenc++;
	}
	if(x){
		c1[lenc] = x;
	}
	else{
		lenc--;
	}
	for(int i = 0;i <= lenc;i++){
		cout<<c1[lenc -i ];
	}
	return 0;
}

思路:观察样例可发现数据范围超过unsigned long long,所以应使用高精度。

第四题 A + B 与 C + D

C++
#include <iostream>
#include <iomanip>
using namespace std;
#define endl '\n'
double a,b;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>a>>b;
	cout<<fixed<<setprecision(3)<<a / 5 + b / 6;
	return 0;
}

思路:观察样例可发现“C + D”实为A / 5 + B / 6。

原文
存档评论1 条抓取于