//输出1~n中1的个数,如f(1)=1,f(13)=6。通过测试,bymyself public class FindOnes{ public static void main(String args[]){ System.out.println(findOnes(0)); } public static int findOnes(int n){ int count = 0; for(int i=1; i<=n; i++){ int temp=i;//不能写i=i/10,因为i控制循环体!!! while(temp!=0){ if(temp%10==1){ count++; } temp = temp/10; } } return count; } }