PAT 1017 乙等 (A除以B) C++版本

本人新人一枚,刚开始锻炼写博客,PAT再刷中。


----------


#include 
#include 
#include 
using namespace std;

int transToNum(char s){

    return (s - 48);
}
// int型数字转string 方法一
string transToChar(int n){
    stringstream ss;
    ss << n;
    return ss.str();
}
//int型数字转string 方法二
string transToChar1(int n){
    return to_string(n);
}
int main()
 {
    string s;
    int n;
    cin >> s >> n;
    string s1;
    int temp=0;
    for (int i = 0; i < s.length();i++){
        int num = temp * 10 + transToNum(s[i]);
        if (i == 0 && num < n){
            if (s.length() == 1){
                s1 += (transToChar1(0));
            }
            s1 = s1;
            temp = num;
        }else{
            if (num >= n){
                s1 += (transToChar1(num / n));
                temp = (num % n);

            }
            else{
                s1 += (transToChar1(0));
                temp = num;
            }
        }

    }

    cout << s1 << " " << temp;
    return 0;
}

你可能感兴趣的:(PAT-编程)