多行,每一行表示要求的数字
输出共T行。每行输出求得的二进制串。
23 535 2624 56275 989835
10111 1000010111 101001000000 1101101111010011 11110001101010001011
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=0; while (in.hasNext()) { n=in.nextInt(); for(int i=0;i<n;i++) { int m=in.nextInt(); System.out.println(Integer.toBinaryString(m)); } } } }
jdk的源码就是这么写的
package com.speical.first;
import java.util.Scanner;
/**
*
* @author special
* @date 2018年2月4日 上午10:38:54
*/
public class Pro184 {
public static String toBinary(int num){
char[] digits = new char[32];
int index = 32;
while(num != 0){
digits[--index] = (char) ('0' + (num & 1));
num >>>= 1;
}
return new String(digits, index, 32 - index);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
while(input.hasNext()){
int n = input.nextInt();
System.out.println(toBinary(n));
}
}
}
using namespace std;
#include <cstdio>
#include <string>
#include <algorithm>
int main(){
unsigned int n;
while(scanf("%u", &n) != EOF){
string ans = "";
while(n > 0){
ans += char((n & 0x00000001) + '0');
n = n >> 1;
}
reverse(ans.begin(), ans.end());
printf("%s\n", ans.c_str());
}
return 0;
} #include<stdio.h>
int main (){//the shorter,the better.
int n,t,bin[28],i;
for(;~scanf("%d",&n)&&n;)
for (;n>0&&~scanf("%d",&t);n--){
for(i=0;t>0&&~(bin[i++]=t&1);t>>=1);
for (--i;i>=0;i==0?printf("%d\n",bin[i]):printf("%d",bin[i]),i--);
}
}
import java.util.Scanner;
import java.util.Stack;
public class
Main{
public static void main(String[] s){
Scanner
sc=new Scanner(System.in);
while(sc.hasNext()){
int t=sc.nextInt();
int[] num=new int[t];
//读取数据
for(int i=0;i<t;i++){
num[i]=Integer.parseInt(sc.next());
}
//开始处理
for(int i=0;i<t;i++){
System.out.println(test(num[i]));
}
}
}
public static String test(int a){
//用了一个栈
Stack<Integer> stack=new Stack<Integer>();
while(a>0){
stack.push(a%2);
a/=2;
}
String str="";
while(!stack.isEmpty()){
str+=stack.pop();
}
return str;
}
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
int main(void)
{
int n;
while(scanf("%d",&n) != EOF)
{
int input[1001];
int output[1001];
int i = 0;int j = 0;
int m=0;
for(i=0;i<n;i++)
{
scanf("%d",&input[i]);
}
for(i=0;i<n;i++)
{
while(input[i] != 0)
{
output[m++] = input[i]%2;
input[i]/=2;
}
for(j=m-1;j>=0;j--)
{
printf("%d",output[j]);
}
m=0;
printf("\n");
}
}
}
#include<iostream>
using namespace std;
void binary(unsigned int n);
int main(){
int num;
unsigned int n;
cin >> num;
for (int i = 0; i < num; i++){
cin >> n;
binary(n);
}
return 0;
}
void binary(unsigned int n){
int a; //yushu
string str;
if (n == 0){
cout << "0" << endl;
return;
}
while(n != 1){
a = n % 2;
n = n/2;
if(a == 1){
str = str + '1';
}
else{
str = str + '0';
}
}
str = str + '1';
int len = str.length();
for (int i = len-1;i >= 0; i--){
cout << str[i];
}
cout << endl;
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
int n;
while(scanner.hasNext()){
n=scanner.nextInt();
StringBuilder str=new StringBuilder();
while(n/2!=0 || n==1){
str.append(n%2);
n=n/2;
}
System.out.println(str.reverse().toString());
}
}
}
#include <iostream>
#include <cstdio>
#include <string>
#include <stack>
using namespace std;
stack<int> num;
int main() {
int x;
while (scanf("%d",&x)!=EOF)
{
while (x!=0)
{
num.push(x % 2);
x /= 2;
}
while (!num.empty())
{
cout << num.top();
num.pop();
}
cout << endl;
}
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
int num = 0, res = 0, remain = 0;
vector<int> binary;
while(cin >> num){
while(num > 0){
remain = num % 2;
binary.push_back(remain);
num = num/2;
}
for(int i = binary.size()-1; i >= 0; --i){
cout << binary[i];
}
cout << endl;
binary.clear(); //清空数组
}
return 0;
}