题解

211 条题解

  • 31
    @ 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

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

    • @ 2020-03-10 14:51:49

      Compile Error
      /in/foo.cc: In function 'int main()':
      /in/foo.cc:38:20: error: 'strlen' was not declared in this scope
      len=strlen(strs);
      ^

  • 5
    @ 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
    @ 2020-02-04 00:54:58

    这道题分为两个模块:
    1.判断回文数
    2.数据的正逆之和
    ☆重点讲下第二模块
    ①数据逆序
    ②对两个数组,N进制->十进制,再求和
    重复进行这两个模块就能得到结果。
    此处定义了反转函数,进制转换函数n->10,10->n
    大家可以复制下来进行使用。

    #include<iostream>
    #include<string>
    #include<string.h>
    #include<cmath>
    using namespace std;
    bool Judge(const char* a) {//进制回文数的判断
        int size = strlen(a);
        for (int i = 0; i < size; i++)if (a[i] != a[size - i - 1]) {
            return false;
        }
        return true;
    }
    string turn_str(string M) {//字符串反转
        string M_copy = M;
        for (int i = 0; i < (int)strlen(M.c_str()); i++)M[i] = M_copy[(int)strlen(M.c_str()) - i - 1];
        return M;
    }
    long int demical(string M, int n) {//(n)进制->10进制
        long int dem=0;
        for (int i = 0; i < (int)strlen(M.c_str()); i++)
        {
            if ((long int)M[i]>=65)dem += ((long int)M[i] - 55) * (long int)pow(n, (int)strlen(M.c_str())-i-1);
            else dem += ((long int)M[i] - 48) * (long int)pow(n, (int)strlen(M.c_str())-i-1);
        }
        return dem;
    }
    string ochange(long int num, int n) {//10进制->(n)进制
        char* a=new char[100];
        int i;
        for (i = 0; num != 0; i++) {
            char b;
            if (num % n < 10) b = (char)(num % n + 48);
            else b = (char)(num % n + 55);
            num = (num - (num % n)) / n;
            a[i] = b;
        }
        a[i] = 0;
        string a2 = string(a);
        a2 = turn_str(a2);
        return a2;
    }
    string sum(string M, int n) {//字符串正逆之和
        long int num = demical(M, n) + demical(turn_str(M), n);
        string a = ochange(num, n);
        return a;
    }
    int main()
    {
        int N, step=0;
        string M;
        cin >> N >> M;
        for (int i = 0; i < 30; i++)
        {
            if (Judge(M.c_str()))break;
            step++;
            M = sum(M, N);
        }
        if (step == 30)cout << "Impossible!";
        else cout << "STEP=" << step;
        return 0;
    }
    
  • 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
    @ 2021-09-04 15:21:47
    #include <bits/stdc++.h>
    using namespace std;
    
    const int S=303;
    int n,a[S],l;
    char c[S],d[S];
    inline void add()
    {
        for (int i=0;i<l;++i)
            d[l-i-1]=c[i];
        l+=2;
        for (int i=0;i<l;++i){
            c[i]+=d[i];
            if(c[i]>=n)
                c[i+1]++,c[i]-=n;
        }
        while(!c[l-1])
            --l;
    }
    
    inline bool pd()
    {
        for(int i=0;i<l;++i)
            if(c[i]!=c[l-1-i])
                return false;
        return true;
    }
    
    int main()
    {
        cin>>n>>c;
        l=strlen(c);
        for (int i=0; i<l; ++i){
            if(c[i]>='0' && c[i]<='9')
                c[i]-='0';
            else
                c[i]=c[i]-'A'+10;
        }
        int step=0;
        while(!pd()){
            ++step;
            if(step>30)
                break;
            add();
        }
        if(step<=30)
            cout<<"STEP="<<step;
        else
            cout<<"Impossible!";
        return 0;
    }
    
  • 1
    @ 2020-10-11 00:04:09
    
    ```trans = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
             '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15}
    transform = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
    
    
    def calculator(jinzhi, num_1, num_2):
        result = ""
        carry = 0
        for i in range(len(num_1)):
            _sum = trans[num_1[-1 - i]] + trans[num_2[-1 - i]] + carry
            carry = _sum // jinzhi
            result += transform[_sum % jinzhi]
        if carry != 0:
            result += transform[carry]
        return result[::-1]
    
    
    if __name__ == '__main__':
        flag = False
        num = 0
        jinzhi = int(input())
        number_1 = input()
        for i in range(30):
            number_2 = number_1[::-1]
            number_1 = calculator(jinzhi, number_1, number_2)
            if number_1 == number_1[::-1]:
                flag = True
                num = i + 1
                break
        if flag:
            print('STEP=' + str(num))
        else:
            print('Impossible!')
    
  • 1
    @ 2020-08-12 11:13:09

    请大佬指点
    #1 Accepted 1ms 216.0 KiB
    #2 Accepted 0ms 228.0 KiB
    #3 Accepted 0ms 216.0 KiB
    #4 Accepted 0ms 232.0 KiB
    C语言

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int Cal(char A[],int time,int N){
        int l=strlen(A),num=0,out=0,in=0,i=0;
        char B[10001]={'\n'};
        while(i<l||out!=0){
            if(i<l){        
                if(A[i]>64){
                    num=A[i]-'0'-7;
                }
                else{
                    num=A[i]-'0';
                }
                if(A[l-1-i]>64){
                    num=num+A[l-1-i]-'0'-7;
                }
                else{
                    num=num+A[l-1-i]-'0';
                }
            }
            else{
                num=0;
            }
            in=(num+out)%N;
            if(in<10){
                B[i]=in+'0';
            }
            else{
                B[i]=in+'0'+7;
            }
            out=(num+out)/N;
            i++;
        }
        strcpy(A,B);
        time++;
        return time;
    }
    
    int main(){
        int N;
        char M[10001]={'\n'};
        int time=0,l=0,yes=0;
        scanf("%d",&N);
        getchar();
        scanf("%s",M);
        while(time<30){
            time=Cal(M,time,N);
            l=strlen(M);
            for(int i=0;i<(l/2);i++){
                if(M[i]!=M[l-1-i]){
                    yes=1;
                    break;
                }
            }
            if(yes==0){
                printf("STEP=%d",time);
                break;
            }
            if(time==30&&yes==1){
                printf("Impossible!");
                break;
            }
            yes=0;
        }
    }
    
  • 1
    @ 2019-04-03 16:32:32
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int n;
    char hexx[6]={'A','B','C','D','E','F'};
    bool huiWen(string t){
      for(int i=0;i<t.size()/2;++i)
        if(t[i]!=t[t.size()-i-1]) return false;
      return true;
    }
    string add(string t1,string t2){
      string res(t1.size(),0); //初始化长度
      int jw=0,t=0;
      for(int i=t1.size()-1;i>=0;--i){
        if(n<=10){
          t=t1[i]-'0'+t2[i]-'0'+jw;
          res[i]=t%n+'0';
          jw=t/n;
        }else{
          t=0;
          if(t1[i]>='0'&&t1[i]<='9') t+=t1[i]-'0';
          else t+=t1[i]-'A'+10;
          if(t2[i]>='0'&&t2[i]<='9') t+=t2[i]-'0';
          else t+=t2[i]-'A'+10;
          t+=jw;
          if(t%n>=10) res[i]=hexx[t%n-10];
          else res[i]=t%n+'0';
          jw=t/n;
        }
      }
      if(jw>0) res=(char)(jw+'0')+res; //jw<=3
      return res;
    }
    int main()
    {
      cin>>n;
      string s,s0; cin>>s; s0=s;
      int cnt=0;
      if(huiWen(s)) {cout<<"0"; return 0;}
      while(1){
        reverse(s0.begin(),s0.end());
        s0=s=add(s0,s);
        ++cnt;
        if(huiWen(s)) break;
        if(cnt>30) break;
      }
      if(cnt>30) cout<<"Impossible!";
      else cout<<"STEP="<<cnt;
        return 0;
    }
    
  • 1
    @ 2019-03-02 22:55:53

    不要用字符串存数字
    开个数组存当前数字
    至于加法,新开一个数组倒着存
    由于是加法,所以只可能进1,那么一个if就好啦(虽然在这里体现不出优越性,但%的确要慢一点),特判一下,如果当前数位大于等于进制,就把下一位++,然后当前位-=n
    ```cpp
    #include<bits/stdc++.h>
    using namespace std;

    int n, tot;
    int num[100000];

    inline void Reverse(){
    int temp[100000];
    memset(temp,0,sizeof(temp));
    for(register int i=1; i<=tot; i++){
    temp[i] += num[i]+num[tot-i+1];
    if(temp[i]>=n){
    temp[i+1] ++;
    temp[i] -= n;
    }
    }
    if(temp[tot+1]) tot++;
    for(register int i=1; i<=tot; i++)
    num[i] = temp[tot-i+1];
    }

    inline bool Check(){
    for(register int i=1; i<=tot/2; i++)
    if(num[i]!=num[tot-i+1])
    return false;
    return true;
    }

    int main(){
    int step=0;
    char ch;
    scanf("%d\n", &n);
    while((ch=getchar())!=-1){
    if(isdigit(ch))
    num[++tot]=ch-'0';
    else if(ch>='A'&&ch<='F')
    num[++tot]=ch-'A'+10;
    }
    //for(register int i=1; i<=tot; i++) printf("%d", num[i]);
    while(step<=30){
    step++;
    Reverse();
    //for(register int i=1; i<=tot; i++)
    //printf("%d", num[i]);
    //putchar('\n');
    if(Check()){
    printf("STEP=%d\n", step);
    return 0;
    }
    }
    printf("Impossible!\n");
    return 0;
    }
    ```

  • 1
    @ 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;
    }
    
    
  • 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.

信息

ID
1304
难度
5
分类
模拟 点击显示
标签
递交数
8080
已通过
2532
通过率
31%
被复制
31
上传者