'분류 전체보기'에 해당되는 글 107건

  1. 2009.06.15 :: [MySql] Alter Table 문법
  2. 2009.06.08 :: [Accept] 10018 Reverse and Add
  3. 2009.06.03 :: [10018] Reverse and Add
  4. 2009.06.03 :: Why do I have a blackout?
  5. 2009.06.03 :: I hate the people (1)
  6. 2009.06.02 :: [Time Limit] Carmichael Numbers
  7. 2009.06.02 :: [Accept] 10006 Carmichael Numbers
  8. 2009.06.02 :: [Accept] 10110 Light, more light
  9. 2009.06.02 :: [Accept] 850 Crypt Kicker II
  10. 2009.06.02 :: [Accept] 10132 File Fragmentation
DataBase 2009. 6. 15. 18:03
반응형

1. ADD new field with Foreign Key : 테이블에 열 추가
ALTER table Table_Name ADD COLUMN  Field_Name Field_Type AFTER COLUMN;
Ex) ALTER table customer ADD COLUMN Gender char(1)
;
Ex)
ALTER table my_contacts ADD COLUMN contact_id INT NOT NULL AUTO _INCREMENT FIRST 
ADD PRIMARY KEY (contact_id) ;

AUTO _INCREMENT : 데이터를 순서대로 증가 시키게 하는 키워드, NOT NULL을 무시하고 알아서 값을 넣는다.
FIRST : 새열을 열리스트의 첫번째로 하라는 키워드
AFTER : 새로 추가되는 열이 명시된 열 다음에 생성되로록 하는 키워드

2. CHANGE field : 기존 열의 이름과 데이터 타입을 변경 (데이터 타입을 변경할 경우 데이터를 손실할 수 있음)
ALTER table Table_Name CHANGE COLUMN Prev_Field_Name Prev_Field_Name Field_Type ;
Ex) ALTER table customer CHANGE COLUMN Address Addr char(50) ;

Ex) ALTER table customer 
      CHANGE Address Addr char(50),
      CHANGE Phone_Num Phone char(50);

3. MODIFY field : 기존 열의 데이터 타입이나 위치 변경 (데이터 타입을 변경할 경우 데이터를 손실할 수 있음)
ALTER table Table_Name MODIFY COLUMN Field_Name Field_Type;
Ex) ALTER table customer MODIFY COLUMN Addr char(30) ;


4. DROP field : 기존 열 삭제
ALTER table Table_Name DROP Gender
Ex) ALTER table customer DROP Gender


5. ADD new FOREIGN KEY : foreign key 추가
ALTER table Table_Name ADD (FOREIGN KEY (`Field_Name`) REFERENCES `Reference_Table_Name` (`Actual_Field_Name`));
Ex) ALTER table MCN ADD (FOREIGN KEY (`USER_GRP_IDX`) REFERENCES `USER_GRP_VIEW` (`IDX`));


6. DROP FOREIGN KEY : foreign key 삭제
ALTER table Table_Name DROP FOREIGN KEY Foreign_Key_Name;
Ex) ALTER table MCN DROP FOREIGN KEY MCN_ibfk_1;

7. DROP PRIMARY KEY : primary key 삭제
ALTER table Table_Name DROP PRIMARY KEY ;
Ex) ALTER table MCN DROP PRIMARY KEY;

8. RENAME Table_Name : 테이블 이름 변경
ALTER TABLE Table_Name RENAME TO New_Table_Name;

Ex) ALTER TABLE project RENAME TO project_list;

9. AUTO_INCREMENT 추가 
ALTER TABLE Table_Name CHANGE Field_Name Field_Name Field_Type NOT NULL AUTO_INCREMENT;
Ex) ALTER TABLE Table_Name CHANGE your_id your_id INT(11) NOT NULL AUTO_INCREMENT;

9. AUTO_INCREMENT 삭제
ALTER TABLE Table_Name CHANGE Field_Name Field_Name Field_Type NOT NULL;
Ex) ALTER TABLE Table_Name CHANGE your_id your_id INT(11) NOT NULL;
반응형

