首页 > ACM题库 > HDU-杭电 > hdu 2137 circumgyrate the string-计算几何-[解题报告]C++
2013
12-29

hdu 2137 circumgyrate the string-计算几何-[解题报告]C++

circumgyrate the string

问题描述 :

  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.

输入:

  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.

输出:

  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.

样例输入:

asdfass 7

样例输出:

a
 s
  d
   f
    a
     s
      s

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2137

题意:把一个字符串(奇数个字符)按中间字符为轴逆时针旋转n次后,输出。

mark:2WA,n居然可以为负。。。

代码:

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


void out0(char str[], int len){puts (str) ;}
void out1(char str[], int len)
{
    int i, j ;
    for (i = len-1 ; i >=0 ; i--)
    {
        for (j = 0 ; j < i ; j++) putchar (' ') ;
        printf ("%c\n", str[i]) ;
    }
}
void out2(char str[], int len)
{
    int i, j ;
    for(i=len-1;i>=0;i--)
    {
        for (j = 0 ; j < len/2 ; j++) putchar (' ') ;
        printf ("%c\n", str[i]) ;
    }
}
void out3(char str[], int len)
{
    int i, j ;
    for(i = len-1 ; i >= 0 ; i--)
    {
        for (j = 0 ; j < len-1-i ; j++) putchar (' ') ;
        printf ("%c\n", str[i]) ;
    }
}
void out4(char str[], int len)
{
    int i ;
    for (i = len-1 ; i >= 0 ; i--) putchar (str[i]) ;
    putchar ('\n') ;
}
void out5(char str[], int len)
{
    int i, j ;
    for (i = 0 ; i < len ; i++)
    {
        for (j = 0 ; j < len-i-1 ; j++) putchar (' ') ;
        printf ("%c\n", str[i]) ;
    }
}
void out6(char str[], int len)
{
    int i, j ;
    for(i=0;i<len ;i++)
    {
        for (j = 0 ; j < len/2 ; j++) putchar (' ') ;
        printf ("%c\n", str[i]) ;
    }
}
void out7(char str[], int len)
{
    int i, j ;
    for(i = 0 ; i < len ; i++)
    {
        for(j = 0 ; j < i ; j++) putchar (' ') ;
        printf("%c\n", str[i]) ;
    }
}


int main ()
{
    int k ;
    char str[100] ;
    while (~scanf ("%s %d%*c", str, &k))
    {
        if (k < 0) k = (-k)%8, k = 8-k ;
        switch (k%8){
        case 0: out0(str, strlen(str)) ; break ;
        case 1: out1(str, strlen(str)) ; break ;
        case 2: out2(str, strlen(str)) ; break ;
        case 3: out3(str, strlen(str)) ; break ;
        case 4: out4(str, strlen(str)) ; break ;
        case 5: out5(str, strlen(str)) ; break ;
        case 6: out6(str, strlen(str)) ; break ;
        case 7: out7(str, strlen(str)) ; break ;
        }
    }
    return 0 ;
}

解题转自:http://www.cnblogs.com/lzsz1212/archive/2012/02/02/2335599.html


  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. 您没有考虑 树的根节点是负数的情况, 若树的根节点是个很大的负数,那么就要考虑过不过另外一边子树了

  3. 网站做得很好看,内容也多,全。前段时间在博客园里看到有人说:网页的好坏看字体。觉得微软雅黑的字体很好看,然后现在这个网站也用的这个字体!nice!