题解

211 条题解

  • 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
    @ 2023-08-23 10:19:07

    一种思路:查表
    def change(N,M):
    list=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
    num=len(M)
    flag=0
    value=[]
    for i in range(num):
    temp=list.index(M[i])+list.index(M[num-i-1])+flag
    if (temp>=N):
    flag=1
    temp=temp%N
    else:
    flag=0
    value.append(list[temp])
    if flag:
    value.append(list[1])
    return value

    def judge(value):
    flag=1
    num=len(value)
    for i in range(num//2+1):
    if (value[i]!=value[num-i-1]):
    flag=0
    break

    return flag

    N=eval(input())
    M=list(input())
    fl=1

    for i in range(1,31,1):
    M=change(N,M)
    if judge(M):
    print('STEP={:d}'.format(i))
    fl=0
    break

    if fl:
    print('Impossible!')

  • 0
    @ 2021-10-21 02:28:49

    STL大法好,STL+高精解法

    #include<bits/stdc++.h>
    using namespace std;
    char cnums[17] = "0123456789ABCDEF";
    inline int ctoi(char c,int cyy) {
        string s;
        s.push_back(c);
        return stoi(s, 0, cyy);
    }
    string add(string a,string b,int cyy) {
        string res = "";
        int pa, pb, sum, cy = 0;
        pa = a.length();
        pb = b.length();
        do
        {
            pa == 0 ? a[pa] = '0' : pa--;
            pb == 0 ? b[pb] = '0' : pb--;
            sum = ctoi(a[pa], cyy) + ctoi(b[pb], cyy) + cy;
            res = cnums[sum % cyy] + res;
            cy = sum / cyy;
        } while (pa!=0||pb!=0||cy!=0);
        return res;
    }
    
    int main() {
        int cyy, time = 0;
        string snum, revstr;
        cin >> cyy >> snum;
        while (true)
        {
            if (time == 30) {
                cout << "Impossible!";
                return 0;
            }
            revstr.resize(snum.size());
            reverse_copy(snum.begin(), snum.end(), revstr.begin());
            if (revstr == snum) break;
            time++;
            snum = add(snum, revstr, cyy);
        }
        cout << "STEP=" << time;
        return 0;
    }
    
  • 0
    @ 2021-06-04 11:10:24
    #include <cstring>
    #include <iostream>
    bool check(char s[])
    {
        bool YorN = 1;
        for (int i = 0; i < strlen(s); i++)
        {
            if (s[i] != s[strlen(s)-1-i])
            {
                YorN = 0; 
            }
        }
        return YorN;
    }
    
    void change(int n, char s[])
    {
        int x = 0, lenc = 0, i, num_tou, num_wei;
        int c[62510];
        for (i = strlen(s)-1; i >= 0; i--)
        {
            if (s[i] <= '9') {num_tou = s[i] - '0'; }
            else {num_tou = s[i] - 55;}
            if (s[strlen(s) - 1 - i] <= '9') {num_wei = s[strlen(s) - 1 - i] - '0'; }
            else {num_wei = s[strlen(s) - 1 - i] - 55;}
            c[lenc] = num_tou + num_wei + x;
            x = c[lenc] / n;
            c[lenc] %= n;
            lenc ++;
        }
        c[lenc] = x;
        lenc = lenc - (c[lenc]== 0);    
        for (i = 0; i <= lenc; i++)
        {
            if (c[lenc - i] < 10) {s[i] = c[lenc - i] + '0';}
            else {s[i] = c[lenc - i] + 55;}
        }
    }
    int main()
    {
        char s[62510];
        int count = 0, n;
        scanf("%d\n%s", &n, s);
        for (int i = 0; i < 30; i++)
        {
            if(!check(s))
            {
                change(n, s);
                count += 1;
            }
            else
            {
                printf("STEP=%d", count);
                return 0;
            }
        }
        printf("Impossible!");
        return 0;
    } 
    
  • 0
    @ 2021-05-31 10:30:06
    /**
     * @author wpx
     * @version V1.0
     * @Package com.algorithm
     * @date 2021/5/28 17:02
     */
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.Scanner;
    
    public class Main {
    
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
            Integer base = Integer.valueOf(sc.nextLine());
            String num = sc.nextLine();
            String tmpAddNum = num;
            int stepNum = 0;
            for(stepNum = 0; stepNum < 30; stepNum++){
                String athorNum = new StringBuilder(tmpAddNum).reverse().toString();
                final String resultR = addFun(tmpAddNum, athorNum, base);
                String result = new StringBuilder(resultR).reverse().toString();
                if(result.equals(resultR)) {
                    break;
                } else {
                    tmpAddNum = result;
                }
            }
            if(stepNum == 30) {
                System.out.println("Impossible!");
            } else {
                System.out.println("STEP=" + (stepNum + 1));
            }
        }
    
        private static String addFun(String one, String two, Integer base) {
            // 逐位相加
            // 取最大的位数进行遍历
            int maxLen = one.length() > two.length() ? one.length() : two.length();
            int oneLastPos = one.length() - 1;
            int twoLastPos = two.length() - 1;
    
            // 从最后一位往前
            StringBuilder result = new StringBuilder();
            boolean hasCarry = false;
            for(int i = 0; i < maxLen; i++) {
                int onePos = oneLastPos - i;
                int twoPos= twoLastPos - i;
    
                int oneChar = one.charAt(onePos);
                int twoChar = two.charAt(twoPos);
                if(oneChar > 57) {
                    oneChar -= 55;
                } else {
                    oneChar -= 48;
                }
    
                if(twoChar > 57) {
                    twoChar -= 55;
                } else {
                    oneChar -= 48;
                }
    
    
                Integer carryValue = hasCarry ? 1 : 0;
                int sumValue = oneChar + twoChar + carryValue;
                Integer appendInValue = sumValue % base;
                String appendValue;
                if(appendInValue > 9) {
                    appendValue = String.valueOf((char) (65 + ( appendInValue - 10)));
                } else {
                    appendValue = String.valueOf(appendInValue);
                }
                result.append(appendValue);
                // 处理进位
                if((sumValue) >= base) {
                    hasCarry = true;
                } else {
                    hasCarry = false;
                }
            }
            if(hasCarry){
                result.append("1");
            }
            return result.toString();
        }
    }
    
    
  • 0
    @ 2020-11-23 21:23:38

    #include<iostream>
    #include<string>
    #include<math.h>
    #define MAX 2000
    #define ll long long
    using namespace std;
    ll N_to_ten(int N,int a[],int n)//把一个n进制数转化为10进制数
    {
    ll sum=0;
    for(int i=0;i<n;i++)
    {
    sum=sum*N+a[i];
    }
    return sum;
    }
    bool hw(int a[],int n)
    {
    for(int i=0;i<n;i++)
    {
    if(a[i]!=a[n-i-1])
    {
    return false;
    }
    }
    return true;
    }
    ll Add(int N,int a[],int n)//实现进制的加减
    {
    int b[n];
    for(int i=0;i<n;i++)
    {
    b[i]=a[n-i-1];
    }
    ll sum=0;
    sum=N_to_ten(N,a,n)+N_to_ten(N,b,n);
    return sum;
    }
    void panduan(int N,int a[],int n)
    {
    if(hw(a,n))
    {
    cout<<"STEP="<<0<<endl;
    }
    else
    {
    int setp=1;
    ll sum=Add(N,a,n);
    int k[MAX];
    do
    {
    int i=0;
    while(sum)
    {
    k[i]=sum%N;
    sum/=N;
    i++;
    }
    if(hw(k,i))
    {
    cout<<"STEP="<<setp<<endl;
    return;
    }
    else
    {
    sum=Add(N,k,i);
    setp++;
    }
    }while(setp<=30);
    }
    cout<<"Impossible!"<<endl;

    }
    int main()
    {
    int N;
    cin>>N;
    string str;
    cin>>str;
    //把这个str类型的数转化为int型
    int length =str.size();
    int a[MAX];//存放改变的字符串
    int a_length=0;
    for(int i=0;i<length;i++)
    {
    if(str[i]<65)
    {
    a[i]=str[i]-'0';//其他进制0-9的转化
    a_length++;
    }
    else
    {
    a[i]=str[i]-55;//16进制ABCD的转化
    a_length++;
    }
    }
    /*for(int i=0;i<a_length;i++)
    {
    cout<<a[i];
    }*/
    //Add(N,a,a_length);
    //cout<<N_to_ten(N,a,a_length);
    //cout<<hw(a,a_length);
    panduan(N,a,a_length);
    return 0;
    }

  • 0
    @ 2020-08-11 14:50:11

    利用string进行判断
    ```cpp
    #include <bits/stdc++.h>
    #define fu(a, b, c) for (int a = b; a <= c; a++)
    #define fd(a, b, c) for (int a = b; a >= c; a--)
    using namespace std;
    int n;
    string s;

    int main() {
    cin >> n >> s;
    for (auto &c : s)
    c=c>=65?c-55:c-48;
    fu(i, 0, 30) {
    string s1 = s;
    reverse(s1.begin(), s1.end());
    if (s1 == s) {
    cout <<"STEP="<<i;
    return 0;
    }
    int sz = s.size() - 1;
    fd(i, sz, 1) {
    s[i]+=s1[i];
    s[i - 1]+=s[i]/n,s[i]%=n;
    }
    s[0] += s1[0];
    if (s[0] >= n)
    s.insert(0,1,char(s[0]/n)),s[1]%=n;
    }
    cout << "Impossible!";
    }
    ```

  • 0
    @ 2020-04-05 23:40:30
    /*
    一开始无论怎样都过不了第二个,后来发现16进制的字母要用大写!!!
    */
    #include <iostream> //[1999提高组-B]回文数
    #include <algorithm>
    #include <string>
    using namespace std;
    
    int H(char ch)
    {
        if (ch >= '0' && ch <= '9')
            return ch - '0';
        else if (ch >= 'A' && ch <= 'Z')
            return ch - 'A' + 10;
    }
    
    string HEX = "0123456789ABCDEF";
    int A[100], B[100], C[102];
    string add(string a, string b, int n)
    {
        string ans;
        int len1 = a.length(), len2 = b.length();
        int len = max(len1, len2), t = 0;
        for (int i = 0; i < len1; i++)
            A[i] = H(a[len1 - 1 - i]);
        for (int i = 0; i < len2; i++)
            B[i] = H(b[len2 - 1 - i]);
    
        for (int i = 0; i < len; i++)
        {
            int k = A[i] + B[i] + t;
            C[i] = k % n;
            t = k / n;
        }
        for (int i = 0; i < len; i++)
            ans += HEX[C[i]];
        if (t > 0)
            ans += '1';
        reverse(ans.begin(), ans.end());
        return ans;
    }
    int step(string num, int n)
    {
        string re = num;
        int ans = 0;
        reverse(re.begin(), re.end());
        while (re != num)
        {
            re = num = add(num, re, n);
            reverse(re.begin(), re.end());
            ans++;
            if (ans == 31)
                break;
        }
        return ans;
    }
    
    int main()
    {
        int n, ans;
        string num;
        cin >> n >> num;
        ans = step(num, n);
        //cout << ans << endl;
        if (ans == 31)
            cout << "Impossible!" << endl;
        else
            cout << "STEP=" << ans << endl;
    
        return 0;
    }
    
    
  • 0
    @ 2019-11-27 20:14:33
    #include<iostream>//wa的两次impossible拼错了,发现以后没有注释掉自己输出的东西又wa一发,也是醉了。 
    #include<cstring>//编译错的两次发现评测机不允许用strrev??? 
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    int to_num(char C){
        if(C >= '0' && C <= '9')
            return C - '0';
        else if(C >= 'A' && C <= 'Z')
            return C - 'A' + 10;
        else if(C >= 'a' && C <= 'z')
            return C - 'a' + 10;
    }
    char N[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    
    int main()
    {
        char A[100000];//正序 
        char B[100000];//反序 
        char C[100001];
        int n;//进制 
        scanf("%d", &n);
        scanf("%s", &A);
        int time = 0;
    
    while(1){
            for(int i = 0 ; i < strlen(A); i++)
                B[i] = A[strlen(A) - 1 - i];
            
            if(time <= 30 && strcmp(A, B) == 0){
                cout<<"STEP="<<time<<endl;
                return 0;
            }
            else if(time > 30){
                cout<<"Impossible!"<<endl;
                return 0;
            }
            int carry = 0;
            for(int i = strlen(A) - 1 ; i >= 0 ; i--){
                C[strlen(A) - 1 - i] = N[(to_num(B[i]) + to_num(A[i]) + carry) % n];
                if(to_num(B[i]) + to_num(A[i]) + carry >= n)
                    carry = 1;
                else
                    carry = 0;
            }
            if(carry == 1)
                C[strlen(A)] = '1';
            strcpy(B, C);
            for(int i = 0 ; i < strlen(C) ; i++)
                A[i] = C[strlen(C) - 1 - i];
                
            time++;
        }
     } 
    
    
  • 0
    @ 2019-08-01 21:17:35

    ##python

    把含有A-F的列表转换为数值列表

    def str_to_int(list_str_x):
    l = len(list_str_x)
    list_int_return = []
    for i in range(0,l):
    if ord(list_str_x[i]) < 58:
    list_int_return.append(ord(list_str_x[i]) - 48)
    else:
    list_int_return.append(ord(list_str_x[i]) - 55)
    return list_int_return

    ##加法 返回两数之和
    def jia_fa(list_int_x):
    l1 = list_int_x
    l2 = list_int_x[::-1]
    len_x = len(list_int_x)
    the_sum = []
    for i in range(0, len_x+2):
    the_sum.append(0)
    for i in range(0, len_x):
    each_plus = int(l1[i]) + int(l2[i]) + int(the_sum[i])
    if each_plus >= n:
    the_sum[i] = each_plus - n
    the_sum[i+1] = 1
    else:
    the_sum[i] = each_plus
    while True:
    if the_sum[-1] == 0:
    del the_sum[-1]
    else:
    break
    return the_sum[::-1]

    ##判断是否回文
    def hui_wen(x):
    y = x[::-1]
    if y == x :
    return True
    else:
    return False

    ##输入及预处理
    n = int(input())
    in_put = input()
    list_str_m = in_put
    list_int_m = str_to_int(list_str_m)

    ##主体程序
    out_put = 'Impossible!'
    for i in range(0,30):
    if hui_wen(list_int_m):
    out_put = 'STEP='+str(i)
    break
    else:
    list_int_m = jia_fa(list_int_m)

    print(out_put)

  • 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
    @ 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;
    }
    
    
  • 0
    @ 2018-03-01 10:51:44

    import java.util.Scanner;

    public class Main {
    public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    String num = scanner.next();
    scanner.close();
    int i = 0;
    while(i<30){
    if(reverse(num).equals(num)){
    System.out.println("STEP="+i);
    return;
    }
    num = add(num, n);
    i ++;
    }
    System.out.println("Impossible!");
    }
    //针对16进制数字和字母的转换
    private static String change(int m){
    switch (m) {
    case 10:
    return "A";
    case 11:
    return "B";
    case 12:
    return "C";
    case 13:
    return "D";
    case 14:
    return "E";
    case 15:
    return "F";
    default:
    return String.valueOf(m);
    }
    }
    //字符串反转
    private static String reverse(String str) {
    if(str == null || str.length() == 0) {
    return str;
    }
    int len = str.length();
    if(len == 1) {
    return str;
    } else {
    return reverse(str.substring(1))+ str.charAt(0);
    }
    }
    //防止溢出,直接转换为string的相加
    private static String add(String num, int n){
    int len = num.length();
    String num_reverse = reverse(num);
    char[] ch1 = num.toCharArray();
    char[] ch2 = num_reverse.toCharArray();
    int jw = 0;
    StringBuilder sb = new StringBuilder();;
    for(int i=len-1;i>=0;i--){
    int add1 = getAdd(ch1[i]);
    int add2 = getAdd(ch2[i]);
    int sum = add1 + add2 + jw;
    jw = sum / n;
    sb.insert(0, change(sum % n));
    }
    if(jw == 1){
    sb.insert(0, "1");
    }
    return sb.toString();
    }
    //针对16进制字母和数字的转换
    private static int getAdd(char ch){
    if(Character.isDigit(ch)){
    return ch - '0';
    }else{
    return ch - 'A' + 10;
    }
    }
    }

  • 0
    @ 2018-02-11 09:28:11
    #include <cstdio>
    #include <cctype>
    #include <vector>
    #include <cstring>
    #include <algorithm>
    
    #define maxn 2333
    
    using namespace std;
    char buf[maxn];
    int n;
    vector<int> a, b;
    
    int main(){
        scanf("%d%s", &n, buf);
        for(int i=0,len=strlen(buf);i<len;i++){ // 简单的按位读入
            if(buf[i]>='0'&&buf[i] <= '9') a.push_back(buf[i]-'0');
            else a.push_back(islower(buf[i])?buf[i]-'a'+ 10:buf[i]-'A'+10);
        }
        b=a; // 将a copy到b里。
        reverse(a.begin(),a.end()); // 反转a。
        if(a==b) return puts("STEP=0"), 0; //反转后相等即为回文
        for(int ans=1;ans<=30;ans++) {
            for(int i=0,siz=a.size();i<siz;i++) {
                a[i]+=b[i]; // 加法
                if(i!=siz-1) a[i+1]+=a[i]/n; // 处理进位
                else if(a[i]>=n) a.push_back(a[i]/n);
                a[i]%=n;
            }
            b=a;
            reverse(b.begin(), b.end());
            if(a==b) return printf("STEP=%d",ans),0; // 判回文
        }
        return puts("Impossible!"), 0;
    }
    
  • 0
    @ 2018-02-05 17:17:37

    #include <iostream>
    #include<string>
    #include<stdlib.h>
    using namespace std;
    int main()
    {
    string str;
    int N;
    int step = 0;
    int v0[1000], v1[1000], v2[1000] = { 0 };
    cin >> N;
    cin >> str;
    int p = str.size();
    if (N == 16)
    {
    for (int i = 0; i < p; i++)
    {
    if (str[i] == 'A' || str[i] == 'B' || str[i] == 'C' || str[i] == 'D' || str[i] == 'E' || str[i] == 'F')
    v0[i] =str[i] - 55;
    else
    v0[i] = str[i] - 48;
    }
    }
    else
    {
    for (int i = 0; i < p; i++)
    {
    v0[i] = str[i] - 48;
    }
    }
    for (int j = p - 1, i = 0; i < p, j >= 0; i++, j--)
    v1[i] = v0[j];
    int pos = 0;
    for (int i = 0; i < p; i++)
    {
    if (v0[i] != v1[i])
    {
    pos = 1;
    break;
    }
    }
    if (pos == 0)
    {
    cout << "step=" << step;
    }
    else
    {
    while (step <= 30)
    {
    pos = 0;
    int ksp = 0;//进位
    int j = 999;
    for (int i = p - 1; i >= 0; i--)
    {
    int o = v0[i] + v1[i] + ksp;
    if (o >= N)
    {
    v2[j] = o - N;
    ksp = 1;
    }
    else
    {
    v2[j] = o;
    ksp = 0;
    }
    j--;
    }
    if (ksp == 1)
    {
    v2[j] = ksp;
    ksp = 0;
    }
    else
    j++;
    int g = 0;
    while (j < 1000)
    {
    v0[g] = v2[j];
    j++;
    g++;
    }
    for (int j = g - 1, i = 0; i <= g, j >= 0; i++, j--)
    v1[i] = v0[j];
    for (int j = 0; j <= g; j++)
    {
    if (v0[j] != v1[j])
    {
    pos = 1;
    break;
    }
    }
    p = g;
    step++;
    if (pos == 0)
    break;
    }
    if (pos == 0)
    cout << "STEP=" << step;
    else
    cout << "Impossible!";
    }
    system("pause");
    return 0;
    }

  • 0
    @ 2018-02-04 19:07:34
    #include<bits/stdc++.h>
    using namespace std;
    int N;
    long long N2dec(string s)//其他进制转10进制
    {
    
        int len = s.size();
        long long num = 0;
        for(int i = 0; i < len; i++)
        {
            num *= N;
            if(isdigit(s[i]))
            {
                num += s[i] - '0';
            }
            else
                num += s[i] - 'A' + 10;
        }
        return num;
    }
    string dec2N(long long num){//10进制转其他进制
        string str = "";
        while(num != 0)
        {
            str += num % N + '0';
            if(str[str.size() - 1] > '9')
                str[str.size() - 1] += 'A'-'0' - 10 ;
            num /= N;
        }
        reverse(str.begin(), str.end());
        return str;
    }
    bool judge(string s){
        int len = s.size();
        for(int i = 0; i < s.size(); i++){
            if(s[i] != s[len-i-1]) return 0;
        }
        return 1;
    }
    int main()
    {
        while(cin >> N){
            string input;
            cin >> input;
    
            string str;
            int ok = 0, times;
            for(times = 0; times <= 30; times++)
            {
                if(judge(input))
                {
                    ok = 1;
                    break;
                }
                long long a = N2dec(input);
                reverse(input.begin(), input.end());
                long long b = N2dec(input);
                input = dec2N(a + b);
    
            }
            if(!ok)
                printf("Impossible!\n");
            else
                printf("STEP=%d\n", times);
    
        }
    }
    
    
  • 0
    @ 2017-11-30 20:25:19
    /*Tokgo*/
    /*
    简单字符处理
    用高精度加法
    不必为进制发愁
    */
    #include <iostream>
    #include <vector>
    #include <cstring>
    using namespace std;
    vector <int> a;
    string in;
    int n;
    int ans;
    void change(){
        for(int i=in.size()-1;i>-1;--i){
            if(in[i]>='0'&&in[i]<='9')
                a.push_back(in[i]-'0');
            else
                a.push_back(in[i]-'A'+10);
        }
    }
    void jia(){
        for(int i=0;i<(a.size()>>1);++i){
            a[i]=a[a.size()-1-i]=a[i]+a[a.size()-1-i];
        }
        if((a.size()&1)==1)
            a[a.size()/2]*=2;
        for(int i=0;i<a.size();++i){
            if(a[i]>(n-1)){
                if(i!=a.size()-1)
                    a[i+1]+=(a[i]/n);
                else
                    a.push_back(a[i]/n);
                a[i]%=n;
            }
        }
    }
    bool re(){
        bool jud=1;
        for(int i=0;i<=(a.size()-1)/2;++i){
            if(a[i]!=a[a.size()-1-i]){
                jud=0;
                break;
            }
        }
        return jud;
    }
    int main(){
        cin>>n;
        cin>>in;
        change();
        for(int i=0;i<=31;++i){
            if(ans=re()){
                ans=i;
                break;
            }
            jia();
        }
        if(ans)
            cout<<"STEP="<<ans<<endl;
        else
            cout<<"Impossible!"<<endl;
        return 0;
    }
    

信息

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