牛客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;
}
扫描二维码关注公众号,回复: 10728447 查看本文章
发布了411 篇原创文章 · 获赞 72 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/Flynn_curry/article/details/104721446