队花的烦恼
Time Limit:1000MS Memory Limit:65536K
Total Submit:50 Accepted:41
Description
ACM队的队花C小+经常抱怨:“C语言中的格式输出中有十六、十、八进制输出,然而却没有二进制输出,哎,真遗憾!谁能帮我写一个程序实现输入一个十进制数n,输出它的二进制数呀?”
Input
输入有多个数据,数据以EOF结束;每个数据ni(0<=ni<=1000010000)用空格隔开;
(温馨提示:EOF即是一个文件的结束标志;while(scanf("%d",&n)!=EOF){})
Output
输出有多行,每行对应一个十进制数ni的二进制数;
注意:输出的二进制去掉任何一个多余的0;
Sample Input
0 1 2 10
Sample Output
0
1
10
1010
Source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AK1181 {
/// <summary>
/// 也是RE,不知道测试数据是一行在一起的,还是一行一个
/// 第二下AC了
/// </summary>
class Program {
static void Main(string[] args) {
string sb;
while ((sb = Console.ReadLine()) != null) {
string[] s = sb.Split();
long n = 0;
for (int k = 0; k < s.Length; k++)//这种写法不管是一行一个还是一行多个都可以处理,what's a fuck
{
n = long.Parse(s[k]);
if (n == 0) Console.WriteLine("0");
else {
long[] a = new long[105];
int i = 0;
while (n > 0) {
a[i++] = n % 2;
n /= 2;
}
int satrt = 0;
for (int j = i - 1; j >= 0; j--) if (a[j] != 0) { satrt = j; break; }
for (int j = satrt; j >= 0; j--)
Console.Write(a[j]);
Console.WriteLine();
}
}
}
}
}
}