Problem N
Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 5
Problem Description
Give you a number on base ten,you should output it on base two.(0 < n < 1000)
Input
For each case there is a postive number n on base ten, end of file.
Output
For each case output a number on base two.
Sample Input
1
2
3
Sample Output
1
10
11
=========================
呃。。。dp。。。???
AC代码:
#include<bits/stdc++.h> using namespace std; int a[50]; int main() { int n; while(cin>>n) { int i=0; do { a[++i]=n%2; n/=2; }while(n!=0); for(int j=i;j>=1;--j)cout<<a[j]; cout<<endl; } return 0; }
借这道题目,我们再来复习一下十进制->n进制的转换代码;
void ks(int m,int n)//m为十进制数,n为目标进制; { int i=0; do { a[++i]=m%n; m/=n; }while(m!=0); for(int j=i;j>=1;--j) { if(a[j]>=0&&a[j]<=9)cout<<a[j]; else cout<<char(a[j]+55);//A,B,C,D,E等字符型。。。 } }
还有n进制——>十进制;
int js(string n,int m) { int sum=0; int count=n.length(); for(int i=count-1;i>=0;--i) { if(n[i]>='0'&&n[i]<='9')(sum+=n[i]-48)*pow(m,count-i-1); else sum+=(n[i]-55)*pow(m,count-i-1); } return sum; }感觉有些东西还是有必要背一下的,当然不能用“死”了。。。。。。
The end;