'DataBase' 카테고리의 다른 글

JOIN  (0) 2009.09.17
[Mysql/Psql] 데이터 암호화  (0) 2009.09.14
[Mysql] 기초 키워드 및 함수  (0) 2009.06.17
[Mysql] 문자 함수  (0) 2009.06.17
[Mysql] Oracle에 MINUS를 Mysql에서 구현해보자  (0) 2009.06.16
posted by ssuk1010
:
ACM 2009. 6. 8. 15:04
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
#define MAX_SIZE 20 
#define MAX_CNT 1000 
 
int isPalindrome(unsigned long long palindrome);
int palindromeFunc(unsigned long long palindrome);
 
unsigned long long g_palindromeNum;
int g_repeatCnt;
 
int main(void)
{
    int testCase, returnVal;
    unsigned long long num;
 
    scanf("%d", &testCase);
 
    while(testCase)
    {
        scanf("%llu", &num);
        returnVal = palindromeFunc(num);
        printf("%d %llu\n", g_repeatCnt, g_palindromeNum);
        testCase--;
    }
    return 0;
}
 
int palindromeFunc(unsigned long long palindrome)
{
    char numStr[MAX_SIZE], reverseNumStr[MAX_SIZE];
    int palindromeLen, i, j=0, returnVal, count=1;
    unsigned long long reversePalindrome;
 
    while(count++)
    {
        if(count==MAX_CNT) return 0;
        
        returnVal =  isPalindrome(palindrome);
        if(returnVal) {
            g_repeatCnt = count-2;
            g_palindromeNum = palindrome;
            return 1;
        }
 
        sprintf(numStr, "%llu", palindrome);
        palindromeLen = strlen(numStr);
 
        // Reverse
        for(i=palindromeLen-1, j=0; i>=0; i--){
            reverseNumStr[j++] = numStr[i];
        }
        reverseNumStr[j] = '\0';
 
        // Add
        reversePalindrome = (unsigned long long)(atoll(reverseNumStr));
        palindrome += reversePalindrome;    
    }
    return 0;
}
 
int isPalindrome(unsigned long long palindrome)
{
    char numStr[MAX_SIZE];
    int palindromeLen, i, j;
 
    snprintf(numStr, MAX_SIZE,  "%llu", palindrome);
    palindromeLen = strlen(numStr);
 
    // Check as if Reverse
    for(i=0, j=palindromeLen-1; i!=palindromeLen/2;  i++, j--)
    {
        if(numStr[i]!=numStr[j])
            return 0;
    }
    return 1;
}

반응형

'ACM' 카테고리의 다른 글

[10018] Reverse and Add  (0) 2009.06.03
[Time Limit] Carmichael Numbers  (0) 2009.06.02
[Accept] 10006 Carmichael Numbers  (0) 2009.06.02
[Accept] 10110 Light, more light  (0) 2009.06.02
[Accept] 850 Crypt Kicker II  (0) 2009.06.02
posted by ssuk1010
:
ACM 2009. 6. 3. 20:25
반응형

Reverse and Add

The Problem

The "reverse and add" method is simple: choose a number, reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number from left to right and right to left), repeat this procedure.

For example:
195 Initial number
591
-----
786
687
-----
1473
3741
-----
5214
4125
-----
9339 Resulting palindrome

In this particular case the palindrome 9339 appeared after the 4th addition. This method leads to palindromes in a few step for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It is not proven though, that there is no such a palindrome.

Task :
You must write a program that give the resulting palindrome and the number of iterations (additions) to compute the palindrome.

You might assume that all tests data on this problem:
- will have an answer ,
- will be computable with less than 1000 iterations (additions),
- will yield a palindrome that is not greater than 4,294,967,295.
 

The Input

The first line will have a number N with the number of test cases, the next N lines will have a number P to compute its palindrome.

The Output

For each of the N tests you will have to write a line with the following data : minimum number of iterations (additions) to get to the palindrome and the resulting palindrome itself separated by one space.

Sample Input

3
195
265
750

Sample Output

4 9339
5 45254
3 6666
반응형

