【发布时间】:2020-06-09 12:55:40
【问题描述】:
这是一个问题,您必须输入一个非零位数字,然后程序会将其转换为具有 4 个部分且每个部分小于 255 的 IP 地址,并且必须打印所有 IP 地址。
我已经尝试过这种递归方法,并且正在陷入无限循环。
#include<stdio.h>
#include<math.h>
int a[3];
void comuni(unsigned long n,int count){
int i=count;
do{
if(count<0){
return;
}
int t=pow(10,i);
a[count]=n/t;
int rem=n%t;
if(a[count]<=255 &&a[count]>0 && count>=0){
printf("%d.",a[count]);
comuni(rem,count-1);
}
i++;
}while(1);
}
int main()
{
// Insert your code here.
unsigned long n;
scanf("%ul",&n);
comuni(n,3);
return 0;
}
【问题讨论】:
-
所有IP地址是什么意思?能举个例子吗?
-
为什么要除以 10?如果这些是 IP 地址的一部分,则应除以 256。
-
int a[3];创建一个数组,其空间足够容纳 三个 值:a[0]、a[1]和a[2]。您尝试访问不存在的a[3]! -
Ayush Gupta,请告诉我如何让我的回答对您更有帮助。