2012年北京邮电大学计算机考研机试试题及答案
package oj_2012_jsj;
import java.util.Scanner;
public class oj_2012_1 {
public static void main(String[]args){
Scanner in = new Scanner(System.in);
int T = in.nextInt();
while(T>0)
{
int n = in.nextInt();
if(n==0)
{
System.out.println();
continue;
}
int num = 1;
int index = 0;
while(num<=n)
{
num*=2;
index++;
}
num = num/2;
n-=num;
/*
* 23 10111 5
* k[4] = 1;
* index = 4; num = 8; n = 7; k[3]=0;
* index = 3; num = 4; n = 7; k[2]=1;
* index = 2; num = 2; n = 3; k[1]=1;
* index = 1; num = 1; n = 1; k[0]=1;
*
*/
int[] k = new int[index];
int r = index;
//System.out.println("num: "+num+" index: "+index);
k[--index] = 1;
while(n>0){
num = num/2;
if(n>=num){
k[--index] = 1;
n = n-num;
}else{
k[--index] = 0;
}
//System.out.println("n: "+n+" index: "+index);
}
for(int i=r-1;i>=0;i--){
System.out.print(k[i]);
}
System.out.println();
T--;
}
}
}
package oj_2012_jsj;
import java.util.Scanner;
public class oj_2012_2 {
public static void main(String[]args){
Scanner in = new Scanner(System.in);
int T = in.nextInt();
while(T!=0){
int n = in.nextInt();
int k = in.nextInt();
int [][]a = new int[n][n];
int [][]b = new int[n][n];
int [][]c = new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j] = in.nextInt();
b[i][j] = a[i][j];
c[i][j] = 0;
}
}
k--;
while(k!=0){
for(int p=0;p<n;p++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
c[p][i] += a[p][j]*b[j][i];
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j] = c[i][j];
}
}
k--;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(j==n-1)
System.out.print(a[i][j]);
else
System.out.print(a[i][j]+" ");
}
System.out.println();
}
T--;
}
}
}
#include<bits/stdc++.h>
using namespace std;
typedef struct Bnode{
int data;
struct Bnode *lchild,*rchild;
}Bnode,*Btree;
Btree Create_BST(Btree T,int key)
{
if(T==NULL)
{
//cout<<"建立"<<endl;
T = (Bnode*)malloc(sizeof(Bnode));
T->data = key;
T->lchild = NULL;
T->rchild = NULL;
return T;
}
else
{
if(T->data>key)
{
//cout<<"T->data: "<<T->data<<" key: "<<key<<endl;
if(T->lchild==NULL)
{
cout<<T->data<<endl;
}
T->lchild = Create_BST(T->lchild,key);
}
else
{
//cout<<"T->data: "<<T->data<<" key: "<<key<<endl;
if(T->rchild==NULL)
{
cout<<T->data<<endl;
}
T->rchild = Create_BST(T->rchild,key);
}
return T;
}
}
int main()
{
int n;
Btree T = NULL;
cin>>n;
if(n>=1){
cout<<"-1"<<endl;
}
while(n--)
{
int key;
cin>>key;
T = Create_BST(T,key);
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
/*
样例输入:
2
45 00 00 34 7a 67 40 00 40 06 63 5a 0a cd 0a f4 7d 38 ca 09 cd f6 00 50 b4 d7 ae 1c 9b cf f2 40 80 10 ff 3d fd d0 00 00 01 01 08 0a 32 53 7d fb 5e 49 4e c8
45 00 00 c6 56 5a 40 00 34 06 e0 45 cb d0 2e 01 0a cd 0a f4 00 50 ce 61 e1 e9 b9 ee 47 c7 37 34 80 18 00 b5 81 8f 00 00 01 01 08 0a 88 24 fa c6 32 63 cd 8d
样例输出:
Case #1
//第四个是总长度
Total length = 52 bytes 52的二进制:32+16+4 00110100 34
//13\14\15\16是源地址
Source = 10.205.10.244 0A.CD.0A.F4
//17\18\19\20
Destination = 125.56.202.9 7d 38 ca 09
// cd f6 21\22
Source Port = 52726
//23\24
Destination Port = 80
Case #2
Total length = 198 bytes 160+16+16=192+6=198 16*12+6 c6
Source = 203.208.46.1
Destination = 10.205.10.244
Source Port = 80
Destination Port = 52833
*/
int swich(int n,char s)
{
if(s-'0'>=49)
{
int a=s-'0'-49;
if(n==2)
return (10+a)*16;
if(n==1)
return 10+a;
}
else
{
if(n==1)
return s-'0';
if(n==2)
return (s-'0')*16;
}
}
int swich2(int n,char s)
{
if(n==1)
{
if(s-'0'>=49)
{
return s-'0'-49+10;
}
else
{
return s-'0';
}
}
if(n==2)
{
if(s-'0'>=49)
{
return (s-'0'-49+10)*16;
}
else
{
return (s-'0')*16;
}
}
if(n==3)
{
if(s-'0'>=49)
{
return (s-'0'-49+10)*16*16;
}
else
{
return (s-'0')*16*16;
}
}
if(n==4)
{
if(s-'0'>=49)
{
return (s-'0'-49+10)*16*16*16;
}
else
{
return (s-'0')*16*16*16;
}
}
}
int main()
{
int T;
scanf("%d",&T);
int num=0;
while(T--)
{
num++;
string s;
//第一次输入把空行符读入,其他的不用读
if(num==1)
cin.get();
getline(cin,s);
//Case #1
printf("Case #%d\n",num);
//Total length = 52 bytes
int Total_length = swich(2,s[9])+swich(1,s[10]);
cout<<"Total length = "<<Total_length<<" bytes"<<endl;
//Source = 10.205.10.244 0A.CD.0A.F4
//cout<<s[36]-'0'<<" "<<s[37]-'0'<<endl;
int four = swich(2,s[36])+swich(1,s[37]);
int three = swich(2,s[39])+swich(1,s[40]);
int two = swich(2,s[42])+swich(1,s[43]);
int one = swich(2,s[45])+swich(1,s[46]);
//cout<<"Source = "<<four<<"."<<three<<"."<<two<<"."<<one<<endl;
//Destination = 125.56.202.9 7d 38 ca 09
int fr = swich(2,s[48])+swich(1,s[49]);
int te = swich(2,s[51])+swich(1,s[52]);
int to = swich(2,s[54])+swich(1,s[55]);
int oe = swich(2,s[57])+swich(1,s[58]);
cout<<"Destination = "<<fr<<"."<<te<<"."<<to<<"."<<oe<<endl;
// cd f6 21\22
//Source Port = 52726
int Source_Port = swich2(4,s[60])+swich2(3,s[61])+swich2(2,s[63])+swich2(1,s[64]);
//cout<<"----"<<swich2(4,s[60])<<"-----"<<swich2(3,s[61])<<"------"<<swich2(2,s[63])<<"---------"<<swich2(1,s[64])<<endl;
cout<<"Source Port = "<<Source_Port<<endl;
//23\24
//Destination Port = 80
int Destination_Port = swich2(4,s[66])+swich2(3,s[67])+swich2(2,s[69])+swich2(1,s[70]);
cout<<"Destination Port = "<<Destination_Port<<endl;
}
return 0;
}