'ACM' 카테고리의 다른 글

[Accept] 10018 Reverse and Add  (0) 2009.06.08
[Time Limit] Carmichael Numbers  (0) 2009.06.02
[Accept] 10006 Carmichael Numbers  (0) 2009.06.02
[Accept] 10110 Light, more light  (0) 2009.06.02
[Accept] 850 Crypt Kicker II  (0) 2009.06.02
posted by ssuk1010
:
ssuk's story 2009. 6. 3. 17:27
반응형

* 자율신경계 균형 무너질 때 발생

◇왜 쓰러지나

실신의 가장 흔한 원인 중 하나는 자율신경계 균형이 무너지며 기절하는 것이다.

우리 몸은 뇌·척수 등의 중추신경계를 비롯해 말초신경계·자율신경계로 구성되고, 자율신경계는 교감 및 부교감신경으로 나뉜다.

인체에는 이들 신경계가 조화를 이루면서 스스로 균형을 유지하는데, 자율 신경 장애로 인한 실신은 교감신경과 부교감신경의 균형이 깨지면서 생긴다. 부교감 신경이 우세해져혈압이 떨어지고 맥박이 느려지면서 뇌로 가는 혈액량이 부족해져 실신하게 되는 것이다.

밀폐된 공간·기침·식후 주의

◇언제 일어나는가

자율 신경계 균형이 잘 깨지는 경우는 밀폐된 장소(만원 버스·백화점·교실·교회·성당·화장실 등)에서 한 자세로 오랫동안 서 있을 때다. 한 자세로 서 있다보면 심장으로 돌아와 순환하는 혈액량이 줄어들고, 이런 경우 심장박동 수가 증가해 혈액량을 늘려줘야 하는데 자율 신경에 이상이 생기면서 뇌혈류가 부족, 기절하는 것.

또 드물긴 하지만 일어나는 상황에 따라 배뇨성·배변성·기침·식후 실신 등으로도 나뉜다. 어떤 행동을 하다 정신을 잃게 되느냐가 중요한 기준이 되는 셈이다.

배뇨성 실신은 A씨처럼 소변을 보는 도중에 실신하는 것으로, 과음을 한 뒤 잠자다 소변을 보려고 할 때 주로 일어난다. 배변 실신은 나이가 많은 사람들 중에 발생하는 경우가 많다.

기침하다가 실신하는 수도 있는데, 만성기관지염이나 담배를 많이 피우는 사람이 심한 기침을 하다 의식을 잃는 경우가 이에 해당한다. 어린이들이 발작성 기침을 하다 실신하기도 한다. 식후 실신은 식사 도중 음식물을 삼키는 과정에서 일어난다.

답답함·식은땀·메스꺼움 증상

◇치료는 어떻게 하나

자율 신경 조절 장애로 인한 실신은 가슴이 답답하고 식은땀이 흐르면서 메스껍고 힘이 없어지는 등의 증상이 먼저 나타나는 만큼 이때 바로 누이면 의식 소실은 막을 수 있다. 누워 다리를 심장 높이 이상으로 올리면 뇌로 가는 혈액이 정상화돼 곧 정상상태를 찾을 수 있다고 전문의들은 설명했다. 전문의들은 또“실신하지 않았더라도 이같은 증상을 경험한 사람들은 주의가 필요하다”고 강조한다.

이동중이라면 최대한 자세를 낮춰 앉는 것이 예방법.

배뇨성·배변성 실신 등 상황성 실신은 실신을 일으키는 특별한 상황을 피하면 충분히 막을 수 있다.

그러나 상황을 피하기 어렵거나 예측하기 힘들 때에는 베타교감신경차단제나 알파자극제·부교감신경억제제·호르몬제·항우울제 등 약물을 쓰기도 한다.

반응형

'ssuk's story' 카테고리의 다른 글

공부하기 좋은 사이트  (0) 2009.09.16
I hate the people (1)  (0) 2009.06.03
ssuk's first story  (1) 2009.06.02
posted by ssuk1010
:
ssuk's story 2009. 6. 3. 11:21
반응형

