int ten(int n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
return decimalNumber;
}
int main(){
int N,i;
scanf(“%d”,&N);
int a[N],b[N],c[N],d[N];
for(i=0;i<N;i++){
scanf(“%8d%8d%8d%8d”,&a[i],&b[i],&c[i],&d[i]);
}
for(i=0;i<N;i++){
printf(“%d.%d.%d.%d\n”,ten(a[i]),ten(b[i]),ten(c[i]),ten(d[i]));
}
}
C语言(C班)C题题解
想写的尽量短一点,就写成这样了
12行的解释:ip地址的第i位可以看成是二进制的第i个8位转成十进制的结果,而第i位在这八位中是第i-8×(i/8)位
include
include
int ten(int n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
return decimalNumber;
}
int main(){
int N,i;
scanf(“%d”,&N);
int a[N],b[N],c[N],d[N];
for(i=0;i<N;i++){
scanf(“%8d%8d%8d%8d”,&a[i],&b[i],&c[i],&d[i]);
}
for(i=0;i<N;i++){
printf(“%d.%d.%d.%d\n”,ten(a[i]),ten(b[i]),ten(c[i]),ten(d[i]));
}
}