2015. 2. 10. 18:25 Programing/Linux

vim 설정방법

보통은 root 계정을 이용하여 vi를 이용하기 때문에 root계정을 사용 할 경우 설정 방법이다.

 

터미널에서 su를 입력하여 root계정으로 로그인 한 뒤

# cat > .vimrc 

를 입력하여 새로운 파일을 생성하자(혹은 존재 할 수도 있다)

그 뒤 파일에 설정 명령어 들을 입력하면 된다.

 

set number //편집기에 라인 넘버 표시

set tabstop=4 //탭을 4칸으로

set shiftwidth=4 //자동 들여쓰기 4칸

set nobackup //백업 파일 만들지 않기

set hlsearch //검색어 강조

set cindent //c에 맞게 들여쓰기

set background=dark //하이라이팅

set ruler //커서 위치 표시

 

파일 저장 위치에 따라 설정이 저장 되지 않을 수도 있다.

vi편집기 자체에 콜론(:)을 붙여 설정 할 수도 있지만 매번 명령어를 입력 하기엔 너무 귀찮을 일이기에

설정 파일로 영구 설정이 가능하다.

'Programing > Linux' 카테고리의 다른 글

Linux 방화벽 설정  (0) 2016.01.11
ssh-keygen 키 인증  (0) 2016.01.11
Posted by kimmayer

문제: 소수 중 처음 6개를 나열하면 2, 3, 5, 7, 11, 13 이다. 이 때 6번째 소수는 13이다.

10001번째 소수는 무엇인가?


#include <stdio.h>

 

int main(){

    int count=0;

    int i;

    int j;

    for(i=2;;i++){

        for(j=2;j<=i;j++){

            if(i==j){

                count++;

                break;

            }

            else if((i%j)==0){

                break;

            }

            

        }

        if(count==10001) {break;}

    }

    printf("%d\n", i);


}

'Programing > Algorithm' 카테고리의 다른 글

순환/재귀 팩토리알 구현  (0) 2015.04.06
3개의 숫자를 내림차순으로 정렬  (0) 2015.04.06
프로젝트 오일러 6번  (0) 2015.02.10
프로젝트 오일러 5번  (0) 2015.02.10
프로젝트 오일러 4번  (0) 2015.02.10
Posted by kimmayer

문제: 1부터 10까지의 자연수의 제곱의 합은 아래와 같습니다.

12 + 22 + ... + 102 = 385

1부터 10까지의 자연수의 합의 제곱은 아래와 같습니다.

(1 + 2 + ... + 10)2 = 552 = 3025

1부터 10까지의 자연수의 합의 제곱과 제곱의 합의 차는 3025 - 385 = 2640 입니다.

1부터 100까지의 자연수의 합의 제곱과 제곱의 합의 차는 얼마인가요?



#include <stdio.h>



int main(){

    int a=0;

    int b=0;

    

    int i;

    for(i=1;i<=100;i++)

        a += (i*i);

    for(i=1;i<=100;i++)

        b += i;

    b = b*b;

    

    printf("%d\n", b-a);


}


1번보다 쉬운거 같습니다.

'Programing > Algorithm' 카테고리의 다른 글

3개의 숫자를 내림차순으로 정렬  (0) 2015.04.06
프로젝트 오일러 7번  (0) 2015.02.10
프로젝트 오일러 5번  (0) 2015.02.10
프로젝트 오일러 4번  (0) 2015.02.10
프로젝트 오일러 3번  (0) 2015.02.10
Posted by kimmayer

문제: 1 ~ 10 사이의 어떤 수로도 나누어 떨어지는 가장 작은 수는 2520입니다.

그러면 1 ~ 20 사이의 어떤 수로도 나누어 떨어지는 가장 작은 수는 얼마입니까?



문제를 푼 알고리즘이 있는데 코딩으로 적용시키는 것이 까다로워서 일단 알고리즘 설명만 남기도록 하겠습니다.


제가 푼 알고리즘은 

문제에서 제시된 2520을 소인수분해 하여 찾았습니다.


2520= 2^3 * 3^2 * 5 * 7

이며, 소인수분해를 보시면 1~10까지 소인수를 조합하여 모든 수를 표현 가능 합니다.

그럼 이 수에 11~20의 소수를 먼저 곱하고

X = 2^3 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19

11~20사이의 수를 표현 가능한지 확인 합니다.


2^4인 16이 표현 되지 못하는군요. 저 윗식에 2를 곱합니다.

X = 2^4 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19

소인수를 보시면 1~20의 수 모두 표현이 가능하므로 정답이 되겠습니다.



PS) 해답을 보면 제가 풀은 알고리즘을 예시로 들고 log를 이용하여 새로운 식(일반해?라고 해야할까요)을 정의합니다.

