| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 459 人关注过本帖
标题:c语言新手求解
只看楼主 加入收藏
dkxc06
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-12-25
收藏
 问题点数:0 回复次数:4 
c语言新手求解
题目是调用函数求输入整数中出现2的次数,案例输入没有问题,但是当数字很大的时候会一直输出0,求大神解释一下,多谢。
代码在下面
#include<stdio.h>
int main()
{
    int n;
    int countdigit(int number,int digit);
    while(scanf("%d",&n)!=EOF)
    {
        printf("%d\n",countdigit(n,2));
    }
}
int countdigit(int number,int digit)
{
    int i=0,s;
    if(number<0)
    number=-number;
    for(;number!=0;number/=10)
    {
        s=number%10;
        if(s==digit)
        i++;
    }
    return i;
}
搜索更多相关主题的帖子: c语言 number 
2014-12-25 16:27
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
收藏
得分:0 
超过了整型能表示的范围

~~~~~~我的明天我知道~~~。
2014-12-25 17:28
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
“但是当数字很大的时候”
------ 你就不能直接说什么哪个数时结果不正确?

while(scanf("%d",&n)!=EOF)
------ 看到好多人这么写,也不知道是哪个缺德鬼这么教的。明明应该是 while(scanf("%d",&n)!=1)
看看C标准是怎么规定的:
The scanf function returns the value of the macro EOF if an input failure occurs before
the first conversion (if any) has completed
. Otherwise, the scanf function returns the
number of input items assigned, which can be fewer than provided for, or even zero, in
the event of an early matching failure.
2014-12-26 08:35
dkxc06
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-12-25
收藏
得分:0 
回复 2楼 xufan
这样的啊,谢谢了。
2014-12-29 16:21
dkxc06
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-12-25
收藏
得分:0 
回复 3楼 rjsp
不好意思,我输入的是12个2,出错误了。
那个while(scanf("%d",&n)!=1)我懂了,多谢解答了。
2014-12-29 16:39
快速回复:c语言新手求解
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017113 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved