zoukankan      html  css  js  c++  java
  • 1024 Palindromic Number

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

    Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

    Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

    Input Specification:

    Each input file contains one test case. Each case consists of two positive numbers N and K, where N (≤) is the initial numer and K (≤) is the maximum number of steps. The numbers are separated by a space.

    Output Specification:

    For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

    Sample Input 1:

    67 3
    

    Sample Output 1:

    484
    2
    

    Sample Input 2:

    69 3
    

    Sample Output 2:

    1353
    3
    我的代码
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdio>
    using namespace std;
    string sum(string s,string s1)
    {
        int n=s.size(),flag=0;
        string s3("",n);
        //cout<<s<<endl;
        //cout<<s1<<endl;
        //reverse(s.begin(),s.end());
        //reverse(s1.begin(),s1.end());
        for(int i=0;i<n;i++)
        {
            s3[i]=(s[i]-'0')+(s1[i]-'0')+flag+'0';
            flag=0;
            if(s3[i]>'9')
            {
                flag=1;
                s3[i]=s3[i]-10;
            }
        }
        if(flag)
        {
            string s4("",n+1);
            for(int i=0;i<n;i++)
            s4[i]=s3[i];
            s4[n]='1';
            reverse(s4.begin(),s4.end());
            //cout<<s4<<endl;
            return s4;
        }
        else{
            reverse(s3.begin(),s3.end());
            //cout<<s3<<endl;
            return s3;
        }
    }
    bool fun(string s)
    {
        int n=s.size();
        int i=0,j=n-1;
        while(i<=j)
        {
            if(s[i]!=s[j])
            return false;
            i++;
            j--;
        }
        return true;
    }
    int main()
    {
        string s,s1,s2;
        bool flag=true;
        int k=0,n;
        cin>>s>>n;
        while(k<n)
        {
            if(fun(s))
            {
                flag=false;
                cout<<s<<endl;
                cout<<k<<endl;
                break;
            }
            k++;
            s1=s;
            reverse(s.begin(),s.end());
            s2=sum(s,s1);
            if(fun(s2))
            {
                flag=false;
                cout<<s2<<endl;
                cout<<k<<endl;
                break;
            }
            s=s2;
        }
        if(flag)
        {
        cout<<s2<<endl;
        cout<<k<<endl;    
        }
        return 0;
     } 

    柳神代码

    #include <iostream>
    #include <algorithm>
    using namespace std;
    string s;
    void add(string t) {
        int len = s.length(), carry = 0;
        for(int i = 0; i < len; i++) {
            s[i] = s[i] + t[i] + carry - '0';
            carry = 0;
            if(s[i] > '9') {
                s[i] = s[i] - 10;
                carry = 1;
            }
        }
        if(carry) s += '1';
        reverse(s.begin(), s.end());
    }
    int main() {
        int cnt, i;
        cin >> s >> cnt;
        for(i = 0; i <= cnt; i++) {
            string t = s;
            reverse(t.begin(), t.end());
            if(s == t || i == cnt) break;
            add(t);
        }
        cout << s << endl << i;
        return 0;
    }

    ps:之前使用string一直有错误,现在知道应该把string当成对象而不是简单的把string当成数组,二者完全不一样

    还有做这题时一直在像67+76=143,23+32=55这两种情况应该怎么做,然后找资料string怎么初始化,怎么定义初始空间,

    然后看了柳神代码发现别人就一句

     if(carry) s += '1';
    给跪了
    如果你够坚强够勇敢,你就能驾驭他们
  • 相关阅读:
    一个非侵入的Go事务管理库——如何使用
    事件驱动的微服务-事件驱动设计
    事件驱动的微服务-总体设计
    如何快速高效率地学习Go语言
    清晰架构(Clean Architecture)的Go微服务
    清晰架构(Clean Architecture)的Go微服务: 编码风格
    清晰架构(Clean Architecture)的Go微服务: 事物管理
    清晰架构(Clean Architecture)的Go微服务: 依赖注入(Dependency Injection)
    msf stagers开发不完全指北(四): msf 中使用域前置技术隐藏流量
    elementui 表格中带有按钮的loading解决方案
  • 原文地址:https://www.cnblogs.com/liuzhaojun/p/11211753.html
Copyright © 2011-2022 走看看