【PAT (Basic Level) Practice (中文)】刷题记录

1001(条件判断)

#include <iostream>

using namespace std;

int main()
{
    
    
    int n;
    cin >> n;
    
    int res = 0;
    while(n > 1)
    {
    
    
        if(n & 1) n = (n * 3 + 1) / 2;
        else n = n / 2;
        res ++;
    }
    
    cout << res << endl;
    
    return 0;
}

1002(数字与字符串)

#include <iostream>
#include <algorithm>


using namespace std;

string get(int x)
{
    
    
    if(x == 0) return "ling";
    else if(x == 1) return "yi";
    else if(x == 2) return "er";
    else if(x == 3) return "san";
    else if(x == 4) return "si";
    else if(x == 5) return "wu";
    else if(x == 6) return "liu";
    else if(x == 7) return "qi";
    else if(x == 8) return "ba";
    else if(x == 9) return "jiu";
}

int main()
{
    
    
    string s;
    cin >> s;
    
    int num = 0;
    for(auto c : s) num += c - '0';
    
    string cnum = to_string(num); // to_string() : 将数字转换为字符串
    string res;
    for(auto c : cnum) 
    {
    
    
        if(res.size()) res += " ";
        res += get(c - '0');
    }
    
    cout << res << endl;
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43154149/article/details/108603865