zzulioj - 2599: 对称的数字
2024-08-28 04:56:47
题目链接:
http://acm.zzuli.edu.cn/problem.php?id=2599
这个题表面上看起来很简单....但是如果直接写的话就算用long long也会有越界的情况(double即使不越界,也可能有精度损失)所以我们需要用到大整数加法
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mst(a) memset(a, 0, sizeof(a))
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double eps = 1e-;
const int INF = 0x3f3f3f3f;
const ll ll_INF = ;
const int maxn = 1e3+;
char num1[maxn], num2[maxn], res[maxn];
void sum() { //大整数加法
mst(res);
int len1 = strlen(num1);
int len2 = strlen(num2);
reverse(num1, num1+len1); //反转第一个数
reverse(num2, num2+len2); //反转第二个数
int kase = ;
while(kase < len1 && kase < len2) { //重叠部分相加
res[kase+] = (res[kase] + num1[kase] + num2[kase] - ''*)/; //模拟进位相加
res[kase] = (res[kase] + (num1[kase] + num2[kase] - ''*))% + '';
++kase;
}
while(kase < len1) { //非重叠部分相加
res[kase+] = (res[kase] + num1[kase] - '')/;
res[kase] = (res[kase] + num1[kase] - '')% + '';
++kase;
}
while(kase < len2) { //同上
res[kase+] = (res[kase] + num2[kase] - '')/;
res[kase] = (res[kase] + num2[kase] - '')% + '';
++kase;
}
if (res[kase]) { //判断最后一次相加有没有进位
res[kase] += '';
res[++kase] = '\0';
}
else
res[kase] = '\0';
}
bool okk() { //判断回文
int len = strlen(res);
for (int i = ; i<len/; ++i)
if (res[i] != res[len-i-])
return false;
return true;
}
int main(void) {
while(~scanf("%[^\n]%*c", num1)) {
bool ok = false;
strcpy(num2, num1);
for (int i = ; i<; ++i) {
reverse(num2, num2+strlen(num2));
sum(); //将反转过后的数与原数字相加
if (okk()) { //判断是否满足回文
ok = true;
break;
}
strcpy(num1, res); //将相加后的结果复制给另外两个数字准备下次计算
strcpy(num2, res);
}
printf(ok ? "%s\n" : "NO\n", res);
}
return ;
}
最新文章
- WebComponent魔法堂:深究Custom Element 之 面向痛点编程
- Android Studio使用Git版本控制工具
- How To Restart timer service on all servers in farm
- SpringMVC和MyBatis整合
- Velocity模板引擎语法
- Java 概述
- Centos Python2 升级到Python3
- 解决中64位Win7系统上PLSQL无法连接ORACLE的方法(PLSQL无法识别ORACLE_HOME的配置)
- sort,ksort,asort的区别
- python 遍历文件夹
- hdu 1075 What Are You Talking About
- EClipse开发NDK流程
- UISearchBar 点击X 按钮收键盘
- Hibernate Annotation _List/Map
- asp.net core 系列 17 通用主机 IHostBuilder
- ife2018 零基础学院 day 3
- Oracle_plsql_开发工具搭建最小化客户端
- python第二天 列表、元组
- ASP.NET Core 启动流程图
- spring-boot+quartz的CRUD动态任务管理系统
热门文章
- Ubuntu无法进入图形化界面(报错/dev/sda1:clean的解决)
- Rabbit MQ 学习参考
- protobuf 中import 的使用
- eclipse中修改项目名
- SpringBoot+Mybatis+Druid批量更新 multi-statement not allow异常
- Linq与委托
- 03 .NET CORE 2.2 使用OCELOT -- Docker中的Consul
- Markdown温故知新(3):六个实用扩展语法
- C#实现UrlEncode (URL编码)
- ElasticSearch 查询索引