본문 바로가기
c++/프로그래머스

[프로그래머스] 수 조작하기1(c++)

by ilp 2023. 12. 27.
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/181926

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr



#include <bits/stdc++.h>
using namespace std;

int solution(int n, string control) {
    
    for(int i=0;i<control.size();i++){
        if(control[i]=='w'){
            n+=1;
        }
        else if(control[i]=='s'){
            n-=1;
        }
        else if(control[i]=='d'){
            n+=10;
        }
        else if(control[i]=='a'){
            n-=10;
        }
    }
    return n;
}

control의 크기만큼 반복문을 돌리고

차례대로 w,s,d,a인지 판단하고 그에 따른 작업을 수행한다.

그런데

if(control[i]=="w"){

}

if문에서 'w'로 하면 되는데 "w"로 하면 안돼니다.

이유는 모르겟다.


반응형