[Baekjoon] - 9단계 : 기본 수학 2
1. 소수 찾기
#include <stdio.h>
int main(){
int t;
scanf("%d",&t);
int count = 0;
for(int i= 0; i<t;i++){
int n;
scanf("%d",&n);
int isPrime = 1;
if(n==1 || n==0) continue;
else if(n==2){
count ++;
continue;
}
for(int j=2;j<=n/2;j++){
if(n%j==0) {
isPrime = 0;
break;
}
}
if(isPrime) count ++;
}
printf("%d",count);
}
2. 소수
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
int main() {
// insert code here...
int m,n;
scanf("%d %d",&m,&n);
bool isPrime[100001];
for(int i =2;i<=n;i++){
isPrime[i] = true;
}
for(int i =2;i<=sqrt(n);i++){
if(!isPrime[i]) continue;
for(int j=i*2;j<=n;j+=i){
isPrime[j] = false;
}
}
int sum = 0;
int min = n;
for(int i=n;i>=m;i--){
if(isPrime[i]){
sum += i;
min = i;
}
}
if(sum == 0)
printf("-1");
else
printf("%d\n%d",sum,min);
return 0;
}
3. 소인수분해
#include <stdio.h>
int main() {
int n;
scanf("%d",&n);
if(n == 1) return 0;
int div = 2;
while(n != 1){
if(n%div== 0){
printf("%d\n",div);
n /= div;
div = 2;
}
else div++;
}
return 0;
}
4. 소수 구하기
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
int main() {
// insert code here...
int m,n;
scanf("%d %d",&m,&n);
bool isPrime[1000001];
for(int i =2;i<=n;i++){
isPrime[i] = true;
}
for(int i =2;i<=sqrt(n);i++){
if(!isPrime[i]) continue;
for(int j=i*2;j<=n;j+=i){
isPrime[j] = false;
}
}
for(int i=m;i<=n;i++){
if(isPrime[i]) printf("%d\n", i);
}
return 0;
}
5. 베르트랑 공준
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
int main() {
bool isPrime[123457*2] = {false,};
for(int i=2;i<123456*2;i++) isPrime[i] = true;
for(int i= 2;i<=sqrt(123456*2);i++){
if(!isPrime[i]) continue;
for(int j=i*2;j<=123456*2;j+=i){
isPrime[j] = false;
}
}
int n;
while(scanf("%d",&n) != EOF){
if(n == 0) return 0;
int count = 0;
for(int i=n+1;i<=n*2;i++){
if(isPrime[i]) count++;
}
printf("%d\n",count);
}
return 0;
}
6. 골드바흐의 추측
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
int main() {
bool isPrime[10001] = {false,};
for(int i=2;i<10000;i++) isPrime[i] = true;
for(int i=2;i<sqrt(10000);i++){
if(!isPrime[i]) continue;
for(int j=i*2;j<=10000;j+=i){
isPrime[j] = false;
}
}
int t;
scanf("%d",&t);
int n;
for(int i=0;i<t;i++){
scanf("%d",&n);
int temp = n;
temp /= 2;
for(int j=temp;j>=2;j--){
if(isPrime[j] && isPrime[n-j]){
printf("%d %d\n",j,n-j);
break;
}
else continue;
}
}
return 0;
}
7. 직사각형에서 탈출
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
int main() {
int x,y,w,h;
scanf("%d %d %d %d",&x,&y,&w,&h);
int min = 1000;
if(min > h-y)
min = h-y;
if(min > y)
min = y;
if(min > w-x)
min = w-x;
if(min > x)
min = x;
printf("%d",min);
return 0;
}
8. 네 번째 점
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
struct arr {
bool x[1001];
bool y[1001];
};
int main() {
struct arr a1;
for(int i=0;i<1001;i++){
a1.x[i] = false;
a1.y[i] = false;
}
int a,b;
int sumX = 0;
int sumY = 0;
for(int i=0;i<3;i++){
scanf("%d %d",&a,&b);
if(!a1.x[a]) sumX += a;
else sumX -= a;
a1.x[a] = a1.x[a] ? false : true;
if(!a1.y[b]) sumY += b;
else sumY -= b;
a1.y[b] = a1.y[b] ? false : true;
}
printf("%d %d",sumX, sumY);
return 0;
}
9. 직각삼각형
#include <stdio.h>
void compare(int *a, int *b);
int main(){
int a,b,c;
while(scanf("%d %d %d",&a,&b,&c) != EOF){
if(a==0&&b==0&&c==0) return 0;
compare(&a,&b);
compare(&a,&c);
compare(&b,&c);
if((a*a)+(b*b) == c*c) printf("right\n");
else printf("wrong\n");
}
return 0;
}
void compare(int *a, int *b){
int temp = 0;
if(*a > *b){
temp = *a;
*a = *b;
*b = temp;
}
}
10. 택시 기하학
#include <stdio.h>
#define PI 3.14159265359
int main(){
double r;
scanf("%lf",&r);
printf("%.6lf",r*r*PI);
printf("\n");
printf("%.6lf",r*r*2);
}
11. 터렛
#include <stdio.h>
#include <math.h>
struct info{
int x;
int y;
int r;
};
int main(){
int t;
scanf("%d",&t);
struct info p1,p2;
double distance, sub;
for(int i=0;i<t;i++){
scanf("%d %d %d %d %d %d", &p1.x, &p1.y, &p1.r, &p2.x, &p2.y, &p2.r);
distance = sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
sub = (p1.r > p2.r) ? (p1.r - p2.r) : (p2.r - p1.r);
if(distance == 0 && p1.r == p2.r) printf("-1");
else if(distance == p1.r + p2.r || sub == distance) printf("1");
else if(p1.r + p2.r > distance && sub < distance) printf("2");
else printf("0");
printf("\n");
}
}
요약
- 비효율적인 문제풀이
- 어제와 달라진 문제풀이
- 구조체도 써주고 포인터 변수까지 쓰기.
- 11번 문제는 원의 접점조건을 알아야 한다.