题解

195 条题解

  • 22
    @ 2017-01-30 19:17:08

    这题只需要注意16进制的不同即可AC
    10进制一下需要-‘0’
    16进制则需要-55(-'A'+10)
    加法进位时不是=10进位,而是=进制(看输入)才进位。若是最后一位进位则要将长度+1
    最后判断是否回文。

    评测结果
    编译成功

    测试数据 #0: Accepted, time = 0 ms, mem = 804 KiB, score = 25
    测试数据 #1: Accepted, time = 0 ms, mem = 800 KiB, score = 25
    测试数据 #2: Accepted, time = 0 ms, mem = 804 KiB, score = 25
    测试数据 #3: Accepted, time = 15 ms, mem = 804 KiB, score = 25
    Accepted, time = 15 ms, mem = 804 KiB, score = 100
    代码(C++)

    #include<iostream>  
    #include<cstring>  
    #include<cstdio>  
    using namespace std;  
    string x;  
    int jz,n,a[10001],s=1;  
    int pdhw(int n)  
    {  
        for(int i=1;i<=n/2;i++)  
            if(a[i]!=a[n-i+1])  
                return 0;  
        return 1;  
    }  
    int jf(int n)  
    {  
        int c[10001]={0},s=1;  
        for(int i=1;i<=n;i++)  
        {  
            c[i]=a[i]+a[n-i+1]+c[i];  
            c[i+1]+=c[i]/jz;  
            c[i]%=jz;  
        }  
        if (c[n+1]!=0)  
            n++;  
        for(int i=n;i>=1;i--)  
        {  
            a[s]=c[i];  
            s++;  
        }  
        return n;  
    }  
    int main()  
    {  
        cin>>jz>>x;  
        n=x.size();  
        for(int i=1;i<=n;i++)  
        {  
            if(x[i-1]<65)  
                a[i]=x[i-1]-'0';  
            else  
                a[i]=x[i-1]-55;  
        }  
        while(s<=30)  
        {  
            if(pdhw(n)==1)  
            {  
                cout<<"STEP="<<s-1;  
                return 0;  
            }  
            s++;  
            n=jf(n);  
        }  
        cout<<"Impossible!";  
    }
    

    祝大家刷机愉快!

    • @ 2017-03-05 08:57:24

      已赞√这个进位写的比我好。学到了学到了

    • @ 2017-04-25 10:50:16

      厉害了

    • @ 2017-04-29 22:10:09

      厉害!!!OTZ神犇!

    • @ 2017-11-19 20:57:54

      这个进制转换非常好,谢谢dalao

    • @ 2018-03-08 23:12:40

      真的学到了,一直在纠结进位问题,赞👍

  • 3
    @ 2018-04-18 22:52:14
    //fuck it
    #include <stdio.h>
    #include <strings.h>
    int len=0;
    int is[101];
    int N;
    void plus(){
        int rev[101];
        for(int k=0;k<len;k++){
            rev[k]=is[len-k-1];
        }
        for(int i=0;i<len;i++){
            is[i]+=rev[i];
        }
        for(int j=0;j<len;j++){
            if(is[j]>=N){
                is[j+1]+=is[j]/N;
                is[j]%=N;
                if(j==len-1){
                    len++;
                    return;
                }
            }
        }
    }
    char fuck(){
        for(int i=0;i<len;i++){
            if(is[i]!=is[len-i-1])
                return 0;
        }
        return 1;
    }
    int main()
    {
        
        char strs[101];
        scanf("%d%s",&N,strs);
        len=strlen(strs);
        for(int i=0;i<len;i++){
            is[len-i-1]=strs[i];
            if(is[len-i-1]>='A'){
                is[len-i-1]-=('A'-10);
            }else{
                is[len-i-1]-='0';
            }
        }
        int times=0;
        for(;times<30;times++){
            plus();
            if(fuck()){
                printf("STEP=%d",times+1);
                return 0;
            }
        }
        printf("Impossible!");
    }
    
  • 2
    @ 2017-06-02 17:44:58

    #include<iostream>
    #include<cstdio>
    using namespace std;

    int main()
    {
    int n;
    string m;
    cin>>n>>m;

    int v[20],vr[20]; //v是字符串转换为整数 下标小的放低位 下标大的放高位
    int len=m.length();
    for(int i=len-1,j=0;i>=0;i--,j++)
    {
    if(m[i]>='A')
    v[j]=m[i]-'A'+10; //处理十六进制

    else
    v[j]=m[i]-'0';
    }

    //逆序放入vr
    for(int i=0;i<len;i++)
    {
    vr[i]=v[len-i-1];
    }

    //相加
    for(int i=1;i<=30;i++)
    {
    int jinwei=0;
    for(int j=0;j<len;j++)
    {
    v[j]+=vr[j]+jinwei;
    jinwei=v[j]/n; //进位等于加起来除以进制
    v[j]%=n; //如果大于等于进制那么就会改变
    }
    if(jinwei>0)
    {
    v[len++]=jinwei; //如果最后有进位产生 那么就进一位
    }

    //判断是否是回文
    int s=0,e=len-1;
    while(s<=e)
    {
    if(v[s]!=v[e])
    break;

    s++;e--;
    }

    if(s>e)
    {
    printf("STEP=%d\n",i);
    return 0;
    }

    else
    {
    //逆序放入vr
    for(int i=0;i<len;i++)
    {
    vr[i]=v[len-i-1];
    }
    }

    }

    printf("Impossible!");

    return 0;
    }

  • 2
    @ 2016-08-31 16:16:20

    U41君来了!
    Pascal
    var
    n,step,i:longint;
    a,b,c:array [0..101] of longint;
    m:string;
    y:boolean;
    begin
    readln(n);
    readln(m);
    step:=0;
    y:=false;
    fillchar(a,sizeof(a),0);
    fillchar(b,sizeof(b),0);
    a[0]:=length(m);
    for i:=1 to a[0] do
    case m[a[0]-i+1] of
    '0'..'9':a[i]:=ord(m[a[0]-i+1])-48;
    'A'..'F':a[i]:=ord(m[a[0]-i+1])-55;
    end;
    b[0]:=a[0];
    for i:=1 to a[0] do b[i]:=a[a[0]-i+1];
    repeat
    inc(step);
    y:=true;
    fillchar(c,sizeof(c),0);
    for i:=1 to a[0] do
    begin
    c[i]:=c[i]+a[i]+b[i];
    c[i+1]:=c[i+1]+c[i] div n;
    c[i]:=c[i] mod n;
    end;
    if c[a[0]+1]>0 then c[0]:=a[0]+1 else c[0]:=a[0];
    for i:=1 to c[0] div 2 do
    if c[i]<>c[c[0]-i+1] then begin y:=false;break;end;
    if y then break;
    a:=c;
    for i:=1 to a[0] do b[i]:=a[a[0]-i+1];b[0]:=a[0];
    until step=30;
    if y then writeln('STEP=',step)
    else writeln('Impossible!');

    end.

  • 1
    @ 2017-07-22 11:10:54
    def palindrome(m, n):
        for i in range(30):
            m = to_dec(m, n) + to_dec(m[::-1], n)
            m = to_n(str(m), n)
            if m == m[::-1]:
                return 'STEP=%d' % (i+1)
        return 'Impossible!'
    def to_dec(m, n):
        return int(m.upper(), n)
    def to_n(m, n):
        num = int(m)
        if num == 0:
            return '0'
        result = ''
        while True:
            if num == 0:
                break
            remainder = num%n
            if remainder >= 10:
                remainder = chr(remainder-10+ord('A'))
            else:
                remainder = str(remainder)
            result += remainder
            num /= n
        return result[::-1].lstrip('0')
    
    n = int(raw_input())
    m = raw_input()
    print palindrome(m, n)
    
  • 1
    @ 2017-06-17 23:03:12

    新人第一道题, 因为内存给的不够和语文不好搞了一个Runtime Error和WA......
    看到题解大佬们各种高端的解法, 感觉我这个有点too young too simple了

    #include <iostream>
    
    using namespace std;
    
    long toDec(char s[], long d){
        long ret = 0;
        for(long i = 0; s[i] != '\0'; i++){
            if(s[i] <= '9' && s[i] >= '0')
                ret = ret * d + s[i] - '0';
            else
                ret = ret * d + s[i] - 'A' + 10;
        }
        return ret;
    }
    
    void Decto(long o, char s[], long d){
        long t = 0, i = 0;
        for(;o != 0; i++){
            t = o % d;
            if(t <= 9)
                s[i] = t + '0';
            else
                s[i] = t - 10 + 'A';
            o /= d;
        }
        s[i + 1] = '\0';
    }
    
    bool Check(char s[]){
        long i = 0;
        for(;s[i]!='\0';i++);
        for(long j = 0; j < i/2; j++)
            if(s[j] != s[i - j - 1])
                return false;
        return true;
    }
    
    void Flip(char s[]){
        long i = 0;
        for(;s[i]!='\0';i++);
        for(long j = 0; j < i/2; j++){
            char c = s[j];
            s[j] = s[i - j - 1];
            s[i - j - 1] = c;
        }
    }
    
    int main(int argc, char **argv) {
        char input[32] = { '\0' };
        char buff[32] = { '\0' };
        long b = 0;
        cin >> b >> input;
        long i = 0;
        long now = 0;
        if(Check(input)){
            cout << "STEP=0" << endl;
            return 0;
        }
        
        for(long i = 0;input[i]!='\0';i++)
            buff[i] = input[i];
        
        for(; i<=30;){
            i++;
            now = toDec(buff, b);
            Flip(buff);
            now += toDec(buff, b);
            Decto(now, buff, b);
            if(Check(buff)){
                cout << "STEP=" << i << endl;
                return 0;
            }
            Flip(buff);
        }
        cout << "Impossible!" << endl;     //一开始输出是STEP=Impossible!, 智障如我
        return 0;
    }
    

    初次接触oi, 萌新烦请(女装)大佬赐教.

  • 1
    @ 2017-06-03 14:38:28
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    void add_num(char result[],char num[], char addto[], int system)
    {
        int numlen = strlen(num);
        int addtolen = strlen(addto);
        int  i = 0;
        int j = 0;
        int t = 0;
        int num1 = 0, num2 = 0;
        int re = 0;
        char temp;
        int carry = 0;
        if (system == 16)
        {
            for (i = numlen - 1; i >= 0; i--)
            {
                if (num[i] >= 'A' && num[i] <= 'F') num1 = (num[i] - 'A') + 10;
                else num1 = num[i] - '0';
                if (addto[i] >= 'A' && addto[i] <= 'F') num2 = (addto[i] - 'A') + 10;
                else num2 = addto[i] - '0';
                re = (carry + num1 + num2) % system ;
                if (re >= 10)
                    result[t++] = 'A' + re - 10;
                else
                    result[t++] =  re+'0';
                carry = (carry + num1 + num2) / system;
            }
            if (carry != 0)
                result[t] += result[t] + carry + '0';
    
        }
        else
        {
    
            for (i = numlen - 1; i >= 0; i--)
            {
                result[t++] = (carry + (num[i] - '0') + (addto[i] - '0')) % system + '0';
                carry = (carry + (num[i] - '0') + (addto[i] - '0')) / system;
    
            }
            if (carry != 0)
                result[t] += result[t] + carry + '0';
        }
        
    
        i = 0; j = strlen(result)-1;
        while (i < j)
        {
            t = result[i];
            result[i] = result[j];
            result[j] = t;
            i++;
            j--;
        }
        
        //printf("%s\n", result);
    
    }
    void change(char addto[], char num[])
    {
        int i = 0, j = 0;
        for (i = 0, j = strlen(num) - 1; i < strlen(num); i++, j--)
            addto[i] = num[j];
    }
    int ispalindrome(char result[])
    {
        int i = 0, j = 0;
        i = 0; j = strlen(result) - 1;
        while (i < j)
        {
            if (result[i] != result[j])
                return 0;       
            i++;
            j--;
        }
        return 1;
    }
    
    int main()
    {
        //freopen("data.in", "r", stdin);
        //freopen("data.out", "w", stdout);
        int N = 0;
        int i = 0;
        char num[500];
        char addto[500];
        char result[500];
        scanf("%d", &N);
        scanf("%s", num);
        
    
        memset(result, 0, sizeof(result));
        memset(addto, 0, sizeof(addto));
    
        
        for (i = 0; i < 30; i++)
        {
            change(addto, num);
            add_num(result, num, addto, N);
            //printf("%s\n", result);
            if (ispalindrome(result) == 1)
            {
                printf("STEP=%d\n", i+1);
                return 0;
            }
            else
            {
                strcpy(num, result);
                memset(result, 0, sizeof(result));
                memset(addto, 0, sizeof(addto));        
            }
        }
        printf("Impossible!\n");
        return 0;
    }
    
  • 1
    @ 2017-05-09 22:55:05
    //新手强答一波,模拟高精
    #include <bits/stdc++.h>
    const int MAXN = 100;
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    using std::ostream;
    
    class BigInteger
    {
    private:
        int data[MAXN];
        int length;
    public:
        BigInteger(string str);
        BigInteger(const BigInteger & s);
        BigInteger(int arr[], int _length);
    
        friend BigInteger add(const BigInteger & x, const BigInteger & y, const int N);
        friend BigInteger reverse(const BigInteger & x);
        friend bool check(const BigInteger & x);
        friend ostream & operator<<(ostream & os,const BigInteger x);
        
    };
    
    BigInteger::BigInteger(string str)
    {
        length = str.size();
        for(int i = length - 1; i >= 0; i--)
            if(str[i] >= '0' && str[i] <= '9')
              data[length - i - 1] = str[i] - '0';
            else if(str[i] >= 'A' && str[i] <= 'Z')
              data[length - i - 1] = str[i] - 55;
    }
    
    BigInteger::BigInteger(const BigInteger & s)
        {
        length = s.length;
        for(int i = 0; i < length; i++)
            data[i] = s.data[i];
        }
    
    BigInteger::BigInteger(int arr[], int _length)
        {
         length = _length;
         for(int i = 0; i < length; i++)
            data[i] = arr[i];
        }
    
    BigInteger add(const BigInteger & x, const BigInteger & y, const int N)
       {
            int arr[MAXN] = {0}, _length = (x.length > y.length) ? x.length : y.length;
        
            for(int i = 0; i < _length; i++)
                arr[i] = x.data[i] + y.data[i];
            for(int i = 0; i < _length; i++)
            { 
               arr[i + 1] += arr[i] / N;
               arr[i] %= N;
            }
           if(arr[_length] != 0) _length++; 
    
           BigInteger tmp(arr, _length);
           return tmp; 
        }
    
    BigInteger reverse(const BigInteger & x)
        {
            int arr[MAXN],_length;
            _length = x.length;
            for(int i = 0; i < _length; i++)
                arr[i] = x.data[x.length - i - 1];
            BigInteger tmp(arr, _length);
            return tmp; 
        }
    
    bool check(const BigInteger & x)
        {
            for(int i = 0; i < x.length/2; i++)
                if(x.data[i] != x.data[x.length - i - 1])
                   return false;
            return true;
        } 
    
    ostream & operator<<(ostream & os,const BigInteger x)
        {
            for(int i = x.length - 1; i >= 0; i--)
                cout << x.data[i]; 
            return os;   
        }  
    
    
        int main()
        { 
            int N, count = 1;
            cin >> N;
            string a;
            cin >> a;
            BigInteger A(a);
            while(count <= 30)
            {
                BigInteger tmp = add(A, reverse(A), N);
                if(check(tmp))
                    {
                        cout << "STEP=" << count << endl;
                        return 0;
                    }
    
                count++;
                A = tmp;    
            }
            cout << "Impossible!" << endl;
            return 0;
        }
    
    
  • 1
    @ 2017-05-06 19:31:45

    #include <iostream>
    #include <cstdio>
    #include <string.h>
    #include <cmath>
    using namespace std;
    const int maxn = 10100;
    char a[maxn];
    int res[maxn];
    int t[maxn];
    bool fun(int *t,int n){ //判断是否是回文
    for(int i=0;i<n/2;i++){
    if(t[i]!=t[n-i-1])
    return false;
    }
    return true;
    }
    int main(){
    int n,i;
    scanf("%d",&n);
    scanf("%s",a);
    for(i=0; i<strlen(a); ++i){ //char类型转换成int
    if(a[i]<='9'&&a[i]>='0'){
    res[i] = a[i] - '0';
    }else{
    res[i] = a[i] - 'A' + 10;
    }
    }
    bool flag = false;
    for(int j=0;j<=30;j++){
    memset(t,0,sizeof(t));
    if(fun(res,i)){
    printf("STEP=%d\n",j);
    flag = true;break;
    }else{
    for(int k=0;k<i;k++){
    t[k] += (res[k] + res[i-k-1]);
    t[k+1] = t[k] / n;
    t[k] %= n;
    }
    if(t[i]!=0)
    i++;
    for(int k=0;k<i;k++){
    res[k] = t[k];
    }
    }
    }
    if(flag == false)
    printf("Impossible!\n");
    return 0;
    }

  • 1
    @ 2017-03-01 11:13:41

    #include <stdio.h>

    int slong(char *a)
    {
    int i=99;
    while(1){
    if(a[i]=='\0') i--;
    if(a[i]!='\0') break;
    }return (i+1);
    }
    int huiwen(char *a)
    {
    int i,k;
    k=slong(a);///printf("k=%dk",k);
    for(i=0;i<k;i++)
    if(a[i]!=a[k-i-1]) return 0;
    return 1;
    }
    int main()
    {
    int N;
    scanf("%d",&N);
    if(N!=16){
    int i,k,step=0;
    char a[100],b[100];
    for (i=0;i<100;i++) a[i]='\0';
    scanf("%s",a);k=slong(a);for (i=0;i<k;i++) a[i]=a[i]-'0';
    //printf("%d",huiwen(a));
    while(1)
    {
    if (huiwen(a)==1) break;
    if (step>=30) break;
    k=slong(a);
    for(i=0;i<k;i++) b[i]=a[i]+a[k-i-1];
    for(i=0;i<k;i++) {a[i]=b[i];b[i]=0;}
    for(i=0;i<k;i++)
    {
    a[i+1]+=a[i]/N;
    a[i]=a[i]%N;
    }
    step++;
    }
    if(step<30) printf("STEP=%d",step);
    else printf("Impossible!");
    }
    if(N==16){
    int i,k,step=0;
    char a[100],b[100];
    for (i=0;i<100;i++) a[i]='\0';
    scanf("%s",a);k=slong(a);
    for (i=0;i<k;i++) {
    if(a[i]>='0'&&a[i]<='9')
    a[i]=a[i]-'0';
    else a[i]=a[i]-'A'+10;
    }
    //printf("%d",huiwen(a));
    while(1)
    {
    if (huiwen(a)==1) break;
    if (step>=30) break;
    k=slong(a);
    for(i=0;i<k;i++) b[i]=a[i]+a[k-i-1];
    for(i=0;i<k;i++) {a[i]=b[i];b[i]=0;}
    for(i=0;i<k;i++)
    {
    a[i+1]+=a[i]/N;
    a[i]=a[i]%N;
    }
    step++;
    }
    if(step<30) printf("STEP=%d",step);
    else printf("Impossible!");
    }
    return 0;
    }

  • 1
    @ 2017-01-22 14:15:41

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<string>
    using namespace std;
    int a[1000000]={0},c[1000000],p,tot=0,ta=1,t1=1;
    string n;
    int power(int m[],int k)//请无视这个函数名..做别的题忘了改
    {
    int l=0;
    memset(c,0,sizeof(c));
    for(int j=1;j<=ta;++j)
    {
    c[j]=m[ta-j+1]+m[j];
    }
    for(int t=1;t<=ta+1;t++)
    if(c[t]>=k)
    {
    c[t+1]+=c[t]/k;
    c[t]%=k;
    }
    ta=ta+1;
    while(1)
    {
    if(c[ta]==0)ta--;
    else break;
    }
    for(int t=ta;t>=1;--t)
    {
    if(c[t]==c[ta-t+1])l++;
    m[t]=c[t];
    }
    if(l==ta)return 1;
    else return 0;
    }
    void get(string z,int m[])
    {
    int l1=z.length();
    for(int i=1;i<=l1;++i)
    {
    switch(z[l1-i])//要注意16进制时读入高精度要进行判断
    {
    case 'A':{m[i]=10;break;}
    case 'B':{m[i]=11;break;}
    case 'C':{m[i]=12;break;}
    case 'D':{m[i]=13;break;}
    case 'E':{m[i]=14;break;}
    case 'F':{m[i]=15;break;}
    default:m[i]=z[l1-i]-'0';
    }
    }
    }
    int main()
    {
    cin>>p>>n;
    get(n,a);
    ta=n.length();
    do
    {
    tot++;
    if(tot==30){cout<<"Impossible!"<<endl;return 0;}
    }while(power(a,p)==0);
    cout<<"STEP="<<tot;
    return 0;
    }

  • 1
    @ 2016-09-17 22:31:39

    忘了注释掉调试用语句了,错了两次poi~
    ~~~C++
    #include <iostream>
    #include <string>
    using namespace std;

    int string_into_int(char a)
    {
    if ((48 <= a + 0) && (a + 0 <= 57)) {
    return (a - 48);
    }
    if ((65 <= a + 0) && (a + 0 <= 90)) {
    return (a - 55);
    }
    }

    bool hw(string str)
    {
    string rst;
    for (auto f : str) {
    rst = f + rst;
    }
    if (rst == str){
    return true;
    }
    else { return false; }
    }

    int main()
    {
    string s = "0123456789ABCDEF";
    string s1, s2, result;
    int n, count=0;
    while (cin >> n >> s1) {
    count = 0;
    result = s1;
    while (hw(result) == false){
    if (count == 30) { cout << "Impossible!" << endl; break; }
    count += 1;
    if (result == "result") { result = ""; }
    else { s1 = result; result = ""; }
    for (auto f : s1) {
    s2 = f + s2;
    }
    int plusone = 0;
    for (int i = s1.size() - 1; i != -1; --i) {
    int res = string_into_int(s1[i]) + string_into_int(s2[i]) + plusone;
    plusone = res / n;
    result = s[res - plusone * n] + result;
    }
    if (plusone == 1) {
    result = '1' + result;
    plusone = 0;
    }
    //cout << result << endl;
    }
    if (count != 30) {cout << "STEP=" << count << endl; }
    }
    return 0;
    }
    ~~~

  • 1
    @ 2016-08-28 11:12:38

    其实在判断16进制时并不用转换成'A'....而用数组的特性就可以完成,需要注意输入时字母的情况(本人就错在这)
    代码:
    var
    m,a:array[0..1000] of longint;
    n,i,j,jw:longint;
    ch:char;
    function check:boolean;
    var
    i:longint;
    begin
    for i:=1 to m[0] div 2 do
    if m[i]<>m[1+m[0]-i] then
    exit(false);
    exit(true);
    end;
    begin
    readln(n);
    while not eoln do
    begin
    inc(m[0]);
    read(ch);
    if ch in ['A'..'Z'] then m[m[0]]:=ord(ch)-55
    else m[m[0]]:=ord(ch)-48;
    end;
    for i:=1 to 30 do
    begin
    jw:=0;
    for j:=1 to m[0] do
    begin
    a[j]:=m[j]+m[1+m[0]-j]+jw;
    jw:=a[j] div n;
    a[j]:=a[j] mod n;
    end;
    a[0]:=m[0];
    if jw>0 then
    begin
    inc(a[0]);
    a[a[0]]:=jw;
    end;
    m:=a;
    if check then
    begin
    writeln('STEP=',i);
    halt;
    end;
    end;
    writeln('Impossible!');
    end.

  • 1
    @ 2016-07-15 21:31:54

    重点是无解时输出'Impossible!',感叹号很重要,很容易错
    var
    n,i,len,k,j,x,y:longint;
    s:string;
    f:boolean;
    num,num2,num3:array[1..100] of integer;
    begin
    readln(n);
    readln(s);
    len:=length(s);
    for i:=1 to len do
    begin
    j:=len-i+1;
    case s[i] of
    '0'..'9' : val(s[i],num[j]);
    'A' : num[j]:=10;
    'B' : num[j]:=11;
    'C' : num[j]:=12;
    'D' : num[j]:=13;
    'E' : num[j]:=14;
    'F' : num[j]:=15;
    end;
    end;
    k:=0;
    repeat
    inc(k);
    for i:=1 to len do num2[len-i+1]:=num[i];
    for i:=1 to len do num3[i]:=num[i]+num2[i];
    for i:=1 to len do
    if num3[i]>=n then
    begin
    inc(num3[i+1],num3[i] div n);
    num3[i]:=num3[i] mod n;
    end;
    if num3[len+1]<>0 then inc(len);
    num:=num3;f:=true;x:=1;y:=len;
    while x<=y do
    begin
    if num3[x]<>num3[y] then f:=false;
    inc(x);dec(y);
    end;
    until f or (k>30);
    if f then writeln('STEP=',k)
    else writeln('Impossible!');
    end.

  • 0
    @ 2019-02-11 17:35:57

    #include <iostream>
    #define max 300
    using namespace std;
    class cnum{
    private:
    int arr[max],r,t,times;
    public:
    cnum(char *c,int g,int sn);
    int gettimes();
    int check();
    void puls();
    };
    cnum::cnum(char *c,int g,int sn){
    t=g;times=0;r=sn;
    for(int i=0;i<300;i++){
    arr[i]=0;
    }
    for(int i=0;i<=sn;i++){
    switch(c[sn-i]){
    case 'A':arr[299-i]=10;break;
    case 'B':arr[299-i]=11;break;
    case 'C':arr[299-i]=12;break;
    case 'D':arr[299-i]=13;break;
    case 'E':arr[299-i]=14;break;
    case 'F':arr[299-i]=15;break;
    default:arr[299-i]=int(c[sn-i])-48;
    }
    }
    }
    int cnum::gettimes(){
    for(int i=0;i<40;i++){
    if(check()){
    return times;
    }else{
    puls();
    }
    }
    return times;
    }
    int cnum::check(){
    for(int i=0;i<=r;i++){
    if(arr[299-r+i]!=arr[299-i]){
    return 0;
    }
    }
    return 1;
    }
    void cnum::puls(){
    int *fuck=new int[300];
    for(int i=0;i<300;i++){
    fuck[i]=arr[i];
    }
    for(int i=0;i<=r;i++){
    arr[299-i]+=fuck[299-r+i];
    if(arr[299-i]>=t){
    arr[299-i-1]+=1;
    arr[299-i]-=t;
    }
    }
    if(arr[299-r-1]){
    r++;
    }
    times++;
    delete []fuck;
    }
    int main(){
    int number,sb=0;char string[300]={' '};
    cin>>number;
    cin>>string;
    for(int i=299;i>=0;i--){
    if(string[i]){
    sb=i;break;
    }
    }
    cnum a(string,number,sb);
    if(a.gettimes()>=0&&a.gettimes()<=30){
    cout<<"STEP="<<a.gettimes();
    }else{
    cout<<"Impossible!";
    }
    return 0;
    }
    /////笨办法

  • 0
    @ 2019-02-04 00:47:57

    投机取巧了一波
    转换来转换去速度大概是慢了许多

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    using namespace std;
    char buffer[256];
    char *stop;
    long int M,A,sum;
    int N,pos,cnt=0,flag=0;
    void itoa(long int n, char*str, int base){
        if(base == 16){
            int i=0;
            while ( n > 0){
                str[i++] = n % base<10? (n%base)+'0':(n%base)-10+'A';
                n /= base;
            }
        }else{
            int i=0;
            while (n > 0){
                str[i++] = (n % base)+'0';
                n /= base;
            }
        }
    }
    void getReverse(){
        pos = 0;
        while((buffer[pos]<='9'&&buffer[pos]>='0')|| (buffer[pos]<='F'&&buffer[pos]>='A')){
            pos++;
        }
        reverse(buffer,buffer+pos);
    }
    int main(){
        scanf("%d%s",&N,buffer);
        while(cnt <= 30){
            M = strtol(buffer, &stop,N);
            getReverse();
            A = strtol(buffer, &stop,N);
            if(M == A){
                flag = 1;
                break;
            }
            sum = M + A;
            itoa(sum, buffer,N);
            cnt++;
        }
        if(flag) printf("STEP=%d",cnt);
        else printf("Impossible!");
    }
    
  • 0
    @ 2019-01-23 20:12:09

    解释在代码里了,用函数做模拟真的爽

    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    int n;//n进制
    
    int judge(string a)  //判断是不是回文串
    {
        string t=a;
        reverse(a.begin(),a.end());
        for(int i=0;i<a.size();i++)
        {
            if(a[i]!=t[i])
                return 0;
        }
        return 1;
    }
    
    string sum(string a)//n在[2,,10]之间的
    {
        string t=a;
        reverse(a.begin(),a.end());
        string ans;
        int add=0;
        for(int i=a.size()-1;i>=0;i--)
        {
            char temp= (  ( (a[i]-'0') + (t[i]-'0') + add )%n  )+'0';
            ans.insert(ans.end(),temp);
            add=( (a[i]-'0') + (t[i]-'0') + add )  / n ;
        }
        if(add!=0)
            ans.insert(ans.end(),(char)(add+'0'));
        return ans;
    }
    
    int turn(char c)
    {
        if(c>='A'&&c<='F')
            return c-'A'+10;
        else
            return c-'0';
    }
    
    char temp_turn(int key)
    {
        if(key>=10)
        {
            char c=(char)(key-10+'A');
            return c;
        }
        else
            return (char)(key+'0');
    
    }
    
    string sum_sixteen(string a)//16进制的加法
    {
        string t=a;
        reverse(a.begin(),a.end());
        string ans;
        int add=0;
        for(int i=a.size()-1;i>=0;i--)
        {
            char temp= temp_turn( (turn(a[i])+turn(t[i])+add)%16 ) ;
            ans.insert(ans.end(),temp);
            add=( turn(a[i])+turn(t[i])+add ) /16 ;
        }
        if(add!=0)
            ans.insert(ans.end(),temp_turn(add));
        return ans;
    }
    
    int main()
    {
        string a;
        int ans_sum=1;
        cin>>n;
        cin>>a;
        if(n==16)
        {
            int tag=0;
            while(tag!=1&&ans_sum<=30)
            {
                a=sum_sixteen(a);
                if(judge(a)==0)
                    ans_sum++;
                else
                {
                    tag=1;
                    break;
                }
            }
            if(tag==0)
                cout<<"Impossible!"<<endl;
            else
                cout<<"STEP="<<ans_sum<<endl;
        }
        else
        {
            int tag=0;
            while(tag!=1&&ans_sum<=30)
            {
                a=sum(a);
                if(judge(a)==0)
                    ans_sum++;
                else
                {
                    tag=1;
                    break;
                }
            }
            if(tag==0)
                cout<<"Impossible!"<<endl;
            else
                cout<<"STEP="<<ans_sum<<endl;
        }
    
        return 0;
    }
    
    
  • 0
    @ 2018-06-11 08:26:16

    /*
    题目名称:回文数
    题目难度:塑料
    */
    #include<iostream>
    #include<cstdio>
    using namespace std;
    int a[100];
    int temp[100];
    int l,n;
    //每一次运行add,则将a的那啥相加算出来
    //temp是辅助存储的数组
    void add(){
    int pi=0;//pi是记录进不进位的
    int t=0;//t是辅助计算的
    for(int i=1;i<=l;i++){
    t=a[i]+a[l-i+1]+pi;
    if(t>=n){
    t-=n;
    pi=1;
    }else pi=0;
    temp[i]=t;
    }
    if(pi){
    temp[l+1]=1;
    l++;
    }
    for(int i=1;i<=l;i++)a[i]=temp[i];
    }
    //check是用来检验是否为回文数的
    bool check(){
    for(int i=1;i<=l;i++){
    if(a[i]!=a[l-i+1])
    return false;
    }
    return true;
    }
    int main(){
    scanf("%d\n",&n);
    char s;
    if(n==16){
    for(l=1;;l++){
    s=getchar();
    if(s=='A')a[l]=10;
    else if(s=='B')a[l]=11;
    else if(s=='C')a[l]=12;
    else if(s=='D')a[l]=13;
    else if(s=='E')a[l]=14;
    else if(s=='F')a[l]=15;
    else{
    if(s=='\n')break;
    a[l]=s-'0';
    }
    }
    }
    else {
    for(l=1;;l++){
    s=getchar();
    if(s=='\n')break;
    a[l]=s-'0';
    }
    }

    l--;
    for(int i=1;i<=30;i++){
    add();
    if(check()){
    printf("STEP=%d",i);
    return 0;
    }
    }
    printf("Impossible!");

    return 0;
    }

  • 0
    @ 2018-05-27 17:34:09

    使用数组进行模拟,16进制的话只需要将字符替换成对应的数字就OK
    我的数组记录顺序是从最低位到最高位,而且输入的时候不需要颠倒顺序
    因为在STEP = 1的时候我已经将它倒序过来相加了
    和颠倒顺序之后操作的结果一样
    cpp
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int num = 505;
    int A[num],B[num];
    char array[num];
    int k,l,ans = 0;
    int getnum(char x) {
    if(x >= '0' && x <= '9')
    return x - '0';
    else return x - 'A' + 10;
    }
    bool check() {
    bool flag = true;
    for(int i = 1;i <= l / 2;i++)
    if(A[i] != A[l - i + 1]) {
    flag = false;
    break;
    }
    return flag;
    }
    void cnt() {
    int push = 0;
    for(int i = 1;i <= l;i++)
    B[i] = A[l - i + 1];
    for(int i = 1;i <= l;i++) {
    int temp = A[i] + B[i];
    temp += push;
    A[i] = temp % k;
    push = temp / k;
    }
    if(push != 0) A[++l] = push;
    ans ++;
    if(check() || ans > 30) return;
    else cnt();
    }
    int main() {
    cin >> k;
    scanf("%s",array);
    l = strlen(array);
    for(int i = 1;i <= l;i++) {
    A[i] = getnum(array[i - 1]);
    }
    cnt();
    if(ans < 31) cout << "STEP=" << ans;
    else cout << "Impossible!";
    return 0;
    }

  • 0
    @ 2018-03-30 00:34:47

    AC了来这献个丑吧

    #include <cstdio>
    using namespace std;
    long long ten(int x)
    {
        long long y = 1;
        for(int i = 0; i < x; i++) y *= 10;
        return y;
    }
    long long flip(long long a, int x)
    {
        int w = 10;
        if(x == 16) w = 16;
        long long c = a, b = 0;
        while( c ){
            b *= w;
            b += c%w;
            c /= w;
        }
        return b;
    }
    long long add(long long a, int x)
    {
        if(x == 16 || x == 10) return a+flip(a, x);
        long long b = flip(a,x);
        long long c = 0;
        int i = 0;
        char s[100001] = {0};
        while( a || b ){
            s[i] += a%10 + b%10;
            if(s[i] >= x){
                s[i+1] += s[i]/x;
                s[i] %= x;
            }
            a /= 10;
            b /= 10;
            i++;
        }
        for(int j = 0; j <= i; j++){
            c += s[j]*ten(j);
        }
        return c;
    }
    int main()
    {
    
        int N;
        scanf("%d", &N);
        long long M;
        if(N == 16) scanf("%llX", &M);
        else scanf("%lld", &M);
        int i;
        for(i = 0; i <= 30; i++){
    ///////////
    //      if(N == 16) printf("%llX\n", M);
    //      else printf("%lld\n", M);
    // ///////////
            if(M == flip(M, N)){
                break;
            }
            M = add(M, N);
        }
        if(i > 30){
            printf("Impossible!\n");
        }else{
            printf("STEP=%d\n", i);
        }
        return 0;
    }
    
    

信息

ID
1304
难度
5
分类
模拟 点击显示
标签
递交数
7008
已通过
2206
通过率
31%
上传者