首页 > 代码库 > HDU2137 circumgyrate the string

HDU2137 circumgyrate the string

circumgyrate the string

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4235    Accepted Submission(s): 978


Problem Description
  Give you a string, just circumgyrate. The number N means you just   circumgyrate the string N times, and each time you circumgyrate the string for 45 degree anticlockwise.
 

Input
  In each case there is string and a integer N. And the length of the string is always odd, so the center of the string will not be changed, and the string is always horizontal at the beginning. The length of the string will not exceed 80, so we can see the complete result on the screen.
 

Output
  For each case, print the circumgrated string.
 

Sample Input
asdfass 7
 

Sample Output
a s d f a s s
 

Author
wangye
 

Source
HDU 2007-11 Programming Contest_WarmUp
 


#include <stdio.h>
#include <string.h>
#define maxn 100

char str[maxn];

void putBackspace(int n) {
    while(n--) putchar(' ');
}

void rotate(int cent, int n) {
    switch(n) {
        case 0:
        puts(str); return;
        case 1:
        for(int i = cent << 1, j = cent << 1; i >= 0; --j) {
            putBackspace(i--);
            printf("%c\n", str[j]);
        } return;
        case 2:
        for(int j = cent << 1; j >= 0; --j) {
            putBackspace(cent);
            printf("%c\n", str[j]);
        } return;
        case 3:
        for(int j = cent << 1, i = 0; j >= 0; --j) {
            putBackspace(i++);
            printf("%c\n", str[j]);
        } return;
        case 4:
        for(int j = cent << 1; j >= 0; --j) {
            printf("%c", str[j]);            
        } putchar('\n'); return;
        case 5:
        for(int j = 0, i = cent << 1; i >= 0; ++j) {
            putBackspace(i--);
            printf("%c\n", str[j]);
        } return;
        case 6:
        for(int j = 0; j <= cent << 1; ++j) {
            putBackspace(cent);
            printf("%c\n", str[j]);
        } return;
        case 7:
        for(int j = 0, i = 0; j <= cent << 1; ++j) {
            putBackspace(i++);
            printf("%c\n", str[j]);
        } return;
    }
}

int main() {
    int n, i, cent;
    while(scanf("%s%d", str, &n) != EOF) {
        cent = strlen(str) >> 1;
        n = (n % 8 + 8) % 8;
        rotate(cent, n);
    }
    return 0;
}


HDU2137 circumgyrate the string