배열을 안쓰고 7개의 숫자를 받으면 4번째로 큰 수를 cout 하는 함수를 이용한 프로그램을 짜는 건데요.
제가 코딩하다가 어디선가 꼬여서 cout을 구석구석 넣었습니다.
짠 건 이렇구요.
#include <iostream>
using namespace std;
int Mid(int, int, int, int, int, int, int);
int main(){
int a, b, c, d, e, f, g;
cin >> a >> b >> c >> d >> e >> f >> g;
cout << Mid(a, b, c, d, e, f, g);
return 0;
}
int Mid(int a, int b, int c, int d, int e, int f, int g)
{
int max = -2147483647;
for (int n = 1; n != 4; n++)
{
if (max < a)
{
max = a;
cout << "update:" << max << endl;
}
if (max < b)
{
max = b;
cout << "update:" << max << endl;
}
if (max < c)
{
max = c;
cout << "update:" << max << endl;
}
if (max < d)
{
max = d;
cout << "update:" << max << endl;
}
if (max < e)
{
max = e;
cout << "update:" << max << endl;
}
if (max < f)
{
max = f;
cout << "update:" << max << endl;
}
if (max < g)
{
max = g;
cout << "update:" << max << endl;
}//일곱 숫자중 최댓값 정하기
if (max = a)
{
a = -2147483647;
max = a; cout << "max reset\n";
}
if (max = b)
{
b = -2147483647;
max = b; cout << "max reset\n";
}
if (max = c)
{
c = -2147483647;
max = c; cout << "max reset\n";
}
if (max = d)
{
d = -2147483647;
max = d; cout << "max reset\n";
}
if (max = e)
{
e = -2147483647;
max = e; cout << "max reset\n";
}
if (max = f)
{
f = -2147483647;
max = f; cout << "max reset\n";
}
if (max = g)
{
g = -2147483647;
max = g; cout << "max reset\n";
}//제일 큰 숫자 없애기
}
if (max < a)
{
max = a;
cout << "update:" << max << endl;
}
if (max < b)
{
max = b;
cout << "update:" << max << endl;
}
if (max < c)
{
max = c;
cout << "update:" << max << endl;
}
if (max < d)
{
max = d;
cout << "update:" << max << endl;
}
if (max < e)
{
max = e;
cout << "update:" << max << endl;
}
if (max < f)
{
max = f;
cout << "update:" << max << endl;
}
if (max < g)
{
max = g;
cout << "update:" << max << endl;
}
return max;
}
변수를 받으면 최댓값을 찾고, 함수에서 그 최댓값을 제외하고 다시 나머지 변수들끼리 비교해서 최댓값을 3번을 다시구하는 이런 식으로 짰거든요..
if (max = a)
{
a = -2147483647; }
이렇게 말고 다른 방식으로는 제외하는 방법이 없을까요..?
모든 경우의 수를 생각해서 if안에 if쓰는 경우는 제외하고싶어서 이런 식으로 짰어요 ㅠㅠ
실행시키면 제일 큰 숫자 없애기 부분을 다 실행하고 가네요! 이건 또 왜이럴까요 !