题解

211 条题解

  • 0
    @ 2017-11-25 21:53:52
    //效率一般的做法,第一遍居然把A写成了11.。。
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <string>
    
    using namespace std;
    
    string read;
    int b;
    
    struct num {
        int base, number[100], temp[100], size;
        inline void init(int _base, string _numstr) {
            base = _base;
            memset(number, 0, sizeof number);
            memset(temp, 0, sizeof temp);
            size = _numstr.size();
            for (int i = size - 1; i >= 0; i--) {
                if (_numstr[i] >= 'A' && _numstr[i] <= 'F') number[size - i] = _numstr[i] - 'A' + 10;
                else number[size - i] = _numstr[i] - '0';
            }
        }
    
        inline void add() {
            for (int i = 0; i <= size; i++) temp[i] = number[i];
            for (int i = 1; i <= size; i++) number[i] += temp[size - i + 1];
            for (int i = 2; i <= size + 1; i++) number[i] += number[i - 1] / base, number[i - 1] %= base;
            if (number[size + 1] != 0) size++;
        }
    
        inline bool verify() {
            for (int i = 1; i <= size / 2; i++) {
                if (number[i] != number[size - i + 1]) return false;
            }
            return true;
        }
    
        inline void debug() {
            cout << "size = " << size << endl << "number:: ";
            for (int i = size; i >= 1; i--) cout << number[i] << " ";
            cout << endl;
        }
    } n;
    
    int main() {
        cin.sync_with_stdio(false), cout.sync_with_stdio(false);
        cin >> b >> read;
        n.init(b, read);
        for (int i = 0; i <= 30; i++) {
    //      n.debug();
            if (n.verify()) {
                cout <<  "STEP=" << i << endl; 
    //          system("pause");
                return 0;
            }
            n.add();
        }
        cout << "Impossible!" << endl;
    //  system("pause");
        return 0;
    }
    
  • 0
    @ 2017-10-20 22:41:48

    #include<iostream>
    #include<string>
    using namespace std;
    string str;
    int step = 1;
    int n;
    int m;
    int a[10001];
    int b[10001];
    void doit() {
    int JinWei = 0;
    for (int i = 0; i < m; i++)
    {
    a[i] += b[i] + JinWei;
    JinWei = a[i] / n;
    a[i] %= n;
    }
    if (JinWei > 0)
    a[m++] = JinWei;
    for (int i = 0; i < m; i++)
    b[i] = a[m - i - 1];
    step++;
    }
    bool judge()
    {
    int flag = true;
    for (int k = 0; k < m; k++)
    {
    if (a[k] != b[k])
    flag = false;
    }
    return flag;
    }

    int main() {
    cin >> n >> str;
    m = str.length();
    for (int i = 0; i < m; i++)
    {
    if (str[i] >= 65)
    {
    a[i] = str[i] - 55;
    }
    else
    {
    a[i] = str[i] - '0';
    }
    }
    for (int i = 0; i < m; i++)
    {
    b[i] = a[m - i - 1];
    }
    while (step <= 30)
    {
    if (judge())
    {
    cout << "STEP=" << step - 1;
    system("pause");
    return 0;
    }
    doit();
    }
    cout << "Impossible!";
    system("pause");
    return 0;
    }

  • 0
    @ 2017-10-04 21:12:25

    重点还是字母处理和递归运算。
    #include <iostream>
    using namespace std;

    int n;
    char m;
    int arr[10005];
    int brr[10005];
    int crr[10];
    bool truefalse = false;

    int roundnumber(int i) {//回文判断
    int j = 0;
    int k = i - 1;
    while (j < k) {
    if (arr[j] == arr[k]) {
    j++;
    k--;
    } else {
    return 0;
    }
    }
    return 1;
    }

    void identify(int i, int number) {
    if (roundnumber(i) == 1) {
    cout << "STEP=" << number;
    truefalse = true;
    } else {
    if (number <= 30) {//递归边界
    for (int j = i - 1; j >= 0; j--) {
    brr[j] = arr[i - j - 1];
    }
    for (int j = 0; j < i; j++) {
    arr[j] += brr[j];
    if (arr[j] >= n) {
    arr[j] -= n;
    arr[j + 1]++;
    }
    }
    if (arr[i] != 0) {
    i++;
    }
    identify(i, number + 1);
    }
    }

    }

    int main() {
    cin >> n;
    m=getchar();//考虑\n
    m=getchar();
    int i = 0;
    while(m!='\n'){
    if((m>='0')&&(m<='9')){
    crr[i]=m-'0';
    i++;
    }else{
    crr[i]=m-'A'+10;
    i++;
    }
    m=getchar();
    }
    for(int j=0;j<=i-1;j++){
    arr[j]=crr[i-1-j];
    }
    identify(i, 0);
    if (truefalse == false) {
    cout << "Impossible!";
    }
    return 0;
    }

  • 0
    @ 2017-09-28 17:59:24
    Var
            n,i,step,l:longint;
            a:array[1..100]of integer;
            s:string;
            f:array['A'..'F'] of integer=((10),(11),(12),(13),(14),(15));
    
    Function pd:boolean;
    Var
            left,right:longint;
    Begin
            left:=1; right:=l;
            while left<right do
                    if a[left]=a[right] then
                    begin
                            inc(left);
                            dec(right);
                    end
                    else
                            break;
            if ((left>right) and (l mod 2=0)) or ((l mod 2<>0) and (left=right)) then
                    exit(true);
            exit(false);
    End;
    
    Procedure gjj;
    Var
            b:array[1..100]of integer;
            i:longint;
    Begin
            fillchar(b,sizeof(b),0);
            for i:=l downto 1 do
                    b[i]:=a[l-i+1];
            for i:=1 to l do
            begin
                    a[i]:=a[i]+b[i];
                    a[i+1]:=a[i] div n+a[i+1];
                    a[i]:=a[i] mod n;
            end;
            l:=100;
            while a[l]=0 do dec(l);
    End;
    
    Begin
            readln(n);
            readln(s);
            l:=length(s);
            for i:=1 to length(s) do
                    if s[i] in ['0'..'9'] then
                            a[l-i+1]:=ord(s[i])-48
                    else
                            if s[i] in ['A'..'Z'] then
                                    a[l-i+1]:=f[s[i]];
    
            step:=0;
            while step<=30 do
            begin
                    if pd then
                            break;
                    gjj;
                    inc(step);
            end;
            if step>30 then
                    writeln('Impossible!')
            else
                    writeln('STEP=',step);
            readln;
    End.
    
    
    
  • 0
    @ 2017-09-20 09:50:58

    模拟

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int maxn = 200;
    
    int a[maxn], b[maxn];
    char ch[maxn];
    int lena;
    bool chk(){
        for ( int i=1; i<=lena/2+1; i++ )
            if( a[i]!=a[lena-i+1] ) return false;
        return true;
    }
    void add(int n){
        for ( int i=1; i<=lena; i++ ){
            a[i+1]+=(a[i]+b[i])/n;
            a[i]=(a[i]+b[i])%n;
        }
        while( a[lena+1]>0 ){
            lena++;
            if( a[lena]>n ){
                a[lena+1]=a[lena]/n;
                a[lena]=a[lena]%n;
            }
        }
        for ( int i=1; i<=lena; i++ )
            b[lena-i+1]=a[i];
    }
    
    int main(){
        int n, p;
        scanf("%d", &n );
        scanf("%s", ch+1 );
        lena=strlen(ch+1);
        for ( int i=1; i<=lena; i++ ){
            if( isdigit(ch[i]) ) a[i]=ch[i]-'0';
            else a[i]=ch[i]-'A'+10;
        }
        for ( int i=1; i<=lena; i++ )
            b[lena-i+1]=a[i];
        for ( int i=0; i<=30; i++ ){
          //  for ( int i=1; i<=lena; i++ ) printf("%d ", a[i] );
            // puts("");
            if( chk() ){
                printf("STEP=%d", i);
                return 0;
            }
            add(n);
        }
        printf("Impossible!");
        return 0;
    }
    
  • 0
    @ 2017-01-20 11:29:26

    正解

    #include<iostream>
    #include<cstdio>
    #include<string>
    using namespace std;
    //
    int a[1001];
    int a2[1001];
    int n;
    int j;
    //
    bool judge()
    {
    for(int i=1000;i>=0;i--)
    {
    if(a[i]!=0)
    {
    j=i;
    break;
    }
    }
    int l=j;
    for(int k=0;k<=j;k++)
    {
    if(a[k]!=a[l])return 0;
    l--;
    }
    return 1;
    }
    //
    void add1()
    {
    int u=j;
    for(int i=0;i<=j;i++)
    {
    a2[u]=a[i];
    u--;
    }
    for(int i=0;i<=j;i++)
    {
    a[i]+=a2[i];
    }
    for(int i=0;i<=j;i++)
    {
    a[i+1]+=a[i]/n;
    a[i]%=n;
    }
    }

    //
    void step1()
    {
    int step2=0;
    while(step2<31)
    {
    if(judge()){printf("STEP=%d",step2);break;}
    add1();
    step2++;
    }
    if(step2>30)printf("Impossible!");
    }
    //
    int main()
    {
    string s;
    for(int i=0;i<1001;i++)
    a[i]=0;
    scanf("%d",&n);
    cin>>s;
    int k=0;
    for(int i=s.size()-1;i>=0;i--)
    {
    if(s[i]>='A'){a[k]=s[i]-55;}else{a[k]=s[i]-48;}
    k++;
    }
    step1();
    }
    //自行理解 不多说

  • 0
    @ 2017-01-20 09:41:01

    #include<iostream>
    #include<cstdio>
    #include<string>
    using namespace std;
    //
    int a[1001];
    int a2[1001];
    int n;
    int j;
    //
    bool judge()
    {
    for(int i=1000;i>=0;i--)
    {
    if(a[i]!=0)
    {
    j=i;
    break;
    }
    }
    int l=j;
    for(int k=0;k<=j;k++)
    {
    if(a[k]!=a[l])return 0;
    l--;
    }
    return 1;
    }
    //
    void add1()
    {
    int u=j;
    for(int i=0;i<=j;i++)
    {
    a2[u]=a[i];
    u--;
    }
    for(int i=0;i<=j;i++)
    {
    a[i]+=a2[i];
    }
    for(int i=0;i<=j;i++)
    {
    a[i+1]+=a[i]/n;
    a[i]%=n;
    }
    }

    //
    void step1()
    {
    int step2=0;
    while(step2<31)
    {
    if(judge()){printf("STEP=%d",step2);break;}
    add1();
    step2++;
    }
    if(step2>30)printf("Impossible!");
    }
    //
    int main()
    {
    string s;
    for(int i=0;i<1001;i++)
    a[i]=0;
    scanf("%d",&n);
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
    if(s[i]>='A'){a[i]=s[i]-55;}else{a[i]=s[i]-48;}
    }
    step1();
    }
    //有错但能ac;不多解释

  • 0
    @ 2016-10-22 10:53:42

    我的天,Impossible没加!白白错了三次

  • 0
    @ 2016-09-19 11:59:19

    #include<iostream>
    using namespace std;
    int a[101],i;
    bool judge(int a[])
    {
    for(i=1;i<=a[0];i++)
    if(a[i]!=a[a[0]-i+1]) return false;
    return true;
    }
    int main()
    {
    int b[101],c=0,n;
    string s;
    cin>>n>>s;
    a[0]=s.size();

    for(i=1;i<=a[0];i++) a[i]=(s[a[0]-i]>'/'&&s[a[0]-i]<':'?s[a[0]-i]-'0':s[a[0]-i]-'A'+10);
    if(judge(a)){cout<<"STEP=0";return 0;} //如果为回文数,输出结果
    while(c<31)
    {
    c++;
    for(i=1;i<=a[0];i++)
    b[i]=a[a[0]-i+1];
    for(i=1;i<=a[0];i++)
    a[i]+=b[i];
    for(i=1;i<=a[0];i++)
    {
    a[i+1]+=a[i]/n;
    a[i]%=n;
    }
    if(a[a[0]+1])
    a[0]++;
    if(judge(a)){cout<<"STEP="<<c;return 0;} //duang,同上
    }

    }

  • 0
    @ 2016-08-25 23:24:10

    //没有验证16进制是否正确,没有进行数据测试,应该没问题! 呵呵!。
    #include <iostream>
    #include <vector>
    #include <string>

    using namespace std;

    vector <int> numb;
    int n;
    string num;

    void addition()//加一次
    {
    vector <int> ans;
    int i=0,len=numb.size();
    for(i=0;i<len;++i)//反转数
    {
    ans.push_back( numb[len-1-i] );
    }
    for(i=0;i<len;++i)//加和
    {
    ans[i]+=numb[i];
    if(i+1>=ans.size()&&(ans[i]/n))ans.push_back(0),numb.push_back(0);
    ans[i+1]+=(ans[i]/n);
    ans[i]%=n;
    }
    len=ans.size();
    for(i=0;i<len;++i)//将结果输出到原始数组,便于下次循环
    numb[i]=ans[len-1-i];
    return;
    }

    int fdd()//判断是否为回文数
    {
    int i=numb.size()-1,j=numb.size()-1;
    while(i>j/2)
    {
    if(numb[i]!=numb[j-i])return 1;//不是回文数返回1
    i--;
    }
    return 0;
    }

    int main()
    {
    cin>>n>>num;
    int i=0,j=0,p=0;
    for(i=0;i<num.length();++i)
    {
    if((int)num[i]>=65)numb.push_back((int)num[i]-55);//16进制转数字
    else numb.push_back((int)num[i]-'0');
    }
    do
    {
    p++;
    addition();
    j=fdd();
    }while(j&&p<31);//是否出现回文数,是否超出30
    if(p>30)cout<<"Impossible!";
    else cout<<"STEP="<<p;
    return 0;
    }

  • 0
    @ 2016-08-09 12:08:05

    挖2楼神做法:
    s=‘123456789ABCDEFG’
    a[m]=pos(ch,s)

  • 0
    @ 2016-07-30 20:44:38

    当做高精做就行了...注意每次加完mod一下
    ```
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    using namespace std;
    const int MAXN = 10001;
    int md;
    string k;
    int a[MAXN];
    int len=0;
    void process(){
    for(int i=0;i<k.length();i++){
    if(k[i]<'A'){
    a[i+1]=k[i]-'0';
    }else{
    a[i+1]=k[i]-'A'+10;
    }
    }
    }

    void init(){
    memset(a,0,sizeof(a));
    cin>>md;
    cin>>k;
    len=k.length();
    process();
    }

    bool judge(){
    for(int i=1;i<=len/2;i++){
    if(a[i]!=a[len-i+1]) return false;
    }
    return true;
    }

    void ps(){
    int sum[MAXN];
    memset(sum,0,sizeof(sum));
    int i;
    for(i=1;i<=len;i++){
    sum[i]=a[i]+a[len+1-i]+sum[i];
    sum[i+1]+=sum[i]/md;
    sum[i]%=md;
    }
    if(sum[i]!=0){
    int j=1;
    len=i;
    while(i>0){
    a[j++]=sum[i--];
    }
    }else{
    i-=1;
    len=i;
    int j=1;
    while(i>0){
    a[j++]=sum[i--];
    }
    }
    }

    void solve(){
    int s=0;
    while(s<=30){
    ps();
    if(judge()){
    cout<<"STEP="<<s+1;
    exit(0);
    }
    s++;
    }
    cout<<"Impossible!";
    exit(0);
    }

    int main(){
    init();
    solve();
    return 0;
    }

  • 0
    @ 2016-07-13 21:12:31

    提示
    高精度压四位是10000进制,将高精度改一下即可
    16进制只需改变读入方式 转换A-F为数字
    代码
    #include <cstdio>
    #include <cstring>

    int k;

    int judge(int a[]){
    int len=a[0];
    int flag=1;
    for(int i=1;i<=len;i++)
    flag*=a[i]==a[len-i+1];
    return flag;
    }

    int read(int a[]){
    char c[10000];
    scanf("%s",c);
    int n=1,len=strlen(c);
    for(int i=0;i<len;i++){
    a[n]=c[len-i-1]-'0';
    n++;
    }
    a[0]=n-1;
    }

    int read16(int a[]){
    char c[10000];
    scanf("%s",c);
    for(int i=0;i<strlen(c);i++)
    if(c[i]>='A'&&c[i]<='F')
    c[i]=c[i]-'A'+10+'0';
    int n=1,len=strlen(c);
    for(int i=0;i<len;i++){
    a[n]=c[len-i-1]-'0';
    n++;
    }
    a[0]=n-1;

    }

    int plusx(int a[],int b[],int c[]){
    int len=a[0]>b[0]?a[0]:b[0];
    for(int i=1;i<=len;i++){
    c[i]+=a[i]+b[i];
    c[i+1]+=c[i]/k;
    c[i]%=k;
    }

    len++;
    while(c[len]==0&&len>1)
    len--;
    c[0]=len;
    }

    int main(){
    // freopen("in.txt","r",stdin);
    int a[10000]={0};
    int pa[10000]={0};
    scanf("%d",&k);
    if(k==16)
    read16(a);
    else
    read(a);

    for(int i=1;i<=30;i++){
    int c[10000]={0};
    pa[0]=a[0];
    for(int z=1;z<=a[0];z++)
    pa[z]=a[a[0]-z+1];
    plusx(a,pa,c);

    memcpy(a,c,sizeof(a));
    if(judge(a)){
    printf("STEP=%d",i);
    return 0;
    }
    }
    printf("Impossible!");
    return 0;
    }

  • 0
    @ 2016-07-12 14:59:00
    #include<iostream>
    #include<cstdio>
    #include<cctype>
    using namespace std;
    
    const int map[] = { 10, 11, 12, 13, 14, 15, 16};
    const int maxn = 100 + 5;
    int n;
    int ans;
    int m[maxn];
    
    void self_plus (int* a) {
        int b[maxn] = {0};
        int l = 0;
        int k = 0;
        for (int i = 1; i <= a[0]; i++) {
            b[i] = a[i] + a[a[0]-i+1] + k;
            k = b[i] / n;
            b[i] %= n;
        }
        b[a[0]+1] = k;
        b[0] = a[0] + k;
        for (int i = 0; i <= b[0]; i++) a[i] = b[i];
    }
    
    bool is_return (int* x) {
        for (int i = 1; i <= x[0]; i++) if (x[i] != x[x[0]-i+1]) return false;
        return true;
    }
    
    void solve (void) {
        while (!is_return(m)) {
            if (ans > 30) break;
            ans++;
            self_plus(m);
        }
        if (ans <= 30) cout << "STEP=" << ans << "\n";
        else cout << "Impossible!" << "\n";
    }
    
    int main () {
    //  freopen ("in.txt", "r", stdin);
        cin >> n;
        char x;
        int l = 0;
        while (cin >> x) {
            if (isdigit(x)) m[++l] = x - '0';
            else if (isalpha(x)) m[++l] = map[x-'A'];
        }
        m[0] = l;
        solve();
        return 0;
    }
    
  • 0
    @ 2016-07-10 23:15:23

    无需进制转换,直接模拟加法即可。
    交了三次我勒个去,前两次没看懂输出要求,在impossible前加了step=......汗......
    #include<iostream>
    #include<algorithm>
    #include<string>
    using namespace std;

    bool istarget(string s){
    for(int i = 0;i < s.length() / 2 + 1;i++){
    if(s[i] != s[s.length() - i - 1]) return false;
    }
    return true;
    }

    int chartoint(char ch){
    if(isalpha(ch)){
    return ch-'A'+10;
    }
    else{
    return ch-'0';
    }
    }

    char inttochar(int nb){
    if(nb<10) return '0'+nb;
    else return 'A'+nb-10;
    }

    int main(){
    int n;
    string s1,s2;
    cin>>n>>s1;
    int step = 0;
    while(step <= 30){
    if(istarget(s1)) break;
    step++;
    s2=s1;
    reverse(s2.begin(),s2.end());
    int add=0;
    for(int i=0;i<s1.length();i++){
    int a = chartoint(s1[i]),b = chartoint(s2[i]);
    int temp=a+b+add;
    add=temp/n;
    temp = temp % n;
    s1[i]=inttochar(temp);
    }
    if(add)s1.insert(s1.length(),1,add+'0');
    //cout<<s1<<" *\n";
    }
    if(step <= 30)cout<<"STEP="<<step<<endl;
    else cout<<"Impossible!"<<endl;
    return 0;
    }

  • 0
    @ 2016-07-10 20:39:36

    自行理解2333
    ```c++
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    using namespace std;

    int n,l,cnt = 30;
    char a[35] = {};
    int b[35] = {};
    int c[35] = {};

    bool check()
    {
    for(int i = 1;i <= l/2; i++)
    if(b[i] != b[l-i+1])
    return false;
    return true;
    }

    void reverse()
    {
    memset(c, 0, sizeof(c));
    for(int i = 1;i <= l; i++)
    c[l-i+1] = b[i];
    }

    bool solve()
    {
    if(check()) return true;
    reverse();
    for(int i = 1;i <= l; i++)
    b[i] = b[i] + c[i];
    for(int i = 1;i <= l; i++)
    while(b[i] >= n)
    {
    b[i] = b[i] - n;
    b[i+1]++;
    }
    if(b[l+1] != 0) l++;
    return false;
    }

    int main()
    {
    scanf("%d",&n);
    scanf("%s",a);
    l = strlen(a);
    for(int i = 1;i <= l; i++)
    {
    if(a[i-1] == 'A') b[i] = 10;
    else if(a[i-1] == 'B') b[i] = 11;
    else if(a[i-1] == 'C') b[i] = 12;
    else if(a[i-1] == 'D') b[i] = 13;
    else if(a[i-1] == 'E') b[i] = 14;
    else if(a[i-1] == 'F') b[i] = 15;
    else b[i] = a[i-1] - '0';
    }
    while(cnt--)
    {
    if(solve())
    {
    printf("STEP=%d\n",29-cnt);
    break;
    }
    }
    if(cnt == -1) printf("Impossible!\n");
    return 0;
    }
    ```

  • 0
    @ 2016-06-26 21:42:34
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    char s[1001];
    int a[1001]={0},b[1001],len=0;
    int step=0,base;
    bool pal()//判断回数
    {
        int p=1,k=1;
        for (int i=0;i<=(len+1)/2;i++)
            if (a[i] != a[len-1-i]) 
            p = 0;
        return p;
    }
    void add()//相加
    {
        int i,j,s,l1=len;
        for (i=0;i<len;i++)
            a[i]=a[i]+b[i];
        for (i=0;i<l1;i++)
        {
            if (a[i]>=base)
            {
                a[i]-=base;
                a[i+1]++;
                if (a[l1]!=0)
                len++;
            }
        }
        for (i=0;i<len;i++)
            b[len-i-1]=a[i];
    }
    int main()
    {
        int j=0;
        cin>>base;
        cin>>s;
        for (int i=strlen(s)-1;i>=0;i--)
        {
            if ( s[i]<=57)
            a[j++]=s[i]-'0',len++;
            else a[j++]=s[i]-'A'+10,len++;
        }//转化
        for (int i=len-1;i>=0;i--)
           b[len-i-1]=a[i];
        while(!pal() && step<=30)
        {
            add();
            step++;  
        }
        if (step<=30)
        printf("STEP=%d\n",step);
        else 
        printf("Impossible!\n");
    }```
    好麻烦啊!!(⊙o⊙)
    
  • 0
    @ 2016-06-26 21:39:41

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    char s[1001];
    int a[1001]={0},b[1001],len=0;
    int step=0,base;
    bool pal()
    {
    //cout<<len<<" ";
    int p=1,k=1;
    for (int i=0;i<=(len+1)/2;i++)
    if (a[i] != a[len-1-i])
    p = 0;
    //cout<<p<<" ";
    return p;
    }
    void add()
    {
    int i,j,s,l1=len;
    for (i=0;i<len;i++)
    a[i]=a[i]+b[i];
    for (i=0;i<l1;i++)
    {
    if (a[i]>=base)
    {
    a[i]-=base;
    a[i+1]++;
    if (a[l1]!=0)
    len++;
    }
    }
    for (i=0;i<len;i++)
    b[len-i-1]=a[i];
    }
    int main()
    {
    int j=0;
    cin>>base;
    cin>>s;
    for (int i=strlen(s)-1;i>=0;i--)
    {
    if ( s[i]<=57)
    a[j++]=s[i]-'0',len++;
    else a[j++]=s[i]-'A'+10,len++;
    }
    /*cout<<"num(a):"<<endl;
    for (int i=len-1;i>=0;i--)
    cout<<a[i]<<" ";
    cout<<endl;*/
    for (int i=len-1;i>=0;i--)
    b[len-i-1]=a[i];
    while(!pal() && step<=30)
    {
    add();
    step++;

    }
    if (step<=30)
    printf("STEP=%d\n",step);
    else
    printf("Impossible!\n");
    }

  • 0
    @ 2016-05-28 15:04:44

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <vector>
    using namespace std;
    const int MAXSTEP=30;//×î´ó²½Êý

    string stringVector;

    bool isOK(string str);

    int getStep(int N,string M,int step);

    string changeNum(string M);///用来转化十六进制高位

    char str[] = "ABCDEF";

    int main()
    {
    int N;
    string M;
    cin>>N;
    getchar();
    cin>>M;
    if(N==16)M=changeNum(M);
    int resultStep=getStep(N,M,0);
    if(resultStep<=MAXSTEP)
    {
    cout<<"STEP="<<resultStep<<endl;
    }
    else
    {
    cout<<"Impossible!"<<endl;
    }
    return 0;
    }

    string changeNum(string M)
    {
    for(int i=0;i<M.size();i++)
    {
    M[i]=(M[i]>='A'&&M[i]<='F')?M[i]-'A'+'10':M[i];
    }
    return M;
    }

    int getStep(int N,string M,int step)///得到程序步数
    {
    int temp=0;///暂时存放进位
    stringVector.erase(stringVector.begin(),stringVector.end());
    while(step++<30)
    {
    for(int i=0;i<M.size();i++)
    {
    int a=M[i]-'0',b=M[M.size()-i-1]-'0';
    int result=(a+b+temp)%N;
    stringVector.push_back(result+'0');
    temp=(a+b+temp)/N;
    }
    if(temp!=0)
    {
    stringVector.push_back(temp+'0');
    temp=0;
    }
    M.erase(M.begin(),M.end());
    M=stringVector;
    stringVector.erase(stringVector.begin(),stringVector.end());
    if(isOK(M))
    {
    for(int i=0;i<M.size();i++)
    {
    cout<<M[i]<<" ";
    }
    cout<<endl;
    return step;
    }

    }
    return step;
    }

    bool isOK(string str)///检验是否为回文数
    {
    for(int i=0;i<str.size()/2;i++)
    {
    if(str[i]!=str[str.size()-i-1])
    {
    return false;
    }
    }
    return true;
    }

    第二个样例错了。求样例测试的数据,多谢啦.

  • 0
    @ 2016-05-12 22:19:15

    模拟大数相加

    include <stdio.h>

    include <string.h>

    int mult_add(char * a, int n, int m)
    {
    int i, k, d;
    char s[100], b[100];
    for (i=0; i<n; i++)
    b[i] = a[n-i-1];
    for (i=0; i<n; i++) {
    s[i] = a[i] + b[i];
    }
    for (i=0, k=0; i<n; i++) {
    d = (s[i]+k) / m;
    a[i] = s[i] = (s[i]+k)%m;
    k = d;
    }
    if (k > 0) {
    a[n] = k;
    return n+1;
    }
    else {
    return n;
    }
    }

    int check(char * a, int n)
    {
    int i;
    for (i=0; i<=n/2-1; i++) {
    if (a[i] != a[n-1-i])
    return 0;
    }
    return 1;
    }

    int main (void)
    {
    int n, i, flag=0, len;
    char a[100];
    scanf("%d",&n);
    scanf("%s",a);
    len = strlen(a);
    for (i=0; i<len; i++)
    a[i] = (a[i]>='A' && a[i]<='F') ? a[i]-'A'+10 : a[i]-'0';
    for (i=0; i<=30; i++) {
    if (check(a, len)) {
    flag = 1;
    break;
    }
    else {
    len = mult_add(a,len,n);
    }
    }
    if (flag == 1)
    printf("STEP=%d\n",i);
    else
    puts("Impossible!");
    return 0;
    }

信息

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