안녕하세요. 우당탕탕 개발일지 입니다. 꾸준히 문제 풀기 다시 시작해보겠습니다.
codetree가 마음에 들어 순서대로 풀 것 같습니다. 아자아자 화이팅!
<프로그래밍 연습>
1.함수
2. 재귀함수
3. 정렬
4. 시뮬레이션1,2
5. 완전 탐색 1,2,3
6.케이스별로 나누기
7.Ad-Hoc
__조건에 따라 "Yes" 또는 "No" 반환__ 반환 타입: string
일의 자리수 구하기 : n%10
십의 자리수 구하기: n/10
#include <iostream>
#include<string>
using namespace std;
string func(int a)
{
if(a%2==0||a%4==0||a%6==0||a%8==0)
{
if(((a/10)+a%10)%5==0)
{
return "Yes";
}
else{
return "No";
}
}
else
{
return "No";
}
}
int main() {
// 여기에 코드를 작성해주세요.
int n;
cin>>n;
cout<<func(n);
return 0;
}
__함수를 이용한 369게임__
함수 마지막에 return 0(=return false)를 안해서 22~33까지 12개라고 error가 났다.
#include <iostream>
using namespace std;
bool func(int a)
{
if(a==3||a==6||a==9)
{
return 1;
}
else if(a%3==0)
{
return 1;
}
else{
while(a>0)
{
if(a%10==3||a%10==6||a%10==9)
{
return 1;
break;
}
a=a/10;
}
}
return 0;
}
int main() {
// 여기에 코드를 작성해주세요.
int n,m;
cin>>n>>m;
int cnt=0;
for(int i=n; i<=m; i++)
{
if(func(i))
{
cnt++;
}
}
cout<<cnt;
return 0;
}
__예제) 출력 결과51__ 숫자가 오름차순인 경우에만 true 반환.
int Lee(int x)
{
int last = 10;
while (x){
if (x % 10 > last) return 0;
last = x % 10;
x /= 10;
}
return 1;
}
__예제) 출력 결과22__출력 결과를 쓰시오 (정답) 61
int f(int x, int L) {
return 2 * L - x;
}
int main() {
int i, x = 1;
for (i = 0; i < 6; i++) {
x = f(x, -i);
x = f(x, i);
}
cout << x;
return 0;
}
__함수를 이용한 합과 함수 판별__2개의 함수로 구현
#include <iostream>
using namespace std;
bool prim(int n)
{
if(n==1){return n;}
for(int j=2; j<n; j++)
{
if(n%j==0)
{
return 0;
}
}
return 1;
}
bool even(int m)
{
if(((m/10)+(m%10))%2==0)
{
return 1;
}
return 0;
}
int main() {
// 여기에 코드를 작성해주세요.
int a,b;
cin>>a>>b;
int cnt=0;
for(int i=a; i<=b; i++)
{
if(prim(i) && even(i))
{
cnt++;
}
}
cout<<cnt;
return 0;
}
실력) 함수를 이용한 연속부분수열 여부 판단하기
#include <iostream>
using namespace std;
static int Arr[100];
static int Brr[100];
static int n1,n2;
bool func(int n)
{
for(int k=0; k<n2; k++)
{
if(Arr[k+n]!=Brr[k])
return 0;
}
return 1;
}
bool fun2()
{
for(int i=0; i<=n1-n2; i++)
{
if(func(i))
{
return 1;
}
}
return 0;
}
int main() {
// 여기에 코드를 작성해주세요
cin>>n1>>n2;
for(int i=0; i<n1; i++)//A
{
cin>>Arr[i];
}
for(int j=0; j<n2; j++)//B
{
cin>>Brr[j];
}
if(fun2())
{
cout<<"Yes";
}
else
{
cout<<"No";
}
return 0;
}
실력) 그 계절, 그날
#include <iostream>
using namespace std;
static int y,m,d;
bool year(int y)
{
if(y%4==0)
{
if(y%100==0)
{
if(y%400==0)
{
return 1;
}
return 0;
}
return 1;
}
return 0;
}
void mou31(int y, int m, int d)
{
if(m==12 || m==1)
{
cout<<"Winter";
}
else if(m==3||m==5)
{
cout<<"Spring";
}
else if(m==7 ||m==8)
{
cout<<"Summer";
}
else if(m==10)
{
cout<<"Fall";
}
}
void mou30(int y, int m, int d)
{
if(m==4)
{
cout<<"Spring";
}
else if(m==6)
{
cout<<"Summer";
}
else if(m==9||m==11)
{
cout<<"Fall";
}
}
int main() {
// 여기에 코드를 작성해주세요.
cin>>y>>m>>d;
if(m==2)
{
if(year(y) && d<=29)
{
cout<<"Winter";
}
else if(!year(y)&& d<=28)
{
cout<<"Winter";
}
else
{
cout<<"-1";
}
}
else if((m==12||m==1||m==5||m==7||m==8||m==10||m==3) && d<=31 )
{
mou31(y,m,d);
}
else if((m==4||m==6||m==9||m==11) && d<=30)
{
mou30(y,m,d);
}
else
{
cout<<"-1";
}
return 0;
}
728x90
'[알고리즘] > CodeTree' 카테고리의 다른 글
[codetree] 프로그래밍 연습 _정렬된 숫자 위치 알아내기 C++ (0) | 2025.01.29 |
---|---|
[codetree] 프로그래밍 연습 _재귀함수 (0) | 2024.12.11 |
[codetree] 프로그래밍 연습_변수의 영역 (1) | 2024.11.14 |
[codetree] 프로그래밍 연습_ call by value/ call by reference (메모리 초과 해결) (1) | 2024.11.12 |
[codetree] 프로그래밍 연습_ 값을 반환하지 않는 함수 (1) | 2024.11.06 |