A+B problem题解
2024-08-22 17:58:43
发布于:安徽
33阅读
0回复
0点赞
ACGO第一道题就这么难了吗???
未成年码将请在成年码王的陪同下观看,否则会引起bushi
超强缺省源
本人只是学了一天C++的蒟蒻
#include<bits/stdc++.h>
//#define ONLINE_TEST
#ifdef ONLINE_TEST
#define getchar() p1 == p2 && (p2 = (p1 = buf) + fread (buf, 1, 1 << 18, stdin), p1 == p2) ? EOF : *p1++
#define flush() fwrite (buffer, 1, p11 + 1, stdout), p11 = -1
#define putchar(c) p11 == p22 ? flush(), buffer[++p11] = c : buffer[++p11] = c
char buf[1 << 18], *p1 = buf, *p2 = buf;
char buffer[1 << 18];
int p11 = -1;
const int p22 = (1 << 18) - 1;
#endif
template <typename T> inline void rei (T &x) {
x = 0; bool f = 1; int ch;
while (!isdigit (ch = getchar ())) f = ch != '-';
if (f) do x = x * 10 + (ch & 15); while (isdigit (ch = getchar ()));
else do x = x * 10 - (ch & 15); while (isdigit (ch = getchar ()));
}
template <typename T> inline void wri (T x) {
if (x < 0) putchar ('-'), x = ~x + 1;
static int stk[40], top = 0;
while (x) stk[++top] = x % 10, x /= 10;
if (top == 0) stk[++top] = 0;
while (top) putchar (stk[top--] ^ 48);
putchar (' ');
}
template <typename T, typename... Args> inline void rei (T &x, Args&... args) {rei (x), rei (args...);}
template <typename T, typename... Args> inline void wri (T x, Args... args) {wri (x), wri (args...);}
template <typename T1, typename T2> inline T1 &cmin (T1 &a, const T2 &b) {return a > b ? a = b : a;}
template <typename T1, typename T2> inline T1 &cmax (T1 &a, const T2 &b) {return a < b ? a = b : a;}
typedef long long LL;
typedef unsigned long long ULL;
#define fir(i,a,b,...) for (register int i = (a), ##__VA_ARGS__; i <= (b); i++)
#define firr(i,a,b,...) for (register int i = (a), ##__VA_ARGS__; i >= (b); i--)
#define cls(a) memset (a, 0, sizeof a)
#define mset(a,b) memset (a, b, sizeof a)
#define dbug(x) (void)(cerr << #x " = " << x << endl)
using namespace std;
signed main () {
int a,b;
cin>>a>>b;
cout<<a+b;
#ifdef ONLINE_TEST
flush ();
#endif
return 0;
}
或者
#include<bits/stdc++.h>
//#define ONLINE_TEST
#ifdef ONLINE_TEST
#define getchar() p1 == p2 && (p2 = (p1 = buf) + fread (buf, 1, 1 << 18, stdin), p1 == p2) ? EOF : *p1++
#define flush() fwrite (buffer, 1, p11 + 1, stdout), p11 = -1
#define putchar(c) p11 == p22 ? flush(), buffer[++p11] = c : buffer[++p11] = c
char buf[1 << 18], *p1 = buf, *p2 = buf;
char buffer[1 << 18];
int p11 = -1;
const int p22 = (1 << 18) - 1;
#endif
template <typename T> inline void rei (T &x) {
x = 0; bool f = 1; int ch;
while (!isdigit (ch = getchar ())) f = ch != '-';
if (f) do x = x * 10 + (ch & 15); while (isdigit (ch = getchar ()));
else do x = x * 10 - (ch & 15); while (isdigit (ch = getchar ()));
}
template <typename T> inline void wri (T x) {
if (x < 0) putchar ('-'), x = ~x + 1;
static int stk[40], top = 0;
while (x) stk[++top] = x % 10, x /= 10;
if (top == 0) stk[++top] = 0;
while (top) putchar (stk[top--] ^ 48);
putchar (' ');
}
template <typename T, typename... Args> inline void rei (T &x, Args&... args) {rei (x), rei (args...);}
template <typename T, typename... Args> inline void wri (T x, Args... args) {wri (x), wri (args...);}
template <typename T1, typename T2> inline T1 &cmin (T1 &a, const T2 &b) {return a > b ? a = b : a;}
template <typename T1, typename T2> inline T1 &cmax (T1 &a, const T2 &b) {return a < b ? a = b : a;}
typedef long long LL;
typedef unsigned long long ULL;
#define fir(i,a,b,...) for (register int i = (a), ##__VA_ARGS__; i <= (b); i++)
#define firr(i,a,b,...) for (register int i = (a), ##__VA_ARGS__; i >= (b); i--)
#define cls(a) memset (a, 0, sizeof a)
#define mset(a,b) memset (a, b, sizeof a)
#define dbug(x) (void)(cerr << #x " = " << x << endl)
using namespace std;
signed main () {
char a[101],b[101];
int a1[101],b1[101];
int c[101];
//1.当字符串输入
cin >> a >> b;
//2.转换并反转整数数组
int la=strlen(a),lb=strlen(b);
for(int i=0;i<la;i++){
a1[la-1-i]=a[i]-48;
}
for(int i=0;i<lb;i++){
b1[lb-1-i]=b[i]-48;
}
//3.对应位相加
int lc=max(la,lb);
int x=0;
for(int i=0;i<lc;i++){
int sum=a1[i]+b1[i]+x;
c[i]=sum%10;
x=sum/10;
}
//4.处理进位
if(x!=0){
c[lc]=x;
lc++;
}
//5.逆序输出
for(int i=lc-1;i>=0;i--){
cout << c[i];
}
#ifdef ONLINE_TEST
flush ();
#endif
return 0;
}
全部评论 1
请注意,发帖者已学习C++2年了
2024-08-31 来自 广东
0
有帮助,赞一个