zoukankan      html  css  js  c++  java
  • A1136 | 字符串处理、大整数运算

    题目链接:

    https://www.patest.cn/contests/pat-a-practise/1136


    今天是12月17号。最近这几天都有点不在状态。已经整整一周没有练算法了,自从12.3考了ccf,12.9考了pat,打击很大。

    前几天想刷考试时的第一题,没想到重新写一遍也没过所有case。可以说第一题就是我pat失败的开始,因为第一题我没记住c++翻转字符串的函数,现场实现了一个,加上刚开始考试还没有进入状态,写了一个小时。本来时间不多的3个小时,就有三分之一花在了这道简单题上。

    今天终于找到过不了case的原因了,就是因为测试数据可能会直接给出回文数,然后引起的错误。

    #include<bits/stdc++.h>
    
    #define I scanf
    #define OL puts
    #define O printf
    #define F(a,b,c) for(a=b;a<c;a++)
    #define FF(a,b) for(a=0;a<b;a++)
    #define FG(a,b) for(a=b-1;a>=0;a--)
    #define LEN 501
    #define MAX 0x06FFFFFF
    
    using namespace std;
    
    bool check(string str);
    string add(string a,string b);
    
    int main() {
        char buffer[10000];
        gets(buffer);
        string a(buffer);
        string ans(a);
        string b;
        int i=10;
        bool isSolve=false;
        while(i-->0){
            if(check(ans)){
                O("%s is a palindromic number.
    ",ans.c_str());
                isSolve=true;
                break;
            }
            b=string(a);
            reverse(b.begin(),b.end());
            ans=add(a,b);
            O("%s + %s = %s
    ",a.c_str(),b.c_str(),ans.c_str());
            a=ans;
        }
        if(!isSolve)
            O("Not found in 10 iterations.");
        return 0;
    }
    
    bool check(string str){
        int len=str.length();
        for(int i=0;i<len-1-i;i++){
            if(str[i]!=str[len-1-i])
                return false;
        }
        return true;
    }
    
    string add(string a,string b){
        int i,n=a.length();
        string ans(n,0);
        int t=0;
        FF(i,n){
            int c=a[n-1-i]+b[n-1-i]-96+t;
            ans[i]=c%10+48;
            if(c>9) t=1;
            else t=0;
        }
        if(t) ans+='1';
        reverse(ans.begin(),ans.end());
        return ans;
    }

    需要拓展的点:大整数的四则运算、取模、等

    需要注意的点:思考在题目条件下会给出怎样的边界条件

  • 相关阅读:
    flask
    redis实战之事物和持久化
    vue 工程从window 到mac
    mysql5.7 group by
    Error creating bean with name 'org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration
    当我回过头
    springmvc 接收json类型的数据封装到map中
    linux 下 home 目录磁盘爆满,rm 后仍然不行
    springboot启动时的一个bug
    vue 使用webpack 打包 出现UnhandledPromiseRejectionWarning: Error: "dependency" is not a valid chunk sort mode at HtmlWebpackPlugin.sortEntryChunks
  • 原文地址:https://www.cnblogs.com/TQCAI/p/8051917.html
Copyright © 2011-2022 走看看