400 - Unix ls (UVA)

题目链接如下:

Online Judge

写完后看刘汝佳代码,其实C可以直接算出,对应R也就可以算出来了,不用我那么麻烦判断。

我的代码如下:

#include 
#include 
#include 
#include 
#include 
// #define debug
const int maxx = 60;

int n, maxLen, R, C;
char s[maxx + 1];
std::vector vec;

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while(scanf("%d", &n) != EOF){
        vec.clear();
        maxLen = 0;
        while(n--){
            scanf("%s", s);
            vec.push_back(s);
            maxLen = std::max((int)strlen(s), maxLen);
        }
        for(int i = 0; i < vec.size(); ++i){
            while(vec[i].size() < maxLen){
                vec[i] += " ";
            }
        }
        sort(vec.begin(), vec.end());
        for(int i = 1; i <= vec.size(); ++i){
            if(((vec.size() - 1) / i + 1) * (maxLen + 2) - 2 <= maxx){
                R = i;
                C = (vec.size() - 1) / i + 1;
                break;
            }
        }
        for(int i = 0; i < maxx; ++i){
            printf("-");
        }
        printf("\n");
        for(int i = 0; i < R; ++i){
            for(int j = 0; j < C; ++j){
                printf("%s%s", i + j * R < vec.size() ? vec[i + j * R].c_str(): "", j == C - 1 ? "" : "  ");
            }
            printf("\n");
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

你可能感兴趣的:(UVA,c++)