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

MISPELL

language/알고리즘 2016. 7. 19. 00:16
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

MISPELL 

Misspelling is an art form that students seem to excel at. Write a program that removes the nth
character from an input string.

The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.

Each dataset consists of a single line of input containing M, a space, and a single word made up of uppercase letters only. M will be less than or equal to the length of the word. The length of the word is guaranteed to be less than or equal to 80.

For each dataset, you should generate one line of output with the following values: The dataset

number as a decimal integer (start counting at one), a space, and the misspelled word. The
misspelled word is the input word with the indicated character deleted.

(출처: 알고스팟, https://algospot.com/judge/problem/read/MISPELL)


문제 푸는 방식은 다양하다.

나는 입력받은 숫자 M에 해당하는 문자열 순서 이후의 문자열들을 한칸 앞으로 끌어와 덮어씌우고

최종적으로 완성된 문자열들을 출력하는 방식을 사용했다.

다른 사람의 경우, 아예 해당 문자열 부분은 출력하지 않고 이어서 출력하도록 구현한 것을 볼 수 있었다.


#include <iostream>

#include <cstring>

 

using namespace std;

 

int main()

{

                  int T, i=1;

                 

                  int count, length;

                  char str[81]={NULL};

 

                  cin>>T;

                 

                  while(T--){

                                   cin>>count>>str;

                                   length=strlen(str);

                 

                                   cout << i << " ";

 

                                   for(int j=count-1; j<=length-1; j++){

                                                     str[j]=str[j+1];

                                   }

                                   for(int j=0; j<=length-1; j++){

                                                     cout<<str[j];

                                   }

                                   cout<<endl;

                                   i++;

                  }

                  system("pause");

                  return 0;

}


#include <iostream>

#include <cstring>

 

using namespace std;

 

int main()

{

                  int T, i=1;

                 

                  int count, length;

                  char str[81]={NULL};

 

                  cin>>T;

                 

                  while(T--){

                                   cin>>count>>str;

                                   length=strlen(str);

                 

                                   cout << i << " ";

 

                                   for(int j=count-1; j<=length-1; j++){

                                                     str[j]=str[j+1];

                                   }

                                   for(int j=0; j<=length-2; j++){

                                                     cout<<str[j];

                                   }

                                   cout<<endl;

                                   i++;

                  }

                  system("pause");

                  return 0;

}


위 두 코드들은 거의 같지만 단 한 글자만 다르다. 위는 오답, 아래는 정답으로 인정되었다.


length-1의 경우, 보이지 않는 NULL 문자열 또한 같이 출력되기 때문에 

화면 상으로는 보이지 않더라도 정답으로 인정하지 않는 것 같다.





'language > 알고리즘' 카테고리의 다른 글

WEEKLYCALENDAR  (0) 2016.07.31
XOR 연산자를 이용한 Swap  (0) 2016.07.18
ENDIANS  (0) 2016.07.18
알고리즘: 효율, 분석 그리고 차수  (0) 2016.07.10
블로그 이미지

saylin

,
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

1. 교환법칙 : A^B=B^A

2. 결합법칙: (A^B)^C = A^(B^C)

3. 항등원 존재 : A^0 = 0^A = A

4. 역원 존재 A^A1=0


위의 4가지는 모든경우의 수를 대입하면 증명할 수 있습니다.


1.교환법칙 증명

모든 경우의수 AB

00 >>>>> 같으므로 생략

01 >>>>>  0^1 = 1,  1^0 = 1  좌측, 우측 같음 

10 >>>>>  1^0 = 1,  0^1 = 1  좌측, 우측 같음

11 >>>>> 같으므로 생략

모든 경우 같으므로 증명됨


2.결합법칙 증명

모든 경우의 수 ABC

000 >>>>> 같으므로 생략

001 >>>>> (0^0)^1 = 1, 0^(0^1)= 1 좌측, 우측 같음

010 >>>>> (0^1)^0 = 1, 0^(1^0)= 1 좌측, 우측 같음

011 >>>>> (0^1)^1 = 0, 0^(1^1)= 0 좌측, 우측 같음

100 >>>>> (1^0)^0 = 1, 1^(0^0)= 1 좌측, 우측 같음

101 >>>>> (1^0)^1 = 0, 1^(0^1)= 0 좌측, 우측 같음

110 >>>>> (1^1)^0 = 0, 1^(1^0)= 0 좌측, 우측 같음

111 >>>>> 같으므로 생략

모든경우 같으므로 증명됨


3. 항등원 존재

모든 경우의 수  A

0 >>>>>  0^0 = 0^0 = 0,  같음

1 >>>>>  1^0 = 0^1 = 1, 같음

모든 경우 같으므로 항등원 0 존재 증명됨



4. 역원

모든 경우의 수  A

0 >>>>>  0^0 = 0, 

1 >>>>>  1^0 = 1 ,

항등원 0의 역원 A1  는 A 자신임






위의 사실을 바탕으로 XOR SWAP 알고리즘을 살펴보면

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


순서대로 0, 1, 2, 3 살펴보면



0. 처음 상태 ----------------A----------------B----------------


1.     A=A^B---------------- A^B ------------- B----------------


2.     B=B^A ----------------A^B -----------B^(A^B)-------------



B^(A^B)      는

B^(B^A)  교환법칙 적용

(B^B)^A  결합 법칙 적용, 역원이 자기 자신이니 (B^B)= 0

0^A 항등원이 존재하므로

A 가 됩니다.



3. A=A^B ----------------(A^B)^A----------------A---------------


(A^B)^A  는

(B^A)^A 교환법칙

B^(A^A) 결합법칙 , 역원이 자기 자신이니 (A^A=0)

B^0 항등원이 존재하므로 이므로

B 가 됩니다.



     결과 ----------------B---------------- A----------------



스왑이 일어나는것을 확인 할 수 있습니다.



(출처: http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040101&docId=230636611&qb=YysrIHhvcg==&enc=utf8&section=kin&rank=1&search_sort=0&spq=0&pid=SGwBNsoRR1dssvp6tX4sssssssG-168491&sid=kKuYgjVn5WDRDdonE69m3g%3D%3D)



'language > 알고리즘' 카테고리의 다른 글

WEEKLYCALENDAR  (0) 2016.07.31
MISPELL  (0) 2016.07.19
ENDIANS  (0) 2016.07.18
알고리즘: 효율, 분석 그리고 차수  (0) 2016.07.10
블로그 이미지

saylin

,
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

runtime check failure #2 - stack around the variable 'XXX' was corrupted



#include <iostream>

using namespace std;

 

void drawrect();

 

int main(){

                  int cases;

                  cin >> cases;

 

                  while(cases--){

                                   drawrect();

                  }

                  system("pause");

                  return 0;

}

 

void drawrect()

{

                  int point[3][2];

                  int x=0, y=0;

                 

                                   for(int i=1; i<=3; i++){

                                                     cin >> point[i][1] >> point[i][2];

                                   }

                                   for(int i=2; i<=3; i++){

                                                     if(point[1][1] == point[i][1]) x = point[5-i][1]; }

                                   if(x==0) x=point[1][1];

                                  

                                   for(int i=2; i<=3; i++){

                                                     if(point[1][2] == point[i][2]) y = point[5-i][2]; }

                                   if(y==0) y=point[1][2];

 

                                   cout<< x << " " << y <<endl;

 

}



위와 같은 코드를 컴파일 및 실행 시, 코드 자체는 문제가 없으나 실행 중간에 runtime check failure #2가 발생한다.


정확한 이유는 모르겠으나, 배열크기를 늘려주면 해결할 수 있다는 말에 따라


                  int point[3][2]; -> point[4][3];


으로 변경하니 에러 문제는 해결되었다.



'language > debug' 카테고리의 다른 글

error C4703  (0) 2016.08.07
_CRT_SECURE_NO_WARNINGS  (0) 2016.05.07
블로그 이미지

saylin

,

ENDIANS

language/알고리즘 2016. 7. 18. 06:21
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

ENDIANS

The two island nations Lilliput and Blefuscu are severe rivals. They dislike each other a lot, and the most important reason was that they break open boiled eggs in different ways.

People from Lilliput are called little-endians, since they open eggs at the small end. People from Blefuscuare called big-endians, since they open eggs at the big end.

This argument was not only about their breakfasts but about their computers, too. Lilliput and Blefuscu's computers differed in the way they store integers; they stored the bytes in different orders. Computers from Lilliput(*little-endians*) ordered it from the LSB(least significant byte) to the MSB(most significant byte), and computers from Blefuscu was exactly the opposite.

For example, a 32-bit unsigned integer 305,419,896 (0x12345678) would be saved in two different ways:

For example, a 32-bit unsigned integer 305,419,896 (0x12345678) would be saved in two different ways:

  • 00010010 00110100 01010110 01111000 (in an Blefuscu computer)
  • 01111000 01010110 00110100 00010010 (in an Lilliput computer)

Given some 32-bit unsigned integers retrieved in a wrong endian, write a program to do a conversion to find the correct value.


The first line of the input file will contain the number of test cases, C (1 ≤ C ≤ 10, 000). Each test case contains a single 32-bit unsigned integer, which is the data without endian conversion.

For each test case, print out the converted number.

(출처: 알고스팟, https://algospot.com/judge/problem/read/ENDIANS)


비트 연산은 자주 사용해보지 않아서 이해하고 구현하는데 약간의 시간이 걸렸다.

위와 같은 연산을 구현하기 위해서 다음과 같이 실험적인 코드 작성 및 컴파일을 수행했다.



#include <iostream>

#include <bitset>

using namespace std;

 

int main(){

                  //int cases;

                  unsigned int x=2018915346;

                  //cin >> cases;

 

                  unsigned int a, b, c, d, e;

 

                  a = (x>>24);

                  b = (x<<8)>>24<<8;

                  c = (x>>8)<<24>>8;

                  d = (x<<24);

                 

                  cout<< bitset<32>(x) <<endl;

                  cout<< bitset<32>(a) <<endl;

                  cout<< a <<endl;

                  cout << bitset<32>(b) << endl;

                  cout<< b <<endl;

                  cout << bitset<32>(c) << endl;

                  cout<< c <<endl;

                  cout << bitset<32>(d) << endl;

                  cout<< d <<endl;

                                   //while(cases--){

                                                     e=a+b+c+d;

                                                     cout <<e <<endl;

                                   //}

                  getchar();

}




출력된 결과 화면



위 결과를 토대로 다음과 같이 간단한 코드로 변환했다.


#include <iostream>

using namespace std;

 

int main(){

                  int cases;

                  unsigned int x;

                  cin >> cases;


                  while(cases--){

                                   cin >> x;

                                   cout <<(x>>24)+((x<<8)>>24<<8)+((x>>8)<<24>>8)+(x<<24) <<endl;

                  }

}



간단한 코드임에도 실행 시간이 58ms 이었다. 

이보다 더 긴 코드임에도 불구하고 실행시간이 5ms인 경우도 있는걸 보니, 위 코드가 매우 효율적이지는 않은 것 같다.





'language > 알고리즘' 카테고리의 다른 글

WEEKLYCALENDAR  (0) 2016.07.31
MISPELL  (0) 2016.07.19
XOR 연산자를 이용한 Swap  (0) 2016.07.18
알고리즘: 효율, 분석 그리고 차수  (0) 2016.07.10
블로그 이미지

saylin

,