程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU2137:circumgyrate the string

HDU2137:circumgyrate the string

編輯:C++入門知識

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

 

水題

就是把一個字符串轉來轉去

 

[cpp]
#include <stdio.h>  
#include <string.h>  
 
int main() 

    char str[100]; 
    int len,i,n,j; 
    while(~scanf("%s%d",str,&n)) 
    { 
        if(n>=8) 
        { 
            n = n%8; 
        } 
        else if(n<0) 
        { 
            n = n%8; 
            n = n+8; 
            n = n%8; 
        } 
        len = strlen(str); 
        if(n == 0) 
            puts(str); 
        else if(n == 1) 
        { 
            for(i = len-1; i>=0; i--) 
            { 
                for(j = 0; j<len; j++) 
                { 
                    if(j == i) 
                    { 
                        printf("%c\n",str[j]); 
                        break; 
                    } 
                    else 
                        printf(" "); 
                } 
            } 
        } 
        else if(n == 2) 
        { 
            for(i = len-1; i>=0; i--) 
            { 
                for(j = 0; j<=len/2; j++) 
                { 
                    if(j == len/2) 
                    { 
                        printf("%c\n",str[i]); 
                        break; 
                    } 
                    else 
                        printf(" "); 
                } 
            } 
        } 
        else if(n == 3) 
        { 
            for(i = 0; i<len; i++) 
            { 
                for(j = 0; j<len; j++) 
                { 
                    if(j == i) 
                    { 
                        printf("%c\n",str[len-1-i]); 
                        break; 
                    } 
                    else 
                        printf(" "); 
                } 
            } 
        } 
        else if(n == 4) 
        { 
            for(i = len-1; i>=0; i--) 
            { 
                putchar(str[i]); 
            } 
            printf("\n"); 
        } 
        else if(n == 5) 
        { 
            for(i = 0; i<len; i++) 
            { 
                for(j = len-1; j>=0; j--) 
                { 
                    if(j == i) 
                    { 
                        printf("%c\n",str[i]); 
                        break; 
                    } 
                    else 
                        printf(" "); 
                } 
            } 
        } 
        else if(n == 6) 
        { 
            for(i = 0; i<len; i++) 
            { 
                for(j = 0; j<=len/2; j++) 
                { 
                    if(j == len/2) 
                    { 
                        printf("%c\n",str[i]); 
                        break; 
                    } 
                    else 
                        printf(" "); 
                } 
            } 
        } 
         else if(n == 7) 
        { 
            for(i = 0; i<len; i++) 
            { 
                for(j = 0; j<len; j++) 
                { 
                    if(j == i) 
                    { 
                        printf("%c\n",str[i]); 
                        break; 
                    } 
                    else 
                        printf(" "); 
                } 
            } 
        } 
    } 
    return 0; 

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

int main()
{
    char str[100];
    int len,i,n,j;
    while(~scanf("%s%d",str,&n))
    {
        if(n>=8)
        {
            n = n%8;
        }
        else if(n<0)
        {
            n = n%8;
            n = n+8;
            n = n%8;
        }
        len = strlen(str);
        if(n == 0)
            puts(str);
        else if(n == 1)
        {
            for(i = len-1; i>=0; i--)
            {
                for(j = 0; j<len; j++)
                {
                    if(j == i)
                    {
                        printf("%c\n",str[j]);
                        break;
                    }
                    else
                        printf(" ");
                }
            }
        }
        else if(n == 2)
        {
            for(i = len-1; i>=0; i--)
            {
                for(j = 0; j<=len/2; j++)
                {
                    if(j == len/2)
                    {
                        printf("%c\n",str[i]);
                        break;
                    }
                    else
                        printf(" ");
                }
            }
        }
        else if(n == 3)
        {
            for(i = 0; i<len; i++)
            {
                for(j = 0; j<len; j++)
                {
                    if(j == i)
                    {
                        printf("%c\n",str[len-1-i]);
                        break;
                    }
                    else
                        printf(" ");
                }
            }
        }
        else if(n == 4)
        {
            for(i = len-1; i>=0; i--)
            {
                putchar(str[i]);
            }
            printf("\n");
        }
        else if(n == 5)
        {
            for(i = 0; i<len; i++)
            {
                for(j = len-1; j>=0; j--)
                {
                    if(j == i)
                    {
                        printf("%c\n",str[i]);
                        break;
                    }
                    else
                        printf(" ");
                }
            }
        }
        else if(n == 6)
        {
            for(i = 0; i<len; i++)
            {
                for(j = 0; j<=len/2; j++)
                {
                    if(j == len/2)
                    {
                        printf("%c\n",str[i]);
                        break;
                    }
                    else
                        printf(" ");
                }
            }
        }
         else if(n == 7)
        {
            for(i = 0; i<len; i++)
            {
                for(j = 0; j<len; j++)
                {
                    if(j == i)
                    {
                        printf("%c\n",str[i]);
                        break;
                    }
                    else
                        printf(" ");
                }
            }
        }
    }
    return 0;
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved