题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
#include <stdio.h> int main() { int ipin[4], ip_buf[12], g, s, b, i, j = 0; long long int anc = 0, exp = 1, ipd, mod; scanf("%d.%d.%d.%d", &ipin[3], &ipin[2], &ipin[1], &ipin[0]); scanf("%lld", &ipd); for (i = 0; i < 4; i++) { anc += ipin[i] * exp; exp = exp * 256; } printf("%lld\n", anc); i = 0; while (ipd > 256) { //用while可以对更多位ip进行处理 ip_buf[i] = ipd % 256; ipd = ipd / 256; i++; } ip_buf[i] = ipd; printf("%d.%d.%d.%d", ip_buf[3], ip_buf[2], ip_buf[1], ip_buf[0]); return 0; }