poj百练1565

原题大意:

如果一个数是十进制值,那么可以表示为10120(10)=1*10^4+0*10^3+……+0*10^0;

如果是二进制的,那么可以表示为10120(2)=1*2^4+……+0*2^0;

现在定义斜二进,那么可以表示为10120(skew) = 1 * (2^5-1) + 0 * (2^4-1) + 1 * (2^3-1) + 2 * (2^2-1) + 0 * (2^1-1)
= 31 + 0 + 7 + 6 + 0
= 44.
输入斜二进制数,遇0结束。

输出十进制。

代码:

   #include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
  char b[50];
  char *s="0"; 
  int i;
  while(scanf("%s",b),strcmp(b,s))
  {
     int n=strlen(b),c=n;
  double d=0;
  for(i=0;i<n;i++)
   d+=(b[i]-'0')*(pow(2,c--)-1);
  printf("%.0f\n",(d));
  }
}

代码中运用都逗号表达式,简化了遇0 结束时的代码。

但是注意这道题目如果你想用int的%d方式输出,结果会出错。

为什么?,很简单。因为int如果未强调其是否有符号。正数最大值只有为(2^32-1)/2;

所以这样就会在第三数据出错,那么用无符号的unsigned就行了,确实用这样就行le

代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
  char b[50];
  char *s="0"; 
  int i;
  while(scanf("%s",b),strcmp(b,s))
  {
     int n=strlen(b),c=n;
  double d=0;
  for(i=0;i<n;i++)
   d+=(b[i]-'0')*(pow(2,c--)-1);
  printf("%d\n",unsign int(d));
  }
}

但是如果这样的话,poj就报你错,它并不识别unsigned int;

也就是他的系统不是vc,估计gcc下只能用第一个。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值