九度oj 题目1475:IP数据包解析
2015-08-28 14:32
417 查看
链接
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);
}
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);
}
相关文章推荐
- Changer常用的软件
- 3.4 从无头链表中删除给定的结点 & 遍历一次逆转链表
- mongo和pymongo简单配置和使用
- 批量更新逗号隔开的名称 (部门里面将多个用逗号隔开的ID转换成用逗号隔开的名称)(mysql)
- 用SQL语句添加删除修改字段和一些其他基本操作------阿冬专栏
- Activiti流程实例任务的执行(四)
- How to stop uwsgi when no pidfile in config?
- A Diesel is a luxury watch brand that is known to all the classes
- 利用Git和TortoiseGit把代码传输到网络服务器
- libSVM使用教程总结
- HashMap源码分析
- 中文CentOS 6.5网络配置及设置IP地址的详细分析
- 如何配置 MongoDB 副本集
- App Crash 解决办法
- jquery select事件
- 解决本地项目上传到github时$git push -u origin master error failed to push some refs...等错误
- mysql 索引类型以及创建
- Python中文乱码的处理
- iOS中的结构体保存到数组与读取
- 1091. Acute Stroke (30)