UVA 575

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=516

本题在于进制之间的转换,十进制换其他进制就除k取余法,反之则加余乘k

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 void zhuanhua(char *s,int *a)//把字符转化为数组存入数组
 5 {
 6  int i=0;
 7  while(*s)
 8      {
 9       a[i]=(*s)-48;
10       i++;
11       s++;
12      }
13 }
14 int main()
15 {
16  int i,j,len,a[100],sum;
17  char str[100];
18  while(scanf("%s",str))
19      {
20       sum=0;
21       if(strlen(str)==1&&str[0]=='0')
22           break;//当只输入0时退出
23       len=strlen(str);
24       zhuanhua(str,a);
25       for(i=0;i<len;i++)
26           sum+=a[i]*(pow(2,len-i)-1);//模拟加k乘余的方法
27       printf("%d\n",sum);
28      }
29      return 0;
30 }

 

posted @ 2013-02-18 15:56  执着追求的IT小小鸟  阅读(89)  评论(0编辑  收藏  举报