标签:
一、题目要求:
f(303)=...=(10+10)*3+(1)+100;
......
1 #include "iostream.h" 2 #include "stdlib.h" 3 int CountNum(int n) 4 { 5 int count=0; 6 int factor=1; 7 int LowerNum=0; 8 int CurNum=0; 9 int HigherNum=0; 10 while (n/factor!=0) 11 { 12 LowerNum=n-(n/factor)*factor; 13 CurNum=(n/factor)%10; 14 HigherNum=n/(factor*10); 15 switch (CurNum) 16 { 17 case 0: 18 count=count+HigherNum*factor; 19 break; 20 case 1: 21 count=count+HigherNum*factor + LowerNum +1; 22 break; 23 default: 24 count=count+(HigherNum+1)*factor; 25 break; 26 } 27 factor=factor*10; 28 } 29 return count; 30 } 31 32 int main() 33 { 34 int num; 35 cout<<"请输入数字:\n"; 36 cin>>num; 37 cout<<"\n"; 38 cout<<num<<"中出现数字1的个数为:\n"; 39 cout<<CountNum(num)<<endl; 40 return 0; 41 }
四、代码截图
五、实验心得
当我们拿到看似没有思路的问题时,可以列举一下有代表性的数,从中找取规律,比如这次实验中,看似无从下手,但是当我们把每个阶段的数据列举出来时,便可以找到其中的规律。
标签:
原文地址:http://www.cnblogs.com/yuanyajiao/p/4474992.html