注册 登录
编程论坛 C++教室

求解释啊,为何觉得IF没一点作用

a99875984 发布于 2012-10-08 18:00, 335 次点击
程序代码:
//1-9 9个数字,33组合,第三个数为第二个的两倍,第一个的三倍
#include <iostream>
using namespace std;
void fun()
{
    int one,two,three;
    int b[10]={0,0,0,0,0,0,0,0,0,0};
    for(one=123;one<=333;one++)
    {
        three=3*one;
        two=2*one;
        if(one%10!=0&&one/10%10!=0&&one/100!=0&&two%10!=0&&two/10%10!=0&&two/100!=0&&three%10!=0&&three/10%10!=0&&three/100!=0)
        {
            b[(one%10)]=one%10;
            b[(one/100)]=one/100;
            b[((one/10)%10)]=((one/10)%10);
            b[(two%10)]=two%10;
            b[((two/10)%10)]=(two/10)%10;
            b[(two/100)]=two/100;
            b[(three%10)]=three%10;
            b[((three/10)%10)]=(three/10)%10;
            b[(three/100)]=three/100;
            if(b[0]==0&&b[1]==1&&b[2]==2&&b[3]==3&&b[4]==4&&b[5]==5&&b[6]==6&&b[7]==7&&b[8]==8&&b[9]==9)
                cout<<one<<" "<<two<<" "<<three<<" "<<"符合条件"<<endl;
        }
    }
}
int main()
{
    fun();
    return 0;
}

请问下为何觉得IF条件没发挥作用,去掉IF,只是少了2组数据,这是什么情况哈?
还有在数组里,如果数组下标是0,减1会怎么样哈?


[ 本帖最后由 a99875984 于 2012-10-8 18:04 编辑 ]
3 回复
#2
风之子MIKEY2012-10-08 18:57
你的B[10]数组在执行完次循环后未重新设置初值。
void fun()
{
    int one,two,three;
    int b[10]={0,0,0,0,0,0,0,0,0,0};
    for(one=123;one<=333;one++)
    {
        three=3*one;
        two=2*one;
        if(one%10!=0&&one/10%10!=0&&one/100!=0&&two%10!=0&&two/10%10!=0&&two/100!=0&&three%10!=0&&three/10%10!=0&&three/100!=0)
        {
            b[(one%10)]=one%10;
            b[(one/100)]=one/100;
            b[((one/10)%10)]=((one/10)%10);
            b[(two%10)]=two%10;
            b[((two/10)%10)]=(two/10)%10;
            b[(two/100)]=two/100;
            b[(three%10)]=three%10;
            b[((three/10)%10)]=(three/10)%10;
            b[(three/100)]=three/100;
            if(b[0]==0&&b[1]==1&&b[2]==2&&b[3]==3&&b[4]==4&&b[5]==5&&b[6]==6&&b[7]==7&&b[8]==8&&b[9]==9)
                cout<<one<<" "<<two<<" "<<three<<" "<<"符合条件"<<endl;
            b[0]=0;
            b[1]=0;
            b[2]=0;
            b[3]=0;
            b[4]=0;
            b[5]=0;
            b[6]=0;
            b[7]=0;
            b[8]=0;
            b[9]=0;            
        }
    }
}
不知对否未验证!
#3
a998759842012-10-08 21:31
回复 2楼 风之子MIKEY
恩,谢谢了,就是没初始化,谢谢了,但还是想问下,当数组的下标为0时,再将下标减1会怎么样哈?
#4
风之子MIKEY2012-10-08 22:22
得不到你想要的结果,得到的值也不确定。
1