石家庄家具,李亚林,鲁迅文章
1 10 44 497 346 542 0 0
2 185 40
1 #include<stdio.h> 2 int main() 3 { 4 int a,b,i,cut; 5 while(scanf("%d%d",&a,&b)!=eof,(a||b)) 6 { 7 if(a > b) 8 { 9 int t = a; 10 a = b; 11 b = t; 12 } 13 cut = 0; 14 for(i=a; i<=b; i++) 15 { 16 int ans = i; 17 while(ans > 0) 18 { 19 if(ans%10 == 1) 20 cut++; 21 ans/=10; 22 } 23 } 24 printf("%d\n",cut); 25 } 26 return 0; 27 }
我没骗你吧!下面我们来讲这题怎么做吧
算法思想:我们先求1~b 1的个数,在求1~(a-1)1的个数,然后再相减就等于a~b 1的个数了。求1~n 1的个数可以参考一下这个博客:https://blog.csdn.net/yi_afly/article/details/52012593 很详细!不说了,附上代码:
ac代码:
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 int f(int n) 5 { 6 if(n < 1) return 0; 7 int ans = 0; 8 int base = 1; 9 int round = n; 10 while(round > 0) 11 { 12 int weight = round%10; 13 round/=10; 14 ans+=round*base; 15 if(weight == 1) 16 ans+=(n%base)+1; 17 else if(weight > 1) 18 ans+=base; 19 base*=10; 20 } 21 return ans; 22 } 23 int main() 24 { 25 int a,b; 26 while(scanf("%d%d",&a,&b)!=eof,(a||b)) 27 { 28 if(a > b) 29 { 30 int t = a; 31 a = b; 32 b = t; 33 } 34 printf("%d\n",f(b)-f(a-1)); 35 } 36 return 0; 37 }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论