//2012年计算机学院机试 A 二叉排序树
//2018/3/3
//by ben yafang
//done
#include<stdio.h>
#include<cstdio>
using namespace std;
struct node {
int value;
node *lchild;
node *rchild;
}*root;
void insert(node *root,int x){
if(x < root->value){//插入左子树
if(root->lchild) insert(root->lchild,x);
else {//左子树插入节点,需要新申请一个node空间
printf("%d\n",root->value);
node *p = new node;//新建节点赋值
p->value = x;
p->lchild = p->lchild = NULL;
root->lchild = p;//链接节点
// printf("%d\n",root->value);
return;
}
}
else{//插入右子树
if(root->rchild) insert(root->rchild,x);
else{
printf("%d\n",root->value);
node *p = new node;
p->value = x;
p->lchild = p->rchild = NULL;
root->rchild = p;
// printf("%d\n",root->value);
return ;
}
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
int a;
scanf("%d",&a);
node *T = new node;
T->value = a;
T->lchild = T->rchild = NULL;
root = T;
printf("-1\n");
for(int i = 1;i < n;i++){
scanf("%d",&a);
insert(root,a);
}
}
return 0;
}
//2012年计算机学院机试 B 二进制
//2018/3/3
//by ben yafang
//done
//AC
#include<stdio.h>
int a[10005]; //存储转换二进制的0/1
int len;
int main(){
int n;
scanf("%d",&n);
while(n--){
len = 0;//记录数组中存储的二进制数组个数
int p;
scanf("%d",&p);
if(p == 0)printf("0\n");
else{
while(p){
a[len++] = p % 2;
p = p / 2;
}
for(int i = len-1;i >= 0;i--) printf("%d",a[i]);
printf("\n");
}
}
}
//2012年计算机学院机试 D ip数据包解析
//2018/3/20
//by ben yafang
//doing
//gets strtol进制转换
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
using namespace std;
int ip_length(char ip[],int start,int n){//从下标start开始,一共有n个字符
//将子串转换成10进制
char pre_ip[n+1];
// string s;
int index = 0;
for(int i = start;i < start+n;i++){
pre_ip[index++] = ip[i];
}
pre_ip[index] = '\0';//最后要赋值结束符
// s.append(a[i]);//需要满足a也是string类型
return strtol(pre_ip,NULL,16); //16进制转换成10进制
//stdlib
}
void ip_address(char ip[],int start,int n){
// char pre_ip[n];
int index = 0;
int x;//保存每次进制转换的10进制数
int flag = 1;//标记输出‘.’
for(int i = start;i < start+n;i+=2){
// pre_ip[index++] = ip[i];
x = ip_length(ip,i,2);
printf("%d",x);
if(flag <= 3){
flag++;
printf(".");
}
}
printf("\n");
}
int main(){
int t;
char s[1000];
char ip[1000];
while(scanf("%d",&t)!=EOF && t!=0){
for(int index = 1;index <= t;index++){
memset(s,0,sizeof s);
memset(ip,0,sizeof ip);//memset要求是固定长度数组
cin.get();
gets(s);
int len = strlen(s);
int i,j;
for(i=j=0;i < len;i++){
if(s[i] != ' '){
ip[j++] = s[i];
}
}
ip[j] = '\0';
printf("Case #%d\n",index);
int length = ip_length(ip,4,4);//从第4位开始,一共4位
printf("Total length = %d bytes\n",length);
printf("Source = ");
ip_address(ip,24,8);
printf("Destination = ");
ip_address(ip,32,8);
int addr = ip_length(ip,1,1) * 4 * 2;
printf("Source Port = %d\n",ip_length(ip,addr,4));
printf("Destination Port = %d\n",ip_length(ip,addr+4,4));
}
}
}
//2012年计算机学院机试 C 矩阵幂
//2018/3/3
//by ben yafang
//done 全局变量 函数传参
#include<stdio.h>
#include<string.h>
//using namespace std;
int n;//定义n为全局变量,以便在函数中调用
int input[10][10];//存储输入数据
int temp[10][10];//存储计算过程中的中间数据
int result[10][10]; //存储输出结果 同时参与下一次运算
void MatrixMultiply(){
int i,j,k;
memset(temp,0,sizeof(temp));//每次调用函数时,需要对temp初始化
for(i = 0;i < n;i++){
for(j = 0;j < n;j++){
for(k = 0;k < n;k++){
temp[i][j] += result[i][k]*input[k][j];//上一次计算结果与原始数据乘积
}
}
}
for(i = 0;i < n;i++){
for(j = 0;j < n;j++){
result[i][j] = temp[i][j];//更新中间变量,可作为最后输出
}
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int k;//2--10,1--5,n*n,k次方
scanf("%d %d",&n,&k);
int i,j;
for(i = 0;i < n;i++){
for(j = 0;j < n;j++){
scanf("%d",&input[i][j]);
result[i][j] = input[i][j];
}
}
k--;//k=1时不进行函数运算
while(k--){//进行矩阵k次方
MatrixMultiply();//数组是全局变量
}
for(i = 0;i < n;i++){
for(j = 0;j < n;j++){
printf("%d",result[i][j]);
if(j < n-1) printf(" ");
if(j == n-1)printf("\n");
}
}
}
}