NOip 2010 普及組 復賽 two 數字統計
//洛谷 p1179 數字統計 //難度:入門難度//考點:輸入,輸出 ,整數四則運算,取整,取模,函數編寫,棧,算法時間復雜度 //適用:小學生 //感悟:原本以為該題需要用什么技巧。一看<=100000,立馬想到枚舉,采用分離個十百千萬的做法,數出2的個數。
附上AC代碼,編譯環境Dev-C++4.9.9.2
#include <stdio.h>int count2(int a){ int top=-1; int b[10]; int i; int count=0; while(a){ top++; b[top]=a%10; a/=10; } for(i=0;i<=top;i++) if(b[i]==2) count++; return count;}int main(){ int L,R; int ans=0; int i; scanf("%d%d",&L,&R); for(i=L;i<=R;i++) ans+=count2(i); PRintf("%d/n",ans); return 0;}
新聞熱點
疑難解答