九度oj 题目1475:IP数据包解析

链接

http://ac.jobdu.com/problem.php?pid=1475


千万要注意使用gets,getline时多出来的回车换行的读取。

在这题里,将'\n'使用scanf读到一个char里会wrong asnwer,必须使用get读到一个char[]里!


还有一点就是,要判断T的文件输入流是否结束,否则也会wrong answer!

上两点应是oj题通用的结论!以后尽量这样用。减少排错时间。


ac代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#define INF 2<<20
#define MAX_INDEX 105
using namespace std;
int stoi(char *s,int n);
int getport(char *s,int n);
int getaddress(char *s,int n);
int main()
{
    int T;
    while(scanf("%d",&T)!=EOF)
    {
        char tp;
        char s[1000];
        gets(s);
        //scanf("%c",&tp);
        int flag = 1;
        while(T--)
        {
            gets(s);
            int IHL = stoi(s,1)*4*3;
            int Totallength  = stoi(s,2*3)*16*16*16+stoi(s,2*3+1)*16*16+stoi(s,3*3)*16+stoi(s,3*3+1);
            int add1,add2,add3,add4;
            printf("Case #%d\nTotal length = %d bytes\n",flag++,Totallength);
            add1 = getaddress(s,12*3);
            add2 = getaddress(s,13*3);
            add3 = getaddress(s,14*3);
            add4 = getaddress(s,15*3);
            printf("Source = %d.%d.%d.%d\n",add1,add2,add3,add4);
            add1 = getaddress(s,16*3);
            add2 = getaddress(s,17*3);
            add3 = getaddress(s,18*3);
            add4 = getaddress(s,19*3);
            printf("Destination = %d.%d.%d.%d\n",add1,add2,add3,add4);
            add1 = getport(s,IHL);
            add2 = getport(s,IHL+6);
            printf("Source Port = %d\n",add1);
            printf("Destination Port = %d\n\n",add2);
        }
    }
    return 0;
}


int stoi(char *s,int n)
{
    if((*(s+n))>='0'&&(*(s+n))<='9')
        return *(s+n)-'0';
    else
        return *(s+n)-'a'+10;
}
int getport(char *s,int n)
{
    return stoi(s,n)*16*16*16 + stoi(s,n+1)*16*16 + stoi(s,n+3)*16 + stoi(s,n+4);
}
int getaddress(char *s,int n)
{
    return stoi(s,n)*16 + stoi(s,n+1);
}


wa代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#define INF 2<<20
#define MAX_INDEX 105
using namespace std;
int stoi(char *s,int n);
int getport(char *s,int n);
int getaddress(char *s,int n);
int main()
{
    int T;
    while(scanf("%d",&T)!=EOF)
    {
        char tp;
        char s[1000];
        //gets(s);
        scanf("%c",&tp);
        int flag = 1;
        while(T--)
        {
            gets(s);
            int IHL = stoi(s,1)*4*3;
            int Totallength  = stoi(s,2*3)*16*16*16+stoi(s,2*3+1)*16*16+stoi(s,3*3)*16+stoi(s,3*3+1);
            int add1,add2,add3,add4;
            printf("Case #%d\nTotal length = %d bytes\n",flag++,Totallength);
            add1 = getaddress(s,12*3);
            add2 = getaddress(s,13*3);
            add3 = getaddress(s,14*3);
            add4 = getaddress(s,15*3);
            printf("Source = %d.%d.%d.%d\n",add1,add2,add3,add4);
            add1 = getaddress(s,16*3);
            add2 = getaddress(s,17*3);
            add3 = getaddress(s,18*3);
            add4 = getaddress(s,19*3);
            printf("Destination = %d.%d.%d.%d\n",add1,add2,add3,add4);
            add1 = getport(s,IHL);
            add2 = getport(s,IHL+6);
            printf("Source Port = %d\n",add1);
            printf("Destination Port = %d\n\n",add2);
        }
    }
    return 0;
}


int stoi(char *s,int n)
{
    if((*(s+n))>='0'&&(*(s+n))<='9')
        return *(s+n)-'0';
    else
        return *(s+n)-'a'+10;
}
int getport(char *s,int n)
{
    return stoi(s,n)*16*16*16 + stoi(s,n+1)*16*16 + stoi(s,n+3)*16 + stoi(s,n+4);
}
int getaddress(char *s,int n)
{
    return stoi(s,n)*16 + stoi(s,n+1);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值