如题所述
ç®æ³åæï¼
1. å®ä¹N åç¨æ¥ç»è®¡çcntï¼ ç¨æ¥å¾ªç¯çnï¼
2. è¾å ¥N ï¼
3. å°nä»1å°N循ç¯ï¼ 对äºæ¯ä¸ªnæ§è¡å¦ä¸æä½ï¼
a) 循ç¯ååºnçæ¯ä½æ°åå¼
b)å¤æ该ä½æ¯å¦ä¸º1ï¼ å¦æ¯åç´¯å å°cntä¸ã
4. è¾åºç»æã
代ç å¦ä¸ï¼
#include <stdio.h>int main()
{
int n, N, cnt = 0;
scanf("%d",&N);//è¾å ¥Nå¼ã
for(n = 1;n<=N; n ++)//循ç¯æ§è¡
{
int t = n;
while(t)//循ç¯ååºæ¯ä¸ä½ã
{
if(t%10 == 1) cnt++;
t/=10;
}
}
printf("%d\n", cnt);//è¾åºç»æ
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2014-05-08
#include <stdio.h>
int main() {
int i,N,t,cnt;
while(scanf("%d",&N) == 1) {
cnt = 0;
for(i = 1; i <= N; ++i) {
t = i;
while(t) {
if(t % 10 == 1) ++cnt;
t /= 10;
}
}
printf("cnt = %d\n",cnt);
}
return 0;
}本回答被提问者采纳
int main() {
int i,N,t,cnt;
while(scanf("%d",&N) == 1) {
cnt = 0;
for(i = 1; i <= N; ++i) {
t = i;
while(t) {
if(t % 10 == 1) ++cnt;
t /= 10;
}
}
printf("cnt = %d\n",cnt);
}
return 0;
}本回答被提问者采纳
第2个回答 2021-05-03
从最高位到个个位处理,如32102的千位上是2,那么万位会出现10000个1 { 万位取1,0到9999 } 千位上会出现4000 { 3000(万位是0到2,千位取1) + 1000(万位是0,千位是1,000到102)} 个1 ,百位上会出现3203个1
#include <math.h>
#include <stdio.h>
int main()
{
int n;
scanf ("%d", &n);
int len = (int)(log10(n));
int a, b, c, ans = 0;
int now;
while (len != -1 )
{
now = 0;
a = pow(10, (double)(len + 1));
b = pow(10, (double)len);
c = (n % a) / b;
if (c == 1)
now += n % b + 1;
if (c > 1)
now += b;
now += n / a * b;
ans += now;
printf ("%d %d\n", len, now);
len -- ;
}
printf ("%d", ans);
return 0;
}
#include <math.h>
#include <stdio.h>
int main()
{
int n;
scanf ("%d", &n);
int len = (int)(log10(n));
int a, b, c, ans = 0;
int now;
while (len != -1 )
{
now = 0;
a = pow(10, (double)(len + 1));
b = pow(10, (double)len);
c = (n % a) / b;
if (c == 1)
now += n % b + 1;
if (c > 1)
now += b;
now += n / a * b;
ans += now;
printf ("%d %d\n", len, now);
len -- ;
}
printf ("%d", ans);
return 0;
}
第3个回答 2014-05-08
真无聊的编程题!