algorithm

[c++] 숫자의 총개수, 가장 많이 사용된 자리수

tonirr 2020. 11. 30. 23:34
  • 숫자의 총개수
#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, sum=0, c=1, d=9, res=0;
	scanf("%d", &n);
	while(sum+d<n){
		sum=sum+d;
		res=res+(c*d);
		c++;
		d=d*10;
	}
	res=res+((n-sum)*c);
	printf("%d\n", res);
	return 0;
}

 

  • 가장 많이 사용된 자리수
#include <iostream>

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