I hate the people who only believe what they think.

They don't liesten that other people think or know even if it is obvoius certain.

Those people always think they are always correct and other people are wrong.

When I talk to them, it makes me mad and uncomfortable.
 
Why do they act like that?

I don't understand.

Every people can be wrong. 

If they realize that they are wrong, they should liesten others thinking and the fact.

I don't want to talk to them anymore.

반응형

'ssuk's story' 카테고리의 다른 글

공부하기 좋은 사이트  (0) 2009.09.16
Why do I have a blackout?  (0) 2009.06.03
ssuk's first story  (1) 2009.06.02
posted by ssuk1010
:
ACM 2009. 6. 2. 21:11
반응형

There are two kinds of ways that I solve.
But both are not accepted from Judge Server because of Time Limit.

----------------------------------------------------------------------------------------------

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

#define MAX_NUM 65001
#define MIN_NUM 2

int carmichaelTable[MAX_NUM];

void precalculate(void);
int isCarMichaelFuc(int number);
unsigned long long myPow(int first, int second);

int main(void)
{
   
int i, inputNumber, isCarmichael;
   
long long result;

   
memset(carmichaelTable, 0, sizeof(carmichaelTable));

   
precalculate();

   
while(scanf("%d", &inputNumber) && inputNumber!=0)
   
{
       
if( !carmichaelTable[inputNumber] )
       
{
           
printf("%d is normal.\n", inputNumber);
       
}
       
else
       
{
           
printf("The number %d is a Carmichael number.\n", inputNumber);
       
}
   
}
   
return 0;
}

void precalculate(void)
{
   
int i, isCarmichael;
   
for(i=MIN_NUM; i<MAX_NUM; i++)
   
{
       
isCarmichael = 1;
       
isCarmichael = isCarMichaelFuc(i);
       
//printf("%d isCarmichael = %d\n", j, isCarmichael);
       
if( isCarmichael ) {
           
carmichaelTable[i] = 1;
           
//printf("%d is %d\n", i, carmichaelTable[i]);
       
}
   
}
}

int isCarMichaelFuc(int number)
{
   
int i, isCarmichael=1;
   
unsigned long long result;
   
for(i=MIN_NUM; i<number; i++)
   
{
       
result = myPow(i, number);
       
//printf("%d %d = %d\n", i, number, result);
       
if( result != i) {
           
isCarmichael = 0;
           
break;
       
}
   
}
   
return isCarmichael;
}

unsigned long long myPow(int first, int second)
{
   
int i;
   
unsigned long long result = first;
   
for(i=1; i<second; i++)
   
{
       
result *= first;
       
result %= second;
   
}
   
//printf("result = %llu\n", result);
   
return result;
}

 
 ----------------------------------------------------------------------------------------------
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <string.h>
#include <math.h>
 
#define MAX_NUM 65001 
#define MIN_NUM 2 
 
int isCarMichaelFuc(int number);
unsigned long long myPow(int first, int second);
 
int main(void)
{
    int i, inputNumber, isPrime, isCarmichael;
    long long result;
 
    while(scanf("%d", &inputNumber) && inputNumber!=0)
    {
        isPrime = 1;
        for(i=MIN_NUM; i<inputNumber; i++)
        {
            if( inputNumber%i == 0 )
            {
                isPrime = 0;
                break;
            }
        }
        if( !isPrime )
        {
            isCarmichael = isCarMichaelFuc(inputNumber);
            if( !isCarmichael )
            {
                printf("%d is normal.\n", inputNumber);
            }
            else
            {
                printf("The number %d is a Carmichael number.\n", inputNumber);
            }
        }
        else
            printf("%d is normal.\n", inputNumber);
    }
    return 0;
}
 
int isCarMichaelFuc(int number)
{
    int i, isCarmichael=1;
    unsigned long long result;
    for(i=MIN_NUM; i<number; i++)
    {
        result = myPow(i, number);
        if( result != i) {
            isCarmichael = 0;
            break;
        }
    }
    return isCarmichael;
}
 
