1075 數制轉換

來源:互聯網
上載者:User
 
描述

設有一個字串A$的結構為: A$=’m(n)p’ 其中m為數字串(長度<=20),而n,p均為1或2位的數字串(其中所表達的內容在2-10之間)。 程式要求:從鍵盤上讀入A$後(不用正確性檢查),將A$中的數字串m(n進位),以p進位的形式輸出。 例如:A$=’48(10)8’ 其意義為:將10進位數48,轉換成8進位數輸出。 輸出結果為:48(10)=60(8)
輸入

包含多組測試資料 一個字串A,中間以一個(n)隔開,(n)之前是一個整數m,n表示n進位,(n)之後是一個整數p,表示要將n轉換成p進位 每個字串佔一行
輸出

每個輸出佔一行 按以下格式輸出 m(n)=q(p) 表示n進位的m和p進位的q相等
範例輸入48(10)8範例輸出

48(10)=60(8)

解題思路:
先把每一位抽取出來,然後按照進位進行還原,反正最後都是按照十進位來做。接下來再按照新的進紙進行位抽取即可。

 

#include <stdio.h>#include <math.h>main(){long long  m;    int n,p;long  long result;int  a[200];int up;int i;long long  t;    long long  xx; while (scanf("%lld(%d)%d",&m,&n,&p)!=EOF) { t=m;    up=0;  //  printf("%d\n",m);   while(m!=0)   {   a[up]=m%10;   up++;   m=m/10;     //  printf("%d\n",a[up-1]);   }   result=0;   xx = 1;   for(i=0;i<up;i++)   {     result=result+xx*a[i];     xx =xx * n;   }  //   printf("%lld\n",result);  //  printf("%d\n",p);   up=0;   while(result!=0)   {   a[up]=result%p;   up++;   result=result/p;   }   result=0;   xx = 1;   for(i=0;i<up;i++)   {   result=result + xx*a[i];   xx = xx * 10;    }   printf("%lld(%d)=%lld(%d)\n",t,n,result,p); }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.