PAT 天梯赛 L1-025. 正整数A+B 【字符串处理】
2024-10-06 16:38:49
题目链接
https://www.patest.cn/contests/gplt/L1-025
思路
注意 输入字符串B的时候 要用getline 因为 可能存在空格
然后就把字符串 转化成 数字 并且判断一下 范围和 是否含有非法字符 就可以了
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;
typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;
int f(string s)
{
int len = s.size();
int i;
int num = 0;
if (s[0] == '0')
return -1;
for (i = 0; i < len; i++)
{
if (s[i] <= '9' && s[i] >= '0')
num = num * 10 + (s[i] - '0');
else
{
return -1;
}
}
return num;
}
int main()
{
string a, b;
cin >> a;
getchar();
getline(cin, b);
int n = f(a);
int m = f(b);
if (n >= 1 && n <= 1000 && m >= 1 && m <= 1000)
printf("%d + %d = %d\n", n, m, n + m);
else if(n <= 1000 && n >= 1)
printf("%d + ? = ?\n", n);
else if (m <= 1000 && m >= 1)
printf("? + %d = ?\n", m);
else
printf("? + ? = ?\n");
}
最新文章
- Asp.Net Core 发布和部署(Linux + Jexus )
- CSS中伪类及伪元素用法详解
- JSONKit does not support Objective-C Automatic Reference Counting(ARC) / ARC forbids Objective-C objects in struct
- 【BZOJ-4326】运输计划 树链剖分 + 树上差分 + 二分
- xcode代码提示功能失效的解决方法
- SQL查询语句执行的顺序是-----------------
- 从源代码分析Universal-Image-Loader中的线程池
- awk 例子
- pager分页框架体会
- ACM 数论小结 2014-08-27 20:36 43人阅读 评论(0) 收藏
- 【POJ】【1067】取石子游戏
- Filter登录验证过滤器(全局)
- thinkphp3.2.3 成功对接支付宝接口
- 5 HBase 常用Shell命令
- poj1804(归并排序求逆序数)
- Python中三种基本结构的语句
- TDE: Transparent Data Encryption brief introduction
- Android onclick监听事件打开新界面
- iOS 隐藏导航条分割线
- Block高级用法:Block传值UI_12(3)