unsigned long long myPow(int first, int second)
{
    int i;
    unsigned long long result = first;
    for(i=1; i<second; i++)
    {
        result *= first;
        result %= second;
    }
    return result;
}
반응형

'ACM' 카테고리의 다른 글

[Accept] 10018 Reverse and Add  (0) 2009.06.08
[10018] Reverse and Add  (0) 2009.06.03
[Accept] 10006 Carmichael Numbers  (0) 2009.06.02
[Accept] 10110 Light, more light  (0) 2009.06.02
[Accept] 850 Crypt Kicker II  (0) 2009.06.02
posted by ssuk1010
:
ACM 2009. 6. 2. 21:08
반응형
I got accept from Judge Server.
But I don't like my code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <string.h>
 
int main(void)
{
    int i, inputNumber, isCarmichael;
    long long result;
    int table[15]={561,1105,1729,2465,2821,6601,8911,10585,15841,29341,41041,46657,52633,62745,63973};
 
    while(scanf("%d", &inputNumber) && inputNumber!=0)
    {
        isCarmichael = 0;
        for(i=0; i<15; i++)
        {
            if( table[i] == inputNumber )
            {
                isCarmichael = 1;
            }
        }
        if( isCarmichael )
            printf("The number %d is a Carmichael number.\n", inputNumber);
        else
            printf("%d is normal.\n", inputNumber);
    }
 
    return 0;
}
반응형

'ACM' 카테고리의 다른 글

[10018] Reverse and Add  (0) 2009.06.03
[Time Limit] Carmichael Numbers  (0) 2009.06.02
[Accept] 10110 Light, more light  (0) 2009.06.02
[Accept] 850 Crypt Kicker II  (0) 2009.06.02
[Accept] 10132 File Fragmentation  (0) 2009.06.02
posted by ssuk1010
:
ACM 2009. 6. 2. 21:05
반응형
I got accept from Judge Server.

----------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <math.h>
 
int main(void)
{
    unsigned long number, i, light=OFF;
    unsigned long maxNum = (unsigned long)(pow((double)2,(double)32)-1);
    while( scanf("%u", &number) && number )
    {
        if( number > maxNum ){
            //printf("!!! %u\n", number);
            return 0;
        }
 
        light = (unsigned long)sqrt(number);
        if( light*light == number )
            printf("yes\n");
        else
            printf("no\n");
 
        light = OFF;
    }
    return 0;
}

반응형

'ACM' 카테고리의 다른 글

[Time Limit] Carmichael Numbers  (0) 2009.06.02
[Accept] 10006 Carmichael Numbers  (0) 2009.06.02
[Accept] 850 Crypt Kicker II  (0) 2009.06.02
[Accept] 10132 File Fragmentation  (0) 2009.06.02
[Accept] 10099 - The Tourist Guide  (0) 2009.06.02
posted by ssuk1010
:
ACM 2009. 6. 2. 21:04
반응형

I got accept from Judge Server.

----------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <string.h>
 
#define MAX_CHAR    85    
#define MAX_LINE    101
#define PLAIN_TEXT    44
#define ALPHABET    26    
 
char replaceTable[ALPHABET];
char plainText[PLAIN_TEXT] = "the quick brown fox jumps over the lazy dog";
 
int makeReplaceTable(char inputText[PLAIN_TEXT]);
void changeInputLine(char inputText[]);
void initTable(void);
 
int main(void)
{
    int caseNum, lineNum=0, i=0, success=0, size=0;
    char inputLine[MAX_LINE][MAX_CHAR];
 
 
    scanf("%d\n", &caseNum);
    do {
        lineNum=0;
        success=0;
        caseNum--;
        while( gets(inputLine[lineNum]) && strlen(inputLine[lineNum]) )
        {
            lineNum++;
        }
 
        
        if( lineNum >= MAX_LINE ) 
        {
            lineNum = MAX_LINE-1;
        }
 
        for(i=0; i<lineNum; i++)
        {
            if( strlen(inputLine[i]) == strlen(plainText) )
            {
                if( makeReplaceTable(inputLine[i]) )
                {
                    success = 1;
                    break;
                }
            }
        }
 
        if( !success )
        {
            printf("%s\n", "No solution.");
        }
        else 
        {
            for(i=0; i<lineNum; i++)
            {
                changeInputLine(inputLine[i]);
                printf("%s\n", inputLine[i]);
            }
        }
 
        if( caseNum != 0 )
            printf("\n");
 
    } while(caseNum>0);
 
    return 0;
}
 
