牛客oj 習題6.1八進制&&習題6.2又一版A+B(進制轉換)

 

 

送分題,不過string的reverse方法真好用。

 

 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cctype>
#include <climits>
 
using namespace std;
 
const int MAXN = 1005;
const int INF = INT_MAX;
 
int main(){
  //  freopen("in.txt", "r", stdin);
    int N;
    while(~scanf("%d", &N)){
    	string str;
    	while(N != 0){
    		str += (N%8) + '0';
    		N /= 8;
    	}
    	reverse(str.begin(), str.end());
    	cout << str << endl;
    }
    return 0;
}

 

 

又一道送分題,不過不要忘了邊界,當A+B爲0時不能輸出爲空!

 

 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cctype>
#include <climits>
 
using namespace std;
 
const int MAXN = 1005;
const int INF = INT_MAX;
 
int main(){
   // freopen("in.txt", "r", stdin);
    int m, A, B;
    while(~scanf("%d", &m)){
    	if(m == 0) break;
    	scanf("%d %d", &A, &B);
    	string str;
    	long long sum = A+B;
    	if(sum == 0) str = "0";
    	while(sum != 0){
    		str += (sum%m) + '0';
    		sum /= m;
    	}
    	reverse(str.begin(), str.end());
    	cout << str << endl;
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章