'Programing > Algorithm' 카테고리의 다른 글

프로젝트 오일러 7번  (0) 2015.02.10
프로젝트 오일러 6번  (0) 2015.02.10
프로젝트 오일러 4번  (0) 2015.02.10
프로젝트 오일러 3번  (0) 2015.02.10
프로젝트 오일러 2번  (0) 2015.02.10
Posted by kimmayer

문제: 앞에서부터 읽을 때나 뒤에서부터 읽을 때나 모양이 같은 수를 대칭수(palindrome)라고 부릅니다.

두 자리 수를 곱해 만들 수 있는 대칭수 중 가장 큰 수는 9009 (= 91 x 99) 입니다.

 

세 자리 수를 곱해 만들 수 있는 가장 큰 대칭수는 얼마입니까 ?

 

#include <stdio.h>
int main() {
int i, j;
int x;
int sol = 0;
int n1, n2, n3, n4, n5, n6;
for (i = 999; i > 100; i - )
	for (j = 999; j > 100; j - ){
		x = i * j;
		n1 = x / 100000;
		x = x % 100000;
		n2 = x / 10000;
		x = x % 10000;
		n3 = x / 1000;
		x = x % 1000;
		n4 = x / 100;
		x = x % 100;
		n5 = x / 10;
		x = x % 10;
		n6 = x / 1;
		if ((n1 == n6) && (n2 == n5) && (n3 == n4)){
			printf("n1 = %d,n2 = %d,n3 = %d,n4 = %d,n5 = %d,n6 = %d \n", n1, n2, n3, n4, n5, n6);
			x = i*j;
			if (x > sol){
				sol = x;
				break;
			}
		}
	}
printf("%d \n", sol);
}

3자리 x 3자리로 이루어진 가장 큰 대칭수는 6자리라고 단정 짓고 짠 알고리즘,

 

 

해답:

대칭수 P는 x,y,z로 구성 가능. P= a x b(a와 b는 100보다 크고 999보다 작은 정수)

P는 반드시 적어도 6자리로 긴 정수 일것이다. 왜냐하면 111111=143 x 777 이기 때문(P는 3자리 x 3자리로 이루어진 대칭수 111111보다 크거나 같을 것)

따라서,

 

P=100000x+10000y+1000z+100z+10y+x

P=100001x+10010y+1100z 

P=11(9091x+910y+100z)

 

11은 소수로 반드시 a, b는 11을 약수로 가짐

따라서 11로 나누어 지지 않는다면 대칭수가 아님.

 

largestPalindrome = 0

a = 999

while a >= 100

if a mod 11 = 0

b = 999

db = 1

else

b = 990 //The largest number less than or equal 999

 //and divisible by 11

db = 11

while b >= a

if a*b <= largestPalindrome

break

if isPalindrome(a*b)

largestPalindrome = a*b

b = b-db

a = a-1

output largestPalindrome

'Programing > Algorithm' 카테고리의 다른 글

프로젝트 오일러 6번  (0) 2015.02.10
프로젝트 오일러 5번  (0) 2015.02.10
프로젝트 오일러 3번  (0) 2015.02.10
프로젝트 오일러 2번  (0) 2015.02.10
Kadane's Algorithm  (0) 2015.02.10
Posted by kimmayer

문제: 어떤 수를 소수의 곱으로만 나타내는 것을 소인수분해라 하고, 이 소수들을 그 수의 소인수라고 합니다.

예를들면 13195의 소인수는 5, 7, 13, 29 입니다. 600851475143의 소인수 중에서 가장 큰 수를 구하세요.


#include <stdio.h>


int main()

{

 int i;

 unsigned long long int n = 600851475143;

 for (i = 2; i<=n; i++)

 {

  if (n%i == 0){

   printf("%d ", i);

   n = n/i;

   i = 2;

  }

 }

 return 0;

 

}


//문제의 답이 제대로 나오지 않았는데, INT형의 범위를 고려하지 않은것이 문제였다. unsigned long long으로 선언하지 않으면 제대로 된 답이 나오지 않는다.

'Programing > Algorithm' 카테고리의 다른 글

프로젝트 오일러 6번  (0) 2015.02.10
프로젝트 오일러 5번  (0) 2015.02.10
프로젝트 오일러 4번  (0) 2015.02.10
프로젝트 오일러 2번  (0) 2015.02.10
Kadane's Algorithm  (0) 2015.02.10
Posted by kimmayer

문제: 피보나치 수열의 각 항은 바로 앞의 항 두 개를 더한 것이 됩니다. 1과 2로 시작하는 경우 이 수열은 아래와 같습니다.

1, 2, 3, 5, 8, 13, 21, 34, 55, 89...

짝수이면서 4백만 이하의 모든 항을 더하면 얼마가 됩니까?


#include <stdio.h>


int main()

{

 int first = 1;

 int second = 2;

 int third = 0;

 unsigned int total = 2;

 int flag = 0;


 while (third < 4000000){

  third = first + second;

  if (third % 2 == 0) total += third;

  first = second;

  second = third;

 }


 printf("%d\n", total);

}

'Programing > Algorithm' 카테고리의 다른 글

프로젝트 오일러 6번  (0) 2015.02.10
프로젝트 오일러 5번  (0) 2015.02.10
프로젝트 오일러 4번  (0) 2015.02.10
프로젝트 오일러 3번  (0) 2015.02.10
Kadane's Algorithm  (0) 2015.02.10
Posted by kimmayer

n^3 알고리즘으로서 주어지는 행렬의 sub array들로 만들어지는 직사각형들 중 최대 합을 가지고 있는 직사각형을 구하는 알고리즘,

 

 

void main(){

 char c;

 string s;

 int n;

 int row, col;

 ifstream fin;

 fin.open("testcase100.txt");

 getline(fin, s);

 istringstream input(s);

 input >> row;

 col = row;

 n = row;

 int **pt = new int *[row];

 for(int i = 0; i < row; i++)

  pt[i] = new int[col];

 

 int *c_pt = new int [row];

 /*for(int i = 0; i < row; i++)

  c_pt[i] = new int[col];*/

 

 int temp = 0;

 

 for(int i=0; i < row; i++)

  for(int j = 0; j < col; j++)

  {

   fin >> temp;

   pt[i][j] = temp;

  }

  

     

  int sum = 0;

  int sum_2 = 0;

  int max_sum = 0; 

  

  

 

 int t = 0;

   int S = 1<<31, d = 0, k, l, x1 = 0,x2 = 0,y1 = 0,y2 = 0,j;

 

 

 for( int z = 0; z < n; z++)

    {

  for(int i = 0; i < n; i++) c_pt[i] = 0;

 

        for(int x = z; x < n; x++)

        {

            t = 0;

   d = 1<<31;

            j = 0;

            k = 0; l = 0;

   for(int i = 0; i < n; i++)

            {

    c_pt[i] = c_pt[i] + pt[x][i];

    t = t + c_pt[i];

    if( t > d)

                {

     d = t;

                    k = i;

                    l = j;

                }

                if( t < 0 )

                {

                    t = 0;

                    j = i + 1;

                }

            }

   if( d > S)

            {

    S = d;

                x1 = x;

                y1 = k;

                x2 = z;

                y2 = l;

            }

        }

    }

 

    cout << x1 << " " << y1 << " " << x2 << " "  << y2 << endl;

    cout << S;

 

    

}

'Programing > Algorithm' 카테고리의 다른 글

프로젝트 오일러 6번  (0) 2015.02.10
프로젝트 오일러 5번  (0) 2015.02.10
프로젝트 오일러 4번  (0) 2015.02.10
프로젝트 오일러 3번  (0) 2015.02.10
프로젝트 오일러 2번  (0) 2015.02.10
Posted by kimmayer

void main(){

 int row, col;

 cin >> row;

 col = row;

 int **pt = new int *[row];

 for(int i = 0; i < row; i++)

  pt[i] = new int[col];

 

 for(int i=0; i < row; i++)

  for(int j = 0; j < col; j++)

  {

   cin >> pt[i][j];

  }

 for(int i=0; i < row; i++){

  for(int j = 0; j < col; j++)

  {

   cout << pt[i][j];

  }

  cout << endl;

 }

 for(int i=0; i < row ;i++)

 {

  delete [] pt[i];

 }

 delete [] pt;

}

 

이 예제는 n*n 행렬을 생성하기 위해서 row 값과 col 값을 같은 값으로 넣어 주었습니다.

다른 값을 입력 하려면 새로 col 값을 받고

row = col; 문을 없애면 되겠지요

'Programing > C++' 카테고리의 다른 글

구조체로 구현한 Invader  (0) 2015.06.09
클래스 template  (0) 2015.04.27
함수 Template  (0) 2015.04.27
Tree 구현하기  (0) 2015.02.10
C++ 문자형을 int형으로  (0) 2015.02.10
Posted by kimmayer

2015. 2. 10. 18:16 Programing/C++

Tree 구현하기

#include <iostream>

using namespace std;


char input[200];

char heap[200];

int cnt_1=0;

int cusor = 1;

int depth_cnt = 1;

int depth_check = 0;

int depth_temp;

int depth = 0;

int k_1 = 1;

int k = 1;//초기값 1

void print_1(int _n);

void print_2(int _n);


int jegop(int _n)

{

 int k = 1;

 int n = _n;

 for(int i = 0 ; i < n; i++)

  k *= 2;

 return k;

}