int makeReplaceTable(char inputText[PLAIN_TEXT])
{
    int i, c;
 
    initTable();
 
    for(i=0; i<PLAIN_TEXT; i++)
    {
        if( inputText[i] >= 'a' && inputText[i] <= 'z' )
        {
            c = inputText[i]-'a';
            if( replaceTable[c] == '\0' )
            {
                replaceTable[c] = plainText[i];
            }
            else
            {
                if ( replaceTable[c] != plainText[i] )
                {
                    return 0;
                }
            }
        }
    }
    
    for(i=0; i<ALPHABET; i++)
    {
        if( replaceTable[i] < 'a' || replaceTable[i] > 'z' )
        {
            return 0;
        }
    }
    return 1;
}
 
void changeInputLine(char inputText[])
{
    int index, i;
    int size = strlen(inputText);    
 
    for(i=0; i<size; i++)
    {
        if( inputText[i] != ' ' )
        {
            if( inputText[i] >= 'a' && inputText[i] <= 'z' )
            {
                index = inputText[i]-'a';
                inputText[i] = replaceTable[index];
            }
        }
    }
}
 
void initTable(void)
{
    int i;
    for(i=0; i<ALPHABET; i++)
    {
        replaceTable[i] = '\0';
    }
}

반응형

'ACM' 카테고리의 다른 글

[Accept] 10006 Carmichael Numbers  (0) 2009.06.02
[Accept] 10110 Light, more light  (0) 2009.06.02
[Accept] 10132 File Fragmentation  (0) 2009.06.02
[Accept] 10099 - The Tourist Guide  (0) 2009.06.02
[Accept] 10099 - The Tourist Guide  (0) 2009.06.02
posted by ssuk1010
:
ACM 2009. 6. 2. 21:03
반응형
I got accept form Judge Server

----------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
 
#define FILENUM            144
#define FILEFRAGMENT    2    
#define FILESIZE        256
#define FALSE            0
#define TRUE            1
 
char* getOriginalFile(int inputNum, int originalFileLen, int originalFileOneNum, int fileIndex[4]);
void findPair(int inputNum, int originalFileLen, int originalFileOneNum, int fileIndex[4]);
 
char fileInfo[FILENUM*FILEFRAGMENT][FILESIZE];
int fragmentLen[FILENUM*FILEFRAGMENT], fragmentOneNum[FILENUM*FILEFRAGMENT];
 
int main(void)
{
    char* originalFile;
    int caseNum, inputNum, i, j, originalFileLen, originalFileOneNum, count;
    int firstLen, secondLen, fileIndex[4];
 
    scanf("%d", &caseNum);
    if(caseNum>FILENUM-1) return 0;
    getchar();
    getchar();
    do{
 
        // initialize
        originalFileLen=0;
        originalFileOneNum=0;
        memset(fileInfo, 0x00, sizeof(fileInfo));
        memset(fragmentLen, 0x00, sizeof(fragmentLen));
        memset(fragmentOneNum, 0x00, sizeof(fragmentOneNum));
        memset(fileIndex, 0x00, sizeof(fileIndex));
 
        for(i=0; i<FILENUM*FILEFRAGMENT; i++) {
            fgets(fileInfo[i], 255, stdin);
 
            // size check
            fragmentLen[i]=strlen(fileInfo[i])-1;
            fileInfo[i][fragmentLen[i]] = '\0';
            if(fragmentLen[i]>=FILESIZE-1){
                printf("Your input size is more than 256\n");
                return 0;
            }
            if(!fragmentLen[i]) break;
        
            // 1의 개수를 구함 
            count=0;
            for(j=0; j<fragmentLen[i]; j++){
                if(fileInfo[i][j]=='1') count++;
            }
            fragmentOneNum[i] = count; 
        }
        inputNum = i;
 
        // 원래 파일 사이즈(originalFileLen)와 1의 개수(originalFileOneNum)을 구함)
        for(i=0; i<inputNum; i++) {
            originalFileLen        += fragmentLen[i];
            originalFileOneNum    += fragmentOneNum[i];
        }
        originalFileLen     = originalFileLen * 2 / inputNum;
        originalFileOneNum     = originalFileOneNum * 2 / inputNum;
        if(originalFileLen >= FILESIZE-1) return 0; // file size check
        
        findPair(inputNum, originalFileLen, originalFileOneNum, fileIndex);
        originalFile = getOriginalFile(inputNum, originalFileLen, originalFileOneNum, fileIndex);
        if(!originalFile) break; // file null check
 
        // result
        caseNum--;
        if(!caseNum) 
            printf("%s\n", originalFile); // 마지막 case일때
        else 
            printf("%s\n\n", originalFile); // 마지막 case가 아니면 한 줄 더 띄기
    } while(caseNum>0);
 
    return 0;
}
 
