http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=73&sca=10b0
JUNGOL | 함수1 - 형성평가4 > 문제은행
두 개의 음이 아닌 정수를 입력받아 큰 수의 제곱에서 작은 수의 제곱을 뺀 결과값을 출력하는 프로그램을 작성하시오. (두 정수를 전달받아 제곱의 차를 리턴하는 함수를 이용할 것)
www.jungol.co.kr
#include <bits/stdc++.h>
using namespace std;
/*
함수1 - 형성평가4
*/
int f(int a,int b){
if(a>b){
return (a*a)-(b*b);
}
else{
return (b*b)-(a*a);
}
}
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d ",f(a,b));
return 0;
}
abs()함수 사용을 개선
https://en.cppreference.com/w/cpp/numeric/math/abs
std::abs, std::labs, std::llabs, std::imaxabs - cppreference.com
int abs( int n ); long abs( long n ); long long abs( long long n ); (since C++11) long labs( long n ); long long llabs( long long n ); (since C++11) (since C++11) (since C++11) Computes the absolute value of an integer number. The behavior
en.cppreference.com
#include <bits/stdc++.h>
using namespace std;
/*
함수1 - 형성평가4
*/
int f(int a,int b){
/*
if(a>b){
return (a*a)-(b*b);
}
else{
return (b*b)-(a*a);
}
*/
return abs((a*a)-(b*b));
}
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d ",f(a,b));
return 0;
}
pow()함수 사용을 추가 개선
https://en.cppreference.com/w/cpp/numeric/math/pow
std::pow, std::powf, std::powl - cppreference.com
(1) float pow ( float base, float exp ); float powf( float base, float exp ); (since C++11) double pow ( double base, double exp ); (2) (3) long double pow ( long double base, long double exp ); long double powl( long double base, long dou
en.cppreference.com
#include <bits/stdc++.h>
using namespace std;
/*
함수1 - 형성평가4
*/
int f(int a,int b){
/*
if(a>b){
return (a*a)-(b*b);
}
else{
return (b*b)-(a*a);
}
*/
return abs(pow(a,2)-pow(b,2));
}
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d ",f(a,b));
return 0;
}
'정보올림피아드-KOI > 기초 문법 문제' 카테고리의 다른 글
579 : 함수2 - 자가진단1 (0) | 2020.02.16 |
---|---|
174 : 함수1 - 형성평가5 (0) | 2020.02.16 |
172 : 함수1 - 형성평가3 (0) | 2020.02.16 |
함수3 - 자가진단2 (0) | 2020.02.09 |
함수3 - 자가진단1 (0) | 2020.02.09 |