首页 > 其他分享 >Reverse and Add UVA - 10018

Reverse and Add UVA - 10018

时间:2024-11-14 19:45:39浏览次数:1  
标签:palindrome int number will Add vector carry UVA 10018

// Reverse and Add UVA - 10018.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*
https://vjudge.net/problem/UVA-10018#author=md_bayazid

The “reverse and add” method is simple: choose a number, reverse its digits and add it to the original. If the sum is not a palindrome
(which means, it is not the same number from left to right and right to left), repeat this procedure.

     195   Initial number
     591
     ---
     786
     687
     ---
     1473  (For example)
     3741
     ----
     5214
     4125
     ----
     9339    (Resulting Palindrome)

In this particular case the palindrome ‘9339’ appeared after the 4th addition. 
This method leads to palindromes in a few steps for almost all of the integers. 
But there are interesting exceptions. 196 is the first number for which no palindrome has been found. 
It is not proven though, that there is no such a palindrome.

You must write a program that give the resulting palindrome and the number of iterations (addi tions) to compute the palindrome.
You might assume that all tests data on this problem:
   • will have an answer ,
   • will be computable with less than 1000 iterations (additions),
   • will yield a palindrome that is not greater than 4,294,967,295.
Input
The first line will have a number N (0 < N ≤ 100) with the number of test cases, the next N lines will have a number P to compute its palindrome.
Output
For each of the N tests, you will have to write a line with the following data: minimum number of iterations and the resulting palindrome 
itself separated by one space.
Sample Input
3
195
265
750
Sample Output
4 9339
5 45254
3 6666
*/
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int T;
vector<int> a, b;


void fill(long long u, vector<int>& a) {
    if (u == 0) { a.push_back(0); return; }
    a.clear();
    while (u) {
        a.push_back(u % 10);
        u /= 10;
    }
}

bool check(vector<int> a) {
    int l = 0; int r = a.size() - 1;
    while (l < r) {
        if (a[l] != a[r]) return false;
        l++; r--;
    }

    return true;
}

vector<int> add(const vector<int>& a, const vector<int>& b) {
    int len = min(a.size(), b.size());
    int carry = 0;
    vector<int> ret;
    for (int i = 0; i < len; i++) {
        ret.push_back((a[i] + b[i] + carry) % 10);
        carry = (a[i] + b[i] + carry) / 10;
    }
    if (carry) ret.push_back(carry);

    return ret;
}




void solve() {
    int ans = 0;
    while (false==check(a)) {
        a = add(a, b);
        b= a; reverse(b.begin(), b.end());
        ans++;
    }
    cout << ans << " ";
    for (int i = a.size() - 1; i >= 0; i--) {
        cout << a[i];
    }
    cout << endl;
    return;
}

int main()
{
    cin >> T;
    while (T--) {
        long long u;
        cin >> u;
        fill(u, a);
        b = a; reverse(b.begin(), b.end());
        solve();
    }

    return 0;
}

标签:palindrome,int,number,will,Add,vector,carry,UVA,10018
From: https://www.cnblogs.com/itdef/p/18546669

相关文章

  • Primary Arithmetic UVA - 10035
    //PrimaryArithmeticUVA-10035.cpp:此文件包含"main"函数。程序执行将在此处开始并结束。///*https://vjudge.net/problem/UVA-10035Childrenaretaughttoaddmulti-digitnumbersfromright-to-leftonedigitatatime.Manyfindthe"carry"operation......
  • PaddleOCR在华为云上实现文本检测识别任务,并部署到华为昇腾NPU的详细步骤
            PaddleOCR是飞桨推出的一套丰富的OCR工具库开源项目,支持文字检测、文字方向检测、多语种文本识别、手写体文本识别等多种OCR相关前沿算法,并提供了丰富的轻量级预训练模型和模型优化技术,可以快速部署和使用OCR功能。https://github.com/PaddlePaddle/PaddleOCR......
  • 题解:[XIX Open Cup, Grand Prix of Korea] Dev, Please Add This!
    前置知识:2-SAT题意[XIXOpenCup,GrandPrixofKorea]Dev,PleaseAddThis!在一张网格上有以下几种物品:空地(.)墙(#)星星(*)一个球(O)现在你要玩一个游戏。你可以让球朝上下左右某一个方向移动,但是一旦移动就必须走到底,也就是说直到前面是墙或者边界才能停。另外,如果你走......
  • 【转】[Java][Idea] 打开时报错 Internal error. Address already in use: bind
    方法一:netshwinsockreset以管理员身份运行cmd执行这个命令,然后重启电脑。 方法二:按报错提示,访问  https://jb.gg/ide/critical-startup-errors  按文章,可以参考 https://youtrack.jetbrains.com/issue/IDEA-238995解决问题 以下是文章摘抄:ReviseIDEdire......
  • 题解:UVA1362 Exploring Pyramids
    思路:显然的,若不是叶子结点都应该至少遍历两次。于是两个相同访问之间就可能是一颗子树。更加具体的,如同\(s_l,\dots,s_k,\dots,s_r\),使得\(s_l=s_k\),那么就可以认为\(s[l,k]\)是\(s[l,r]\)的一颗子树,设区间\(s[l,r]\)的结构数量为\(f_{l,r}\),那么根据乘法原理,当把\(......
  • MySQL问题解决记录(1):IP address 'xxx.xxx.xxx.xxx' could not be resolved: 这是在主机
    目录问题描述排查流程解决方案总结问题描述[Warning][MY-010055][Server]IPaddress'xxx.xxx.xxx.xxx'couldnotberesolved:这是在主机名解析时通常出现的暂时错误,它意味着本地服务器没有从权威服务器上收到响应。问题表现:A主机的服务在访问B主机的MySQL数据库时,产......
  • Linux中关于useradd、chmod、chown、getfacl、setfact等权限设置
    文章目录一、Linux用户管理1、用户(user)、用户组(group)、其他用户概念(other)1.1理解Linux的`单用户多任务`,`多用户多任务`概念1.2用户(user)和用户组(group)概念;查看主机名和修改主机名需要root权限(然后输入密码)2.1创建用户2.1.1用adduser创建用户3、删除用户查看用户列......
  • ffmpeg Adding new interfaces
    nynewpublicidentifiersininstalledheadersareconsiderednewAPI-thisincludesnewfunctions,structs,macros,enumvalues,typedefs,newfieldsinexistingstructs,newinstalledheaders,etc.ConsiderthefollowingguidelineswhenaddingnewAPIs......
  • LBA(Logical Block Addressing,逻辑块寻址)是一种硬盘寻址方式,用于将硬盘中的每个存储块
    LBA(逻辑块寻址)模式简介LBA(LogicalBlockAddressing,逻辑块寻址)是一种硬盘寻址方式,用于将硬盘中的每个存储块映射为一个唯一的逻辑地址。这种寻址方式使得操作系统能够通过逻辑地址而不是物理位置来访问硬盘数据,从而简化了硬盘的管理和数据访问。LBA的背景与作用在硬盘的传统寻......
  • ArcGIS Pro SDK Addin-DAML
    ArcGISProSDKAddin-DAML文章目录ArcGISProSDKAddin-DAML1Panes:重置窗格2Button:从功能区中移除核心按钮3Button:将新按钮插入功能区上的现有组4Menu:在图层上下文菜单中插入一个新按钮5Menu:在MapContainer上下文菜单中插入新菜单6Menu......