Problems

# The minimal sum of digits

published at 6/14/22, 10:53:09 am

# include<map>

using namespace std;

int main() { long m,i,mini=100,n,ans=0; cin>>m>>n; for(i=m;i<=n;i++) { long ic=i,sum=0; while(ic!=0) { sum+=ic%10; ic/=10; } if(sum<mini) mini=sum; if(mini==1) break; } for(i=m;i<=n;i++) { long sum=0,ic=i; while(ic!=0) { sum+=ic%10; ic/=10; } if(sum==mini) { ans++; } } cout<<ans; }

published at 6/8/23, 1:02:06 pm

# include <bits/stdc++.h>

using namespace std;

int getSumOfDigits(int number) { int sum = 0; while (number > 0) { sum += number % 10; number /= 10; } return sum; }

int main() { int m, n; cin >> m >> n;

int minSum = getSumOfDigits(m);
int count = 1;

for (int num = m + 1; num <= n; num++) {
int sum = getSumOfDigits(num);
if (sum < minSum) {
minSum = sum;
count = 1;
} else if (sum == minSum) {
count++;
}
}

cout << count << endl;

return 0;


}

published at 7/5/23, 12:13:53 pm

# include<map>

using namespace std; bool OK(int n,int m, int d){ return (m <= d and d <= n); } int give_digits(int n){ int res = 0; while(n > 0){ res += n % 10; n /= 10; }

  return res;


} int comput(int m, int n){ int res = 0; if(m == n){ return 1; }else{ if(OK(n,m,1)) ++res; if(OK(n,m,10)) ++res; if(OK(n,m,100)) ++res; if(OK(n,m,1000)) ++res; if(OK(n,m,1e4)) ++res; if(OK(n,m,1e5)) ++res; if(OK(n,m,1e6)) ++res; if(res) return res; } if(!res){ map<int,int> mp; for(int i = m; i <= n; ++i){ mp[give_digits(i)]++; } return mp.begin()->second; } } int main(){ int m, n; scanf("%d %d",&m,&n); printf("%d\n",comput(m,n)); puts(" "); return 0; }