poj1617---columnar encryption

题意:给出keyword,如BATBOY,A的ascii值最小,所以第二列最先输出,B有两个,左边的先输出,也就是说,接下来输出第一列和第4列,

所以每一个字母都带有一个ascii值和一个序号,用结构组合起来

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

struct com{
    int asc;
    int th;
};
int cmp(const void *a,const void *b)
    {
        if(((struct com *)a)->asc!=((struct com *)b)->asc)
            return(((struct com *)a)->asc>((struct com *)b)->asc);
        else
            return(((struct com *)a)->th-((struct com *)b)->th);
    }
int main()
{
    int i,j;
    struct com Arr[10];
    char temp[11],cybier[101];
    while(scanf("%s",temp),strcmp(temp,"THEEND")){
    scanf("%s",cybier);
    int column=strlen(temp);
    int row=strlen(cybier)/column;
    for(i=0;i<column;i++)
    {
        Arr[i].asc=temp[i]-'A';
        Arr[i].th=i+1;
    }
    qsort(Arr,column,sizeof(Arr[0]),cmp);
    int cur=0;
    char table[row][column+1];
    for(j=0;j<column;j++)
    {
        for(i=0;i<row;i++)
        {
            table[i][Arr[j].th-1]=cybier[cur++];
        }
    }
    for(i=0;i<row;i++)
    {
        table[i][column]='';
    }
    for(i=0;i<row;i++)
    {
        printf("%s",table[i]);
    }
    printf("
");
    }
    return 0;
}

输出出了很多次问题:实际上我如果定义个table[row][column]数组,对这个数组的值采用的是双重for循环赋值的形式,每一行可以不要'',用printf(table),但是在最后一行就得超出数组之外添加'',但是这就越界了

所以定义为table[row][column+1],在每行最后一列加'',最后for循环输出每一行

原文地址:https://www.cnblogs.com/gabygoole/p/4494957.html