// find get the original File
char* getOriginalFile(int inputNum, int originalFileLen, int originalFileOneNum, int fileIndex[4])
{
    static char resultFile[4][FILESIZE];
    int i, j;
 
    // index를 조합해서 original file이 될 수 있는 후보를 찾음
    sprintf(resultFile[0], "%s%s", fileInfo[fileIndex[0]], fileInfo[fileIndex[1]]);
    sprintf(resultFile[1], "%s%s", fileInfo[fileIndex[1]], fileInfo[fileIndex[0]]);
    sprintf(resultFile[2], "%s%s", fileInfo[fileIndex[2]], fileInfo[fileIndex[3]]);
    sprintf(resultFile[3], "%s%s", fileInfo[fileIndex[3]], fileInfo[fileIndex[2]]);
 
    // 후보 중에 같은 것이 있으면, 그것이 original file
    if(!strncmp(resultFile[0], resultFile[2], originalFileLen) 
            || !strncmp(resultFile[0], resultFile[3], originalFileLen))    
        return resultFile[0];
    else if(!strncmp(resultFile[1], resultFile[2], originalFileLen) 
            || !strncmp(resultFile[1], resultFile[3], originalFileLen))    
        return resultFile[1];
    else
        return NULL;
}
 
 
// Fine 2 pair of file and save indexes;
void findPair(int inputNum, int originalFileLen, int originalFileOneNum, int fileIndex[4])
{
    int i, j;
    int findFlag=FALSE, count=0;
 
    for(i=0; i<inputNum; i++) {
        for(j=0; j<inputNum; j++) {
            if(i==j) continue;
 
            // 자신의 쌍을 찾았을 경우
            if(fragmentLen[i]+fragmentLen[j]==originalFileLen 
                    && fragmentOneNum[i]+fragmentOneNum[j]==originalFileOneNum)
            {
                // 자신의 쌍의 후보가 2개 이상일 경우(break)
                if(findFlag){ // pair is more than 2
                    fileIndex[count]=0;
                    fileIndex[count+1]=0;
                    findFlag = FALSE;
                    break;
                }
                // 자신의 쌍을 찾았을 경우 index를 저장
                else{   
                    fileIndex[count]=i;
                    fileIndex[count+1]=j;
                    findFlag = TRUE;
                }
            }
        }
 
        if(findFlag) {
            count+=2;
            if(count>=4) break;
        }
        findFlag = FALSE;
    }
    return;
}

반응형

'ACM' 카테고리의 다른 글

[Accept] 10110 Light, more light  (0) 2009.06.02
[Accept] 850 Crypt Kicker II  (0) 2009.06.02
[Accept] 10099 - The Tourist Guide  (0) 2009.06.02
[Accept] 10099 - The Tourist Guide  (0) 2009.06.02
[Time Limit] 10099 - The Tourist Guide  (0) 2009.06.02
posted by ssuk1010
: