기본 

if else

1
2
3
if (a % 2 ==0 ) return 1;
else return 0;
 
cs


for

1
2
3
4
5
6
int sum =0;
for (int i =1 ; i < n ; i++)
{
    sum += i ;
}
return sum;

cs



배열


C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <algorithm>
#include <vector>
 
using namespace std;
 
int getMaxNum (vector<int> array)
{
    int ret = array[0];
    for (int i =1 ; i< array.size(); i++)
    {
        if (ret < array[i]) ret = array[i];
        
    }
    return ret; 
}
cs




제출 형식


문제 : int 형의 매개변수 a,b가 주어질 때 a+b를 리턴하세요
class : AplusBProblem
method : public int calc(int a, int b)


C++


using namespace std;


class AplusBproblem

{    

public : 

int calc(int a, int b)

{

return a+b;

}

};




추가로 알면 편리한 지식



정렬

C++
#include <algorithm>

sort(array);



JAVA

inport java.util.*;

Arrays.sort(array);



C#

using System;


System.Array.Sort(array);




문자열 처리


C++
string s ="abc";

// 동일 판정
if(s == "abc") cout <<"equal" << endl;    

//문자 하나 추출
char c = s[1]; // 'b'

//문자열 연결
s = "def" + s + "ghi";    // "defabcghi"

//문자열 잘라내기
s = s.substr(3,3);   //"abc"



연관 배열 

C++ 이면 map 클래스 , java 라면 Hashmap 클래스 , C# 이면 Dictionary  클래스 등의 자료구조가 연관 배열 이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <map>
 
 
void countString(vector<string>s)
{
    map<stringint>m;
    for (int i =0; i< s.size(); i++)
    {    
        m[s[i]]++;
 
    }
    map<stringint> :: iterator it = m.begin();
    while(it != m.end())
    {
        cout << (*it).first << "" << (*it).second << endl;
        ++it;
    }
}
cs









+ Recent posts