每一行都跑迴圈 迴圈從最後一列到第零列 每次都輸出字元 遇 '\0'輸出空白(陣列要先初始化)

網址:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=431

-2017-05-22-修正-------------------------------------------------------------------------

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

#define MAX 101

void Print_a_line(int, int , char(*)[MAX]);

int main()
{
    int column, count, max_len = 0;
    char sentences[MAX][MAX] = {0};

    /* Read in and find the max length. */
    for(count = 0; gets(sentences[count]); count++)
        if(strlen(sentences[count]) > max_len)
            max_len = strlen(sentences[count]);

    for(column = 0; column < max_len; column++)
        Print_a_line(column, count, sentences);

    return 0;
}

void Print_a_line(int column, int count, char sentences[][MAX])
{
    int row;
    for(row = count-1; row >= 0; row--)
        if(sentences[row][column])
            printf("%c", sentences[row][column]);
        else{
            /* If nothing, print whitespace */
            if(row != 0){
                // The 0th row don't need the white space.
                printf(" ");
            }
        }
    printf("\n");
}

 

 

-2017-05-22--------------------------------------------------------------------------

package test;

import java.util.*;

public class main {

    public static void main(String[] args) {
        try {
            final Scanner in = new Scanner(System.in);
            ArrayList<String> al = new ArrayList<>();
            int maxLen = 0;

            while (in.hasNextLine()) {
                String str = in.nextLine();
                if (str.length() > maxLen) {
                    maxLen = str.length();
                }
                al.add(str);
            }
            // ab
            // 123
            // 由1->a (由下往上)再由左往右
            for (int j = 0; j < maxLen; j++) {
                for (int i = (al.size() - 1); i >= 0; i--) {
                    try {
                        System.out.print("" + al.get(i).charAt(j));
                    } catch (StringIndexOutOfBoundsException sioobe) {
                        if (i != 0) {
                            // 第0列轉到最後一行,最後一行遇缺不補空白。
                            System.out.print(" ");
                        }
                    }
                }
                System.out.println("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 

arrow
arrow
    全站熱搜

    awesq123 發表在 痞客邦 留言(0) 人氣()