水仙花数-C语言和python实现

C语言代码

#include

int main(){
    int i=100,j=0;
    int count=0;
    int num=0;
    for (; i < 1000; i++)
    {

        count = i;
        for(;j<3;j++){
            num = num + (count%10)*(count%10)*(count%10);
            count = (int)(count/10);
        }
        if (num==i)
        {
            /* code */
            printf("%d是一个水仙花数\n",num);
            num=0;
            count=0;
            j=0;
        }else{
            num=0;
            count=0;
            j=0;
        }
        
    }
   return 0; 
}

python实现

count = 0
for i in range(100,1000):
    for j in range(3):
        num = str(i)
        count = count + eval(num[j])**3
    if count == i:
        print("{}是水仙花数".format(i))
        count = 0
    else:
        count = 0

你可能感兴趣的:(c语言,python)