IT_Programming/C · C++

[c] 다이아몬드 지그재그로 출력하기 (출력 문자 : A~Z)

JJun ™ 2009. 6. 1. 16:53

완전 캐노가다 인거다~ ㅡ_ㅡ;;

 

================================================================================================

 

[실행결과] 

: A~Z까지 반복하면서 역 S자 모양으로 출력한다. 

 

================================================================================================

 

#include <stdio.h>
#define BLANK ' '

 

int main(void)
{
       int i, j, middle, size, count, temp, asc;

       while(1)
       {
              printf("사이즈를 입력하세요. (홀수) : ");
              scanf("%d", &size);

            

              if(size % 2 == 1)
                     break;
              else
                     continue;
       }

 

       middle = size / 2;
       count = temp = 0;
       asc = 65;
 
       for(i=0; i<size; ++i) 
       {
              if(i % 2 == 1)  // 홀수 행
              {
                     temp = asc + ((middle+count) - (middle-count)); // 미리 들어갈 아스키코드 값을 계산
                     asc = temp + 1;    // 아스키 값 저장
   
                     for(j=0; j<size; ++j)
                     {
                            if((j>=middle-count) && (j<=middle+count))
                            {
                                   while(temp > 90) // Z의 아스키 코드값을 넘은 경우
                                   { 
                                          temp -= 26;
                                   }
     
                                   if(temp < 65) // A의 아스키 코드값보다 작은 경우
                                          temp += 26;

                              

                                   printf("%c", temp--);
                            }
                            else
                                   printf("%c", BLANK);
                     }
              }
              else // 짝수행
              {
                     for(j=0; j<size; ++j)
                     {
                            if((j>=middle-count) && (j<=middle+count))
                            {
                                   while(asc > 90) // Z의 아스키 코드값을 넘은 경우
                                   {
                                          asc -= 26;
                                   }

                                   printf("%c", asc++);
                            }
                            else
                                   printf("%c", BLANK);
                     }
              }

            

              (i<middle)? ++count : --count;
              printf("\n");
       }

       return 0;
}


================================================================================================