二进制和十进制【难度:0级】:
答案1:
using System
;
namespace Solution
{
public static class Program
{
public static int binToDec(string s
)
{
return Convert
.ToInt32(s
, 2);
}
}
}
答案2:
using System
;
namespace Solution
{
public static class Program
{
public static int binToDec(string s
)
{
return Convert
.ToInt32(s
, 2);;
}
}
}
答案3:
using System
;
namespace Solution
{
public static class Program
{
public static int binToDec(string s
)
{
return Convert
.ToInt32((s
), 2);
}
}
}
答案4:
namespace Solution
{
using System
;
public static class Program {
public static int binToDec( string s
) {
return Convert
.ToInt32( s
, 2 );
}
}
}
答案5:
using System
;
namespace Solution
{
public static class Program
{
public static int binToDec(string s
)
{
return (Convert
.ToInt32(s
, 2));
}
}
}
答案6:
using System
;
namespace Solution
{
public static class Program
{
public static int binToDec(string s
) => Convert
.ToInt32(s
, 2);
}
}
答案7:
using System
;
namespace Solution
{
public static class Program
{
public static int binToDec(string s
) => (Convert
.ToInt32(s
, 2));
}
}
答案8:
using System
;
namespace Solution
{
public static class Program
{
public static int binToDec(string s
)
{
byte RADIX
= 2;
return Convert
.ToInt32(s
, RADIX
);
}
}
}
答案9:
using System
;
namespace Solution
{
public static class Program
{
public static int binToDec(string s
)
{
uint res
= 0;
var pos
= 0;
for(var i
=0; i
<s
.Length
; i
++)
{
uint value = 0;
switch(s
[i
])
{
case '0': value = 0; break;
case '1': value = 1; break;
default: throw new Exception("334");
}
res
= res
| (value << (s
.Length
- i
- 1));
}
return (int)res
;
}
}
}
答案10:
using System
;
namespace Solution
{
public static class Program
{
public static int binToDec(string s
)
{
int temp
= 0;
return temp
= Convert
.ToInt32(s
, 2);
}
}
}
答案11:
using System
;
using System
.Linq
;
namespace Solution
{
public static class Program
{
public static int binToDec(string s
)
{
int pow
=0;
return (int)s
.Reverse().Sum(x
=> int.Parse(x
.ToString()) * Math
.Pow(2, pow
++));
}
}
}
答案12:
using System
;
namespace Solution
{
public static class Program
{
public static int binToDec(string input
)
{
return Convert
.ToInt32(input
, 2);
}
}
}
答案13:
using System
;
using System
.Linq
;
namespace Solution
{
public static class Program
{
public static int binToDec(string s
)
{
char[] arr
= s
.ToCharArray();
Array
.Reverse(arr
);
s
= new string(arr
);
int res
= 0;
for (int i
= 0; i
< s
.Length
; i
++)
{
if (s
[i
] == '1' )
{
res
+= (int)Math
.Pow(2, i
);
}
}
return res
;
}
}
}
答案14:
using System
;
namespace Solution
{
public static class Program
{
public static int binToDec(string s
)
{
String num
= s
;
int dec_value
= 0;
int base1
= 1;
int len
= num
.Length
;
for (int i
= len
- 1; i
>= 0; i
--)
{
if (num
[i
] == '1')
dec_value
+= base1
;
base1
= base1
* 2;
}
return dec_value
;
}
}
}
转载请注明原文地址: https://mac.8miu.com/read-485578.html