标签:
题目要求:
#include "stdafx.h" #include<iostream.h> #include "stdlib.h" int CountNum(int n) { int count=0; int factor=1; int LowerNum=0; int CurNum=0; int HigherNum=0; while (n/factor!=0) { LowerNum=n-(n/factor)*factor; CurNum=(n/factor)%10; HigherNum=n/(factor*10); switch (CurNum) { case 0: count=count+HigherNum*factor; break; case 1: count=count+HigherNum*factor + LowerNum +1; break; default: count=count+(HigherNum+1)*factor; break; } factor=factor*10; } return count; } int main() { int num; cout<<"请输入数字:\n"; cin>>num; cout<<"\n"; cout<<num<<"中出现数字1的个数为:\n"; cout<<CountNum(num)<<endl; return 0; }
三、结果截图
标签:
原文地址:http://www.cnblogs.com/luyu2783/p/4467035.html