algorithm

나이차이, 나이계산, 숫자만추출

tonirr 2020. 11. 26. 00:17
  • 나이차이
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
	//freopen("input.txt", "rt", stdin);
	int n, i, a, max=-2147000000, min=2147000000;
	cin>>n;
	
	for(i = 1; i <= n; i++){
		cin>>a;
		if(a>max){
			max=a;
		}
		if(a<min){
			min=a;
		}
	}
	cout<<max-min;
	return 0;
}

 

  • 나이계산
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
	//freopen("input.txt", "rt", stdin);
	char a[20];
	int year, age;
	scanf("%s", &a);
	if(a[7]=='1' || a[7]=='2'){
		year = 1900 + ((a[0]-48)*10+(a[1]-48));
	} else {
		year = 2000 + ((a[0]-48)*10+(a[1]-48)); 
	}
	
	age = 2019 - year + 1;
	printf("%d ", age);
	if(a[7]=='1' || a[7]=='3'){
		printf("M\n");
	} else {
		printf("W\n");
	}	
	return 0;
}

 

  • 숫자만 추출
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
	//freopen("input.txt", "rt", stdin);
	char a[100];
	int res=0, cnt=0, i;
	scanf("%s", &a);
	for(i=0; a[i]!='\0'; i++){
		if(a[i]>=48 && a[i]<=57){
			res = res*10+(a[i]-48);
		}
	}
	printf("%d\n", res);
	for(i=1; i<=res; i++){
		if(res%i==0){
			cnt++;
		}
	}
	printf("%d\n", cnt);
	return 0;
}