統計數字
統計1到n,所有數字中,0,1,....,9出現的次數。
額,這個問題簡單。
編程啊,要養成好習慣啊,先設計好算法,再上機敲代碼才行啊。不然,全亂了。
額,這個問題簡單。
#include<stdio.h>
int main()
{
int n,i,q,r,t,now,a[10];
while (scanf("%d",&n)==1)
{
for (i=0;i<=9;i++)
a[i]=0;
t=10;
while (n/t!=0)
{
r=n%t;
q=n/t;
now=r*10/t;
for (i=0;i<=9;i++)
a[i]=a[i]+q*t/10;
for (i=0;i<now;i++)
a[i]=a[i]+t/10;
a[now]=a[now]+r%(t/10)+1;
t=t*10;
}
now=n*10/t;r=n;
for (i=1;i<now;i++)
a[i]=a[i]+t/10;
a[now]=a[now]+r%(t/10)+1;
if (a[0]>0) a[0]--;
for (i=0;i<=9;i++)
printf("%d\n",a[i]);
}
}
int main()
{
int n,i,q,r,t,now,a[10];
while (scanf("%d",&n)==1)
{
for (i=0;i<=9;i++)
a[i]=0;
t=10;
while (n/t!=0)
{
r=n%t;
q=n/t;
now=r*10/t;
for (i=0;i<=9;i++)
a[i]=a[i]+q*t/10;
for (i=0;i<now;i++)
a[i]=a[i]+t/10;
a[now]=a[now]+r%(t/10)+1;
t=t*10;
}
now=n*10/t;r=n;
for (i=1;i<now;i++)
a[i]=a[i]+t/10;
a[now]=a[now]+r%(t/10)+1;
if (a[0]>0) a[0]--;
for (i=0;i<=9;i++)
printf("%d\n",a[i]);
}
}
編程啊,要養成好習慣啊,先設計好算法,再上機敲代碼才行啊。不然,全亂了。
posted on 2012-03-06 23:42 wangs 閱讀(128) 評論(0) 編輯 收藏 引用 所屬分類: ACM-201203