标题:输出1000以内能被3整除且至少有一位是3得数,每行输出4个
只看楼主
王璐
Rank: 2
等 级:论坛游民
帖 子:126
专家分:54
注 册:2010-7-26
结帖率:93.1%
已结贴  问题点数:20 回复次数:27 
输出1000以内能被3整除且至少有一位是3得数,每行输出4个
大家看看这个程序哪里有错误,还有没有更简洁效率更高的程序?
#include<stdio.h>
void main()
{
    int i,j,k;
    for(i=0;i<=9;i++)
    {
        if((i+5)%3==0)
        {
            printf("%3d %3d",i*10+5,50+i);
            k+=2;
            if(k%4==0)
                printf("\n");
        }
        for(j=0;j<=9;j++)
        {
            if((i+j+5)%3==0)
            {
                if(i!=j)
                {
                    if((i!=5)&&(j!=5))
                    {
                        printf("%3d %3d ",500+10*i+j,500+10*j+i);
                        k+=2;
                        if(k%4==0)
                            printf("\n");
                        else
                        {
                            printf("%3d %3d ",100*i+10*j+5,100*i+50+j);
                            k+=2;
                        }
                        if(k%4==0)
                            printf("\n");
                        else
                        {
                            printf("%3d %3d ",100*j+10*i+5,100*j+50+i);
                            k+=2;
                        }
                        if(k%4==0)
                            printf("\n");
                    }
                    if(i==5)
                    {
                        if(k%4==1||k%4==0)
                        {
                            printf("%3d %3d %3d ",550+i,505+10*i,100*i+55);
                            k+=3;
                            if(k%4==0)
                                printf("\n");
                        }
                        else
                        {
                            if(k%4==2)
                            {
                                printf("%3d %3d \n%3d ",550+i,505+10*i,100*i+55);
                                k+=3;
                            }
                            else
                            {
                                if(k%4==3)
                                {
                                    printf("%3d \n%3d %3d ",550+i,505+10*i,100*i+55);
                                    k+=3;
                                }
                                
                        }
                    }
                    if(j==5)
                    {}
                }
                if(i==j&&i!=5)
                {
                    if(k%4==1||k%4==0)
                    {
                        printf("%3d %3d %3d ",500+10*i+i,100*i+50+i,100*i+10*i+5);
                        k+=3;
                        if(k%4==0)
                                printf("\n");

                    }
                    else
                    {
                        if(k%4==2)
                        {
                            printf("%3d %3d \n%3d ",500+10*i+i,100*i+50+i,100*i+10*i+5);
                            k+=3;
                        }
                        else
                        {
                            printf("%3d \n%3d %3d ",500+10*i+i,100*i+50+i,100*i+10*i+5);
                            k+=3;
                        }
                    }

                }
                if(i==j&&i==5)
                printf("%3d ",555);

            }            

        }
    }
}
搜索更多相关主题的帖子: 每行 内能 整除 输出 
2010-09-23 16:42
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
得分:0 
确实写的比较长,估计三分之一的代码就足足有余了。最笨的方法就是从3开始,增量为3,再判断该数,若其某位含有3即是符合条件的。

★★★★★为人民服务★★★★★
2010-09-23 17:06
御坂美琴
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:魔術の禁書目錄
等 级:小飞侠
威 望:9
帖 子:952
专家分:2929
注 册:2010-8-18
得分:0 
#include <stdio.h>

int main()
{
    int n;
    for (n=3; n<1000; n+=3)
    {
        if (n%10==3 || n/10%10==3 || n/100==3)
            printf("%d,", n);
    }
    return 0;
}

永远为正义而奋斗,锄强扶弱的Level 5 超能力者
とある魔術の禁書目錄インデックス__御み坂さか美み琴こと
http://bbs.bccn.net/space.php?action=threads&uid=483997
2010-09-23 17:21
御坂美琴
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:魔術の禁書目錄
等 级:小飞侠
威 望:9
帖 子:952
专家分:2929
注 册:2010-8-18
得分:0 
#include <stdio.h>

int main()
{
    int n, c=0;
    for (n=3; n<1000; n+=3)
    {
        if (n%10==3 || n/10%10==3 || n/100==3)
        {
            printf("%d ", n);
            if (++c>=4)
                puts(""), c=0;
        }
    }
    return 0;
}

4个一行的话

永远为正义而奋斗,锄强扶弱的Level 5 超能力者
とある魔術の禁書目錄インデックス__御み坂さか美み琴こと
http://bbs.bccn.net/space.php?action=threads&uid=483997
2010-09-23 17:36
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
得分:0 
3的倍数也就是各个位数的和能整除3

设个 char a,b,c;
a=3;
0<=b<10,0<=c<10;
那么只需(b+c)%3==0
然后 a,b,c组合  
2010-09-23 19:06
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
得分:0 
回复 3楼 御坂美琴
御姐的回帖一定要顶,

我就是真命天子,顺我者生,逆我者死!
2010-09-23 19:10
真我
Rank: 4
等 级:业余侠客
威 望:1
帖 子:146
专家分:210
注 册:2010-7-14
得分:0 
以下是引用hahayezhe在2010-9-23 19:06:35的发言:

3的倍数也就是各个位数的和能整除3

设个 char a,b,c;
a=3;
0<=b<10,0<=c<10;
那么只需(b+c)%3==0
然后 a,b,c组合  
不带你这样玩的,这样更累
2010-09-23 22:42
真我
Rank: 4
等 级:业余侠客
威 望:1
帖 子:146
专家分:210
注 册:2010-7-14
得分:0 
一个数除10的余就是他的个位数,
还是这招if (n%10==3 || n/10%10==3 || n/100==3)
2010-09-23 22:46
真我
Rank: 4
等 级:业余侠客
威 望:1
帖 子:146
专家分:210
注 册:2010-7-14
得分:0 
#include <stdio.h>

int main()
{
    int n, c=0;
    for (n=1; n<1000; n++)
    {
        if( (n%3==0)&&( (n%10==3 || n/10%10==3 || n/100==3)))
        {
            printf("%d ", n);
            if (++c>=4)
                puts(""), c=0;
        }
    }
    return 0;
}
拿御姐的改一下,别生气哦
2010-09-23 22:59
王璐
Rank: 2
等 级:论坛游民
帖 子:126
专家分:54
注 册:2010-7-26
得分:0 
我知道你们那样会比较短,但我的那个效率会更高的啊
2010-09-23 23:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-321164-1-1.html




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

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