티스토리 뷰

algorithm

[c++] 자릿수의 합

tonirr 2020. 11. 29. 17:51
  • 자릿수의 합
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
	int digit_sum(int x){
		int tmp, sum=0;
		while(x>0){
			tmp = x%10;
			sum += tmp;
			x = x/10;
		}
		return sum;
	}
	
int main() {
//	freopen("input.txt", "rt", stdin);
	int n, num, i, sum, max=-2147000000, res;
	scanf("%d", &n);
	
	for(i=0; i<n; i++){
		scanf("%d", &num);
		sum = digit_sum(num);
		if(sum > max){
			max = sum;
			res = num;
		}
		else if(sum == max){
			if(num > res){
				res = num;
			}
		}
	}
	
	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 */
using namespace std;
int main() {
	//freopen("input.txt", "rt", stdin);
	int n, i, cnt=0;
	scanf("%d", &n);
	for(i=1; i<=n; i++){
		int tmp=i;	// for문에서 i인덱스로 돌고있을 때 i를 변경시켜선 안됨  
		while(tmp>0){
			tmp = tmp/10;
			cnt++;
		}
	}	
	printf("%d\n", cnt);
	return 0;
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함