题解 | #将真分数分解为埃及分数#

将真分数分解为埃及分数

https://www.nowcoder.com/practice/e0480b2c6aa24bfba0935ffcca3ccb7b

#include <iostream>
using namespace std;
int main() {
    string str;
    while (cin >> str) { 
        string str_a,str_b;
        str_a.assign(str,0,str.find('/'));//assign:赋值操作
        str_b.assign(str,str.find('/')+1);
        int a=atoi(str_a.c_str()),b=atoi(str_b.c_str());//atoi:c风格字符转换【int】;p=str_a.c_str():p为str_a的c风格字符串版本;
        cout << "1/"<<b;
        for(int i=0;i<a-1;i++){
            cout << "+1/"<<b;
        }
        cout << endl;
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务