#字符串分隔#__huawei_no.4-1

字符串分隔

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

#include <iostream>
using namespace std;

int main() {
    string str;
    getline(cin, str);
    int n = str.size();
    int n1 = n / 8;
    int n2 = n % 8;
    int index = 0 ;
    if (n1 == 0) {
        cout << str;
        for (int i = 0; i < 8 - n2; i++) {
            cout << 0;
        }
    } else {
        for (int i = 1 ; i <= n1; i++) {
            for (int j = 0 ; j < 8; j++) {
                cout << str[index++];
            }
            cout << endl;
        }
        if ( n2 != 0) {
            for(int i = 0 ; i < n2; i++){
                cout<<str[index++];
            }
            for (int i = 0; i < 8 - n2; i++) {
                cout << 0;
            }
        }
    }
}

粗暴的方法,我个人觉得用substr函数会更快。

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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