Home > Database > Mysql Tutorial > body text

整型相除截断的技巧

WBOY
Release: 2016-06-07 15:30:13
Original
1204 people have browsed it

欢迎进入C/C++编程社区论坛,与300万技术人员互动交流 >>进入 该题中主要用到了整型相除截断的技巧。例如,计算行数和输出时候判断是否为最后一列。 [cpp] #include stdio.h #include stdlib.h #include string.h int cmp(const void *_a, const void *_b)

欢迎进入C/C++编程社区论坛,与300万技术人员互动交流 >>进入

    该题中主要用到了整型相除截断的技巧。例如,计算行数和输出时候判断是否为最后一列。

    [cpp]

    #include

    #include

    #include

    int cmp(const void *_a, const void *_b) {

    char *a = (char*)_a;

    char *b = (char*)_b;

    return strcmp(a, b);

    }

    // 根据len长度的字符串,不足以空格补齐

    void print(char st[], int len) {

    int tmp = len - strlen(st);

    printf("%s", st);

    while (tmp--)

    printf(" ");

    }

    int main() {

    int n;

    char s[105][65];

    while (scanf("%d", &n) != EOF) {

    int lMax, c, r;

    lMax = c = r = 0;

    for (int i=0; i

    scanf("%s", s[i]);

    int tmp = strlen(s[i]);

    if (tmp > lMax)

    lMax = tmp;

    }

    c = 62 / (lMax+2);                      // 计算列数

    r = (n+c-1) / c;                        // 计算行数

    qsort(s, n, sizeof (s[0]), cmp);

    printf("------------------------------------------------------------\n");

    for (int i=0; i

    for (int j=0; j

    int tmp = j*r + i;

    if (tmp >= n) continue;

    if (tmp >= ((int)((n-1)/r))*r)  // 如果为最后一列

    print(s[tmp], lMax);

    else

    print(s[tmp], lMax+2);

    }

    printf("\n");

    }

    }

    return 0;

    }

整型相除截断的技巧

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!