提示:文章
文章目录
前言
先全部获取完输入再处理数据
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool isTargetSonNet(int array[], int len)
{
int tempArray[32] = {0};
int tempArrayIndex = 0;
for(int i = 0; i < len; i++)
{
int data = array[i];
if(data > 255 || data < 0)
{
return false;
}
int index = ((tempArrayIndex + 1) * 8);
while(data)
{
int dat = data % 2;
tempArray[--index] = dat;
data /= 2;
}
tempArrayIndex++;
}
int count = 0;
for(int i = 0; i + 1 < 32; i++)
{
int temp = tempArray[i] - tempArray[i + 1];
if(temp != 0 && temp != 1)
{
return false;
}
if(temp == 1)
{
count++;
}
}
if(count != 1)
{
return false;
}
return true;
}
int main() {
int a, b;
char sonNet[20] = {'\0'};
char ip1[20] = {'\0'};
char ip2[20] = {'\0'};
int arraySonNet[4] = {0};
int arrayIp1[4] = {0};
int arrayIp2[4] = {0};
while (scanf("%s", sonNet) != EOF) { // 注意 while 处理多个 case
if(strlen(sonNet) < 4)
{
return 0;
}
// 64 位输出请用 printf("%lld") to
char output = 'f';
char delimiters[2] = ".";
char* p = strtok(sonNet, delimiters);
int index = 0;
while(p != NULL)
{
//printf("%s\n", p);
int data = atoi(p);
// if(data > 255)
// {
// output = '1';
// break;
// }
// else if(data != 255 && data != 0)
// {
// output = '1';
// break;
// }
arraySonNet[index++] = data;
p = strtok(NULL, delimiters);
}
if(!isTargetSonNet(arraySonNet, 4))
{
output = '1';
}
if(output != 'f')
{
goto RESULT;
}
if(scanf("%s", ip1) != EOF)
{
p = strtok(ip1, delimiters);
int index = 0;
while(p != NULL)
{
//printf("%s\n", p);
int data = atoi(p);
if(data > 255 || data < 0)
{
output = '1';
break;
}
arrayIp1[index++] = data;
p = strtok(NULL, delimiters);
}
}
if(output != 'f')
{
goto RESULT;
}
if(scanf("%s", ip2) != EOF)
{
p = strtok(ip2, delimiters);
int index = 0;
while(p != NULL)
{
//printf("%s\n", p);
int data = atoi(p);
if(data > 255 || data < 0)
{
output = '1';
break;
}
arrayIp2[index++] = data;
p = strtok(NULL, delimiters);
}
}
if(output != 'f')
{
goto RESULT;
}
int count = 0;
for(int i = 0; i < 4; i++)
{
if( (ip1[i] & sonNet[i]) == (ip2[i] & sonNet[i]) )
{
++count;
}
}
if(output == 'f')
{
if(count == 4)
{
output = '0';
}
else {
output = '2';
}
}
RESULT:
printf("%c\n", output);
}
return 0;
}
最终修改成如下的代码,全部通过实例,Ace了。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool isTargetSonNet(int array[], int len)
{
int tempArray[32] = {0};
int tempArrayIndex = 0;
for(int i = 0; i < len; i++)
{
int data = array[i];
if(data > 255 || data < 0)
{
return false;
}
int index = ((tempArrayIndex + 1) * 8);
while(data)
{
int dat = data % 2;
tempArray[--index] = dat;
data /= 2;
}
tempArrayIndex++;
}
int count = 0;
for(int i = 0; i + 1 < 32; i++)
{
int temp = tempArray[i] - tempArray[i + 1];
if(temp != 0 && temp != 1)
{
return false;
}
if(temp == 1)
{
count++;
}
}
if(count != 1)
{
return false;
}
return true;
}
int main() {
int a, b;
char sonNet[20] = {'\0'};
char ip1[20] = {'\0'};
char ip2[20] = {'\0'};
int arraySonNet[4] = {0};
int arrayIp1[4] = {0};
int arrayIp2[4] = {0};
while (scanf("%s", sonNet) != EOF) { // 注意 while 处理多个 case
// 64 位输出请用 printf("%lld") to
scanf("%s", ip1);
scanf("%s", ip2);
char output = 'f';
char delimiters[2] = ".";
char* p = strtok(sonNet, delimiters);
int index = 0;
while(p != NULL)
{
int data = atoi(p);
arraySonNet[index++] = data;
p = strtok(NULL, delimiters);
}
if(!isTargetSonNet(arraySonNet, 4))
{
output = '1';
goto RESULT;
}
p = strtok(ip1, delimiters);
index = 0;
while(p != NULL)
{
//printf("%s\n", p);
int data = atoi(p);
if(data > 255 || data < 0)
{
output = '1';
goto RESULT;
}
arrayIp1[index++] = data;
p = strtok(NULL, delimiters);
}
p = strtok(ip2, delimiters);
index = 0;
while(p != NULL)
{
//printf("%s\n", p);
int data = atoi(p);
if(data > 255 || data < 0)
{
output = '1';
goto RESULT;
}
arrayIp2[index++] = data;
p = strtok(NULL, delimiters);
}
int count = 0;
for(int i = 0; i < 4; i++)
{
if( (ip1[i] & sonNet[i]) == (ip2[i] & sonNet[i]) )
{
++count;
}
}
if(count == 4)
{
output = '0';
}
else {
output = '2';
}
RESULT:
printf("%c\n", output);
}
return 0;
}
3、总结
3.1 atoi可以转换带符号变量
3.2 strtok写法一开始不会写了
3.3 掩码网络号的处理
3.4 使用了goto语句,是否还有其他好的解决方法。
三、
3.1
总结
未完待续