这两段代码的结果为什么会不同?(求三位数的水仙花数)
代码一:#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
int num, yu, shan, sum;
int i;
for(num=100; num<=999; num++)
{
sum = 0;
shan = num;
for(i=1; i<=3; i++)
{
yu = shan%10;
shan = shan/10;
int mid = pow(yu,3);
sum = sum + mid;
}
if(sum == num)
printf("%d\n", num);
}
return 0;
}
代码二:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
int num, yu, shan, sum;
int i, j;
for(num=100; num<=999; num++)
{
sum = 0;
shan = num;
for(i=1; i<=3; i++)
{
yu = shan%10;
shan = shan/10;
int mid=1;
for(j=0; j<3; j++)
mid = mid*yu;
sum = sum + mid;
}
if(sum == num)
printf("%d\n", num);
}
return 0;
}