链接:https://ac.nowcoder.com/acm/contest/19859/D
来源:牛客网
题目描述
求出[a,b]区间内有多少个数数位之和为5的倍数
输入描述:
输入一行包含两个整数a,b (1<= a<= b<=1000000)
输出描述:
输出一个整数
示例1
输入
10 20
输出
2
说明
14和19的数位和为5和10,符合条件
分析:主要在于怎样判断所有数位之和为5的倍数这一函数的实现。
初试
#include<bits/stdc++.h>
using namespace std;
bool test_five(int num){
stack<int> s;
int temp=num;
while(temp){
s.push(temp%10);
temp=temp/10;
}
temp=0;
while(!s.empty()){
temp+=s.top();
s.pop();
}
if(temp%5==0)
return true;
else
return false;
}
int fun(int lo,int hi){
int count=0;
for(int i=lo;i<=hi;i++){
if(test_five(i))
count++;
}
return count;
}
int main(){
int a,b,ans;
cin>>a>>b;
ans=fun(a,b);
cout<<ans<<endl;
return 0;
}
原文链接: https://www.cnblogs.com/walter-mitty/p/17054918.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/312471
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!