void ancestor_sort(int _i, int _j)

{

 int i = _i; //i보내는거

 int n = _j; //cusor 보내는거

 int cnt=0;

 char temp;

 if(cusor != 0)

 {

  if((i%2) == 1) //홀수고 왼쪽 노드

  {

   cusor = ((i-1)/2);

   if(heap[i] < heap[cusor])

   {

    temp = heap[cusor];

    heap[cusor] = heap[i];

    heap[i] = temp;

    ancestor_sort(cusor, cusor);

   }

   }

  else if((i%2) == 0) //짝수고 오른쪽 노드

  {

   cusor = ((i-2)/2);

    if(heap[i] < heap[cusor])

   {

    temp = heap[cusor];

    heap[cusor] = heap[i];

    heap[i] = temp;

    ancestor_sort(cusor, cusor);

   }

  }

 }

 cusor = 1;

}

void sort(char *_c,int _arr_size) //'A'랑 'D'는 없는 상태

{

 int arr_size = _arr_size;

 int n = (arr_size/3)+1;

 for(int i = n-1 ; i >= 0; i--)

 {

  ancestor_sort(i,1);

 }

}

void insert(char *_c, int _arr_size) //들어온게 input

{

 char *c = _c;

 int arr_size = _arr_size;

 int count = 0;

 for(int i = 0; i < arr_size; i++)

 {

  if(c[i] == 'A')

  {

   if(c[i+1] != ' '){

   heap[count] = c[i+1];

   count ++;

   }

  }

  else if (c[i] == 'D')

  {

   if(c[i+1] != ' ')

   for(int j=0; j <arr_size; j++)

    if(c[i+1] == heap[j])

     heap[j] = NULL;

  }

 }

 sort(heap, arr_size);

}

void main()

{

 int n;

 cin >> n;

// char input[500]; //입력받은 문자열들이 저장되는 배열

 int i_int; //getchar를 받기 위해 있는 변수

 int arr_size = (n * 3)-1;

 int cnt=0;

 fflush(stdin);

 while((i_int=getchar()) != '\n')

 {

  input[cnt] = i_int;

  cnt++;

 }

 insert(input, arr_size); //cnt 안받기로 했음

 

 cout<<"Original Form"<<endl;

 print_1(n);

}


void print_1(int _n)

{

 int n = _n;

 for(int i = 0; i < n; i++)

 {

  if(jegop(i) <= n)

   depth = i;

 }

 cout << depth <<endl;

 int depth_save = depth;

 int arr_count=0;

 int count = 0;

 

 char **arr = new char *[jegop((depth+1))-1];

 for(int i = 0; i < (jegop(depth+1))-1; i++)

  arr[i] = new char[depth];


 for(int i = 0 ; i <= depth; i++)

  for(int j =0; j<= jegop(depth+1)-1; j++)

   arr[i][j] = ' ';



 int cnt=0;

 int depth_check = 0;

 int d1, d2, d3;

 d1 = jegop(depth) -1;

 

 for(int i = 0; i < n ; i++)

 {

  if(depth_check == 0)

  {

   arr[count][d1] = heap[i];

   cnt++;

   depth_check ++;

   }

  else 

  {

   arr[count][d1+d2] = heap[i];

   d2 = d2 + d3;

   cnt++;

  }


  if(cnt == k)

  {

   d2 = d1+1;

   d3 = d2;

      depth--;

   d1 = jegop(depth)-1;

   depth_check = 0;

   cnt = 0;

   count++;

   k = jegop(k); 

  }

  

  

 }

   /*  cout << endl;

  char **arr_1 = new char *[depth];

     for(int i = 0; i < depth; i++)

  arr_1[i] = new char[jegop((depth+1))-1];

  */



 for(int i = 0 ; i <= depth_save; i++)

 {

  for(int j =0; j<= jegop(depth_save+1)-1; j++)

  {

   cout << arr[i][j];

  } 

  cout << endl;

 }

 cout << endl;

 cout << "Lotation Form" << endl;

  for(int i = 0 ; i <= jegop(depth_save+1)-1; i++)

  {

   for(int j =0; j<= depth_save; j++)

  {

   cout<< arr[j][i];

  } 

  cout << endl;

     }


 

 



}

'Programing > C++' 카테고리의 다른 글

구조체로 구현한 Invader  (0) 2015.06.09
클래스 template  (0) 2015.04.27
함수 Template  (0) 2015.04.27
이차원 동적 배열 입력 받아 생성하기  (0) 2015.02.10
C++ 문자형을 int형으로  (0) 2015.02.10
Posted by kimmayer
이전버튼 1 2 3 4 5 6 이전버튼

블로그 이미지
IT 기술들 정리, 독후감을 주로 남깁니다!
kimmayer

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.3
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

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함