#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
//顺序表的存储结构定义
#define LIST_INIT_SIZE 11
#define LISTINCREMENT 10
typedef int ElemType; //假设线性表中的元素均为整型
typedef struct
{
ElemType *elem; //存储空间基地址
int length; //表中元素的个数
int listsize; //表容量大小
} SqList; //顺序表类型定义
Status InitList_Sq(SqList &L)
{
//初始化L为一个空的有序顺序表
L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem)
exit(OVERFLOW);
L.listsize = LIST_INIT_SIZE;
L.length = 0;
return OK;
}
Status ListInsert_Sq(SqList &L, int pos, ElemType e)
{
if (L.length == L.listsize)
{
ElemType *tmp = (ElemType *)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
if (!tmp)
exit(OVERFLOW);
L.elem = tmp;
}
if (pos > L.length + 1 || pos < 1)
{
return ERROR;
}
for (int i = L.length; i > pos - 1; i--)
{
L.elem[i] = L.elem[i - 1];
}
L.elem[pos - 1] = e;
L.length++;
return OK;
}
Status ListDelete_Sq(SqList &L, int pos)
{
for (int i = pos - 1; i < L.length; i++)
{
L.elem[i] = L.elem[i + 1];
}
L.length--;
return OK;
}
void ListPrint_Sq(SqList L)
{
for (int i = 0; i < L.length - 1; i++)
{
printf("%d ", L.elem[i]);
}
printf("%d\n", L.elem[L.length - 1]);
}
int main()
{
SqList L;
if (InitList_Sq(L) != OK)
{
printf("InitList_Sq: 初始化失败!!!\n");
return -1;
}
int n = 0;
int tmp = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &tmp);
ListInsert_Sq(L, i, tmp);
}
ElemType min = 0, max = 0;
scanf("%d", &min);
scanf("%d", &max);
// if (min > max)
// {
// tmp = min;
// min = max;
// max = tmp;
// }
for (int i = 0; i < L.length;)
{
if (min <= L.elem[i] && L.elem[i] <= max)
{
ListDelete_Sq(L, i + 1);
}
else
{
i++;
}
}
ListPrint_Sq(L);
return 0;
}
//库函数头文件包含
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//函数状态码定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType; //假设线性表中的元素均为整型
typedef struct LNode
{
ElemType data;
struct LNode *next;
} LNode, *LinkList;
Status ListCreate_L(LinkList &L)
{
L = (LNode *)malloc(sizeof(LNode));
if (!L)
{
exit(OVERFLOW);
}
L = (LNode *)malloc(sizeof(LNode));
LNode *tmp = L;
int n = 0, num = 0;
while (scanf("%d", &num) != EOF)
{
if (num == -1)
{
break;
}
tmp->next = (LNode *)malloc(sizeof(LNode));
tmp->next->data = num;
tmp = tmp->next;
n++;
}
tmp->next = NULL;
L->data = n;
return OK;
}
void ListPrint_L(LinkList &L)
{
if (L->next==NULL) {
printf("NULL");
return ;
}
//输出单链表
LNode *p = L->next; // p指向第一个元素结点
while (p != NULL)
{
if (p->next != NULL)
printf("%d ", p->data);
else
printf("%d", p->data);
p = p->next;
}
}
LNode *LinkOrderList(LinkList &L1, LinkList &L2)
{
//确定头部最小的序列
if (!(L1->next)) {
return L2;
}
if (!(L2->next)) {
return L1;
}
LinkList p = L1->next, t = L2->next;
if ((L1->next->data) > (L2->next->data))
{
p = L2->next, t = L1->next;
}
while (p->next != NULL && t != NULL)
{
if ((p->next->data) > (t->data))
{
LinkList s = p->next;
p->next = t;
t = s;
}
else
{
p = p->next;
}
}
p->next = t;
return L1;
}
int main()
{
LinkList L1, L2;
if (ListCreate_L(L1) != OK)
{
printf("表创建失败!!!\n");
return -1;
}
if (ListCreate_L(L2) != OK)
{
printf("表创建失败!!!\n");
return -1;
}
LNode *head = LinkOrderList(L1, L2);
ListPrint_L(head);
return 0;
}
//库函数头文件包含
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//函数状态码定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType; //假设线性表中的元素均为整型
typedef struct LNode
{
ElemType data;
struct LNode *next;
} LNode, *LinkList;
//打印链表中的数据
void ListPrint_L(LinkList &L)
{
if (L->next == NULL)
{
printf("NULL");
return;
}
//输出单链表
LNode *p = L->next; // p指向第一个元素结点
while (p != NULL)
{
if (p->next != NULL)
printf("%d ", p->data);
else
printf("%d", p->data);
p = p->next;
}
}
//为链表添加结点,需要传入直接前驱
LNode *addLNode(LinkList &L, ElemType e)
{
L->next = (LinkList)malloc(sizeof(LNode));
if (!(L->next))
{
exit(OVERFLOW);
}
L->next->data = e;
L->next->next = NULL;
return L->next;
}
Status ListCreate_L(LinkList &L)
{
L = (LNode *)malloc(sizeof(LNode));
L->next = NULL;
if (!L)
{
exit(OVERFLOW);
}
LNode *p = L;
int num = 0;
while (scanf("%d", &num) != EOF)
{
if (num == -1)
{
break;
}
p = addLNode(p, num);
}
return OK;
}
LNode *LinkOrderList(LinkList &L1, LinkList &L2)
{
LNode *head = (LNode *)malloc(sizeof(LNode));
head->next = NULL;
//列表有一个为空的情况
if ((L1->next) && (L2->next))
{
LNode *s = L1, *t = L2, *p = head;
while ((s->next) && (t->next))
{
if ((s->next->data) > (t->next->data))
{
t = t->next;
}
else if ((s->next->data) < (t->next->data))
{
s = s->next;
}
else
{
p = addLNode(p, s->next->data);
s = s->next;
t = t->next;
}
}
}
return head;
}
int main()
{
LNode *L1 = (LNode *)malloc(sizeof(LNode)), *L2 = (LNode *)malloc(sizeof(LNode));
ListCreate_L(L1);
ListCreate_L(L2);
LNode *head = LinkOrderList(L1, L2);
ListPrint_L(head);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef long long ll;
typedef unsigned long long ull;
struct Node{
int l, u;
} item1[1004], item2[1004];
int c[3000];
int d[3000];
int main(){
int m, n;
int index = -1;
int cnt1 = 0, cnt2 = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++){
int left, up;
scanf("%d%d", &left, &up);
item1[cnt1].l = left;
item1[cnt1++].u = up;
c[up] += left;
index = (index > up) ? index : up;
}
scanf("%d", &m);
for(int i = 1; i <= m; i++){
int left, up;
scanf("%d%d", &left, &up);
item2[cnt2].l = left;
item2[cnt2++].u = up;
c[up] += left;
index = (index > up) ? index : up;
}
int indexxx = item1[0].u + item2[0].u;
for(int i = 0; i < cnt1; i++){
for(int j = 0; j < cnt2; j++){
d[item1[i].u + item2[j].u] += item1[i].l * item2[j].l;
}
}
int flag = 0;
if(cnt1 == 0 || cnt2 == 0) printf("0 0\n");
else {
for(int i = indexxx; i >= 0; i--){
if(d[i]) {
if(flag) printf(" ");
printf("%d %d", d[i], i);
flag = 1;
}
}
if(flag == 0) printf("0 0");
printf("\n");
}
flag = 0;
for(int i = index; i >= 0; i--){
if(c[i]) {
if(flag) printf(" ");
printf("%d %d", c[i], i);
flag = 1;
}
}
if(flag == 0) printf("0 0");
return 0;
}
#include<stdio.h>
#include<malloc.h>
struct Node{
int data;
struct Node *next;
};
int main()
{
int k,t,i;
scanf("%d",&k);
struct Node *head,*p;
head=(struct Node *)malloc(sizeof(struct Node));
head->next=NULL;
while(scanf("%d",&t)&&t>=0)
{
p=(struct Node *)malloc(sizeof(struct Node));
p->data=t;
p->next=head->next;
head->next=p;
}
for(i=0;i<k;i++)
head=head->next;
if(head) printf("%d",head->data);
else printf("NULL");
}
#include<stdio.h>
int main()
{
//输入栈容量和操作数,用length表示栈内现有元素个数
int Ssize, oper, counter, length = 0,flag = 0;
scanf("%d %d ",&oper, &Ssize);
for(counter = 0; counter < oper; counter ++) {
char chr;
//s入栈,length加一,x出栈,length减一
while((chr = getchar()) != '\n') {
if(chr == 'S') {
length++;
if(length > Ssize)
flag = 1;
}
if(chr == 'X') {
length--;
if(length < 0)
flag = 1;
}
}
if(length == 0 && flag == 0)
printf("YES\n");
else
printf("NO\n");
length = 0;
flag = 0;
}
return 0;
}
#include<iostream>
#include<string>
#include<map>
#include<stack>
using namespace std;
int main()
{
string a;
stack<char>s;
int c=0,printk=0;
cin>>a;
map<char,int>p;
p['*']=p['/']=1;
p['(']=p[')']=2;
for(int i=0;i<a.length();i++)
{
if((i<1||a[i-1]=='(')&&(a[i]=='+'||a[i]=='-')||a[i]=='.'||a[i]>='0'&&a[i]<='9')
{
if(printk)
{
cout<<" ";
}
printk++;
if(a[i]!='+')cout<<a[i];
while(a[i+1]=='.'||a[i+1]>='0'&&a[i+1]<='9')
{
i++;
cout<<a[i];
}
}
else
{
if(a[i]==')')
{
while(s.size()>0&&s.top()!='(')
{
cout<<" "<<s.top();
s.pop();
}
s.pop();
}
else if(s.size()==0||p[a[i]]>p[s.top()])
{
s.push(a[i]);
}
else
{
while(s.size()>0&&s.top()!='(')
{
cout<<" "<<s.top();
s.pop();
}
s.push(a[i]);
}
}
}
while(s.size())
{
cout<<" "<<s.top();
s.pop();
}
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
stack<string> st;
string s;
string res;
while(1)
{
getline(cin,s);
if(s==".")
break;
res=res+s;
}
for(int i=0;i<res.length();i++)
{
if(res[i]=='/'&&res[i+1]=='*'&&(i+1<res.length()))
{
st.push(res.substr(i,2));
i++;
}
else if(res[i]=='*'&&res[i+1]=='/'&&(i+1<res.length()))
{
if(!st.empty()&&st.top()=="/*")
st.pop();//出栈
else
{
cout<<"NO"<<endl;
if(st.empty())
cout<<"?-*/";
else
cout<<st.top()<<"-?";
return 0;
}
i++;
}
else if(res[i]=='('||res[i]=='{'||res[i]=='[')
{
st.push(string(1,res[i])); //用1个字符res[i]初始化
}
else if(res[i]=='}')
{
if(!st.empty()&&st.top()=="{")
st.pop();
else
{
cout<<"NO"<<endl;
if(st.empty())
cout<<"?-"<<res[i];
else
cout<<st.top()<<"-?";
return 0;
}
}
else if(res[i]==']')
{
if(!st.empty()&&st.top()=="[")
st.pop();
else
{
cout<<"NO"<<endl;
if(st.empty())
cout<<"?-"<<res[i];
else
cout<<st.top()<<"-?";
return 0;
}
}
else if(res[i]==')')
{
if(!st.empty()&&st.top()=="(")
st.pop();
else
{
cout<<"NO"<<endl;
if(st.empty())
cout<<"?-"<<res[i];
else
cout<<st.top()<<"-?";
return 0;
}
}
}
if(st.empty())
cout<<"YES";
else
{
cout<<"NO"<<endl;
cout<<st.top()<<"-?";
}
return 0;
}
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int root[n];
int len = 0;
int k;
while(n--){
cin>>k;
if(len == 0 || root[len-1] < k){
root[len++] = k;
}else{
int one = 0,two = len-1;
while(one < two){
int mid = one + (two-one)/2;
if(root[mid] > k){
two = mid - 1;
}else{
one = mid + 1;
}
}
root[one] = k;
}
}
cout<<len;
return 0;
}
#include<stdio.h>
#include<string.h>
char a[10001];
int judge(int i, int j) {
while (i <= j)
if (a[i++] != a[j--])
return 0;
return 1;
}
int main() {
gets(a);
int maxn = 0;
int len=strlen(a);
for (int i = 0; i <len; ++i)
for (int j =len- 1; j >= i; --j)
if (judge(i, j))
maxn = maxn>(j - i + 1)?maxn:(j-i+1);
printf("%d",maxn);
return 0;
}
#include<stdio.h>
int main()
{
int t,n,i,j,f;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int a[n][n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
{
f=1;
for(j=0;j<n;j++)
{
if(i>j&&a[i][j]!=0)
{
f=0;
break;
}
}
if(f==0)
break;
}
if(f==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int post[31], in[31], N;
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
}* Tree;
Tree BuildTree(int* post, int* in, int n);
void PreOrder(Tree t);
int main()
{
scanf("%d", &N);
for (int i = 0; i < N; ++i)
{
scanf("%d", &post[i]);
}
for (int i = 0; i < N; ++i)
{
scanf("%d", &in[i]);
}
printf("Preorder:");
Tree t = BuildTree(post, in, N);
PreOrder(t);
return 0;
}
Tree BuildTree(int* post, int* in, int n)
{
if (n == 0)
return NULL;
int* mid = in;
while (*mid != *(post + n - 1))
mid++;
Tree T = (Tree)malloc(sizeof(struct Node));
T->data = *mid;
int m = mid - in;
T->left = BuildTree(post, in, m);
T->right = BuildTree(post + m, mid + 1, n - m - 1);
return T;
}
void PreOrder(Tree t)
{
if (t)
{
printf(" %d", t->data);
PreOrder(t->left);
PreOrder(t->right);
}
}
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 12
#define Null -1
struct TreeNode{
char Data;
int left;
int right;
}T1[MAXSIZE], T2[MAXSIZE];
typedef struct TreeNode Tree;
int BuildTree(struct TreeNode T[])
{
int N,Root = -1, i;
scanf("%d", &N);
/*
int *c;
c = (int *)malloc(sizeof(int) * N);*/
int c[MAXSIZE];
char cl, cr;
if(N)
{
for( i = 0; i< N; i++) c[i] = 0;
for( i = 0; i < N; i++)
{
scanf("\n%c %c %c", &T[i].Data, &cl, &cr);
if(cl != '-')
{
T[i].left = cl - '0';
c[cl - '0'] = 1;
}
else
T[i].left = Null;
if(cr != '-')
{
T[i].right = cr - '0';
c[cr - '0'] = 1;
}
else
T[i].right = Null;
}
for( i = 0; i < N; i++)
if(!c[i]) break;
Root = i;
}
return Root;
}
int Iso(int R1, int R2)
{
if( (R1 == Null) && (R2 == Null) )
return 1;
if((R1 != Null && R2 == Null) || (R1 == Null && R2 != Null))
return 0;
if(T1[R1].Data != T2[R2].Data)
return 0;
if(T1[R1].left == Null && T2[R2].left == Null)
return Iso(T1[R1].right, T2[R2].right);
if((T1[R1].left != Null && T2[R2].left != Null) && (T1[T1[R1].left].Data == T2[T2[R2].left].Data))
return (Iso(T1[R1].left, T2[R2].left) && Iso(T1[R1].right, T2[R2].right));
else
return (Iso(T1[R1].left, T2[R2].right) && Iso(T1[R1].right, T2[R2].left));
}
int main() {
int R1, R2, result;
R1 = BuildTree(T1);
R2 = BuildTree(T2);
result = Iso(R1, R2);
if(result)
printf("Yes\n");
else
printf("No\n");
return 0;
}
#include<stdio.h>
#include<string.h>
struct Node{
char name[15];//名字
char father[15];//父亲名字
int num;//空格数
}people[101];
int main()
{
int n,m;//n是家谱中名字的数目,m是家谱中陈述语句的数目
scanf("%d %d",&n,&m);
getchar();//从缓冲区读入\n
char temp[75];
int k,i;
//读取家谱中的名字
for(k = 0;k < n;k++)
{
gets(temp);
people[k].num = 0;//初始化空格数
for(i = 0;i < strlen(temp);i++)//添加家谱的名字
{
if(temp[i] == ' ')
people[k].num++;
else
{
strcpy(people[k].name,temp+i);//复制名字
break;
}
}
if(people[k].num == 0)
{
strcpy(people[i].father,"root");//空格数为0则为根节点
}
else
{
for(i = k-1;i >= 0;i--)
{
//寻找父节点
if(people[k].num > people[i].num)
{
strcpy(people[k].father,people[i].name);
break;
}
}
}
}
char a[15],b[15],c[15],d[15];
char temp1[15],temp2[15];
//读取家谱中的陈述句
for(i = 0;i < m;i++)
{
scanf("%s %s %s %s %s %s",a,d,d,b,d,c);
if(b[0] == 'c')
{//X is a child of Y
for(k = 0; k < n;k++)
{
if(!strcmp(people[k].name,a))
{
if(!strcmp(people[k].father,c))
printf("True\n");
else
printf("False\n");
break;
}
}
}
if(b[0] == 'p')
{//X is the parent of Y
for(k = 0;k < n; k++)
{
if(!strcmp(people[k].name,c))
{
if(!strcmp(a,people[k].father))
printf("True\n");
else
printf("False\n");
break;
}
}
}
if(b[0] == 's')
{//X is a sibling of Y
for(k = 0;k < n;k++)
{
if(!strcmp(a,people[k].name))
strcpy(temp1,people[k].father);
if(!strcmp(c,people[k].name))
strcpy(temp2,people[k].father);
}
if(!strcmp(temp1,temp2))
printf("True\n");
else
printf("False\n");
}
if(b[0] == 'd')
{//X is a descendant of Y
for(k = 0;k < n;k++)
{
if(!strcmp(people[k].name,a))
{
strcpy(temp1,people[k].father);
break;
}
}
//循环结束条件:成功找到X的祖先是c或者没有找到
while( strcmp(temp1,c)&&strcmp(temp1,"root") )
{
for(k = 0;k < n;k++)
{
if(!strcmp(people[k].name,temp1))
{
strcpy(temp1,people[k].father);
break;
}
}
}
if(!strcmp(temp1,"root"))
printf("False\n");
else
printf("True\n");
}
if(b[0] == 'a')
{//X is an ancestor of Y
for(k = 0;k < n;k++)
{
if(!strcmp(people[k].name,c))
{
strcpy(temp1,people[k].father);
break;
}
}
while( strcmp(temp1,a)&&strcmp(temp1,"root") )
{
for(k = 0;k < n;k++)
{
if(!strcmp(people[k].name,temp1))
{
strcpy(temp1,people[k].father);
break;
}
}
}
if(!strcmp(temp1,"root"))
printf("False\n");
else
printf("True\n");
}
}
return 0;
}
#include <stdio.h>
int main(){
int n, i, j = 0, k = 0;
scanf("%d", &n);
int a[n], b[n];
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n; i++)
scanf("%d", &b[i]);
while(j + k < n - 1){
if(a[j] >= b[k])
k++;
else
j++;
}
printf("%d", a[j]>b[k]?b[k]:a[j]);
return 0;
}
#include <iostream>
using namespace std;
struct Node{
int data;
Node * left;
Node * right;
int flag;
};
typedef Node* Tree;
Tree insert(Tree BT,int x){
if(!BT){
BT=new(Node);
BT->data=x;
BT->left=BT->right=NULL;
BT->flag=0;
}
else if(x<BT->data)
BT->left=insert(BT->left,x);
else if(x>BT->data)
BT->right=insert(BT->right,x);
return BT;
}
bool check(Tree T,int x){
if(T->flag){
if(x<T->data)
return check(T->left,x);//虽然说任何arrow前面都得加入是否判断该地址是否为NULL,但是x的个数其实就是结点个数
else if(x>T->data)
return check(T->right,x);
else
return false;
}
else{
if(x==T->data){
T->flag=1;
return true;
}
else
return false;
}
}
void reset(Tree T){//每次标记要清0
if(T->left)
reset(T->left);
if(T->right)
reset(T->right);
T->flag=0;
}
int main(){
int n,l,x;
while(cin>>n&&n){
cin>>l;
Tree BT=NULL;
for(int i=0;i<n;i++){
cin>>x;
BT=insert(BT,x);
}
for(int i=0;i<l;i++){
bool judge=true;
reset(BT);
for(int i=0;i<n;i++){
cin>>x;
if(!check(BT,x))
judge=false;
}
judge?cout<<"Yes"<<endl:cout<<"No"<<endl;
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
typedef struct TNode* BinTree;
struct TNode{
int Data;
BinTree Left;
BinTree Right;
};
BinTree Insert( BinTree BST, int x )
{
if(!BST){
BST=(BinTree)malloc(sizeof(struct TNode));
BST->Data=x;
BST->Left=BST->Right=NULL;
}
else{
if(x>BST->Data) BST->Left=Insert( BST->Left, x );
else if(x<BST->Data) BST->Right=Insert( BST->Right, x );
}
return BST;
}
void Out(BinTree BST)
{
int i,front=0,tail=0,p=0;
BinTree a[30],x;
a[++tail]=BST;
while(tail!=front){
x=a[++front];
if(p==0) {
printf("%d",x->Data);
p=1;
}
else printf(" %d",x->Data);
if(x->Left) a[++tail]=x->Left;
if(x->Right) a[++tail]=x->Right;
}
}
void Pan(BinTree BST)
{
int i,front=0,tail=0,q=0;
BinTree a[30],x;
a[++tail]=BST;
while(tail!=front){
x=a[++front];
if(!x->Left&&x->Right){
printf("\nNO");
return;
}
if(x->Left&&x->Right){
a[++tail]=x->Left;
a[++tail]=x->Right;
}
if(!x->Right&&x->Left){
a[++tail]=x->Left;
q=1;
}
if(!x->Right&&!x->Left) q=1;
while(q==1&&tail!=front){
x=a[++front];
if(x->Left||x->Right){
printf("\nNO");
return;
}
}
}
printf("\nYES");
}
int main()
{
int n,i,x;
BinTree BST=NULL;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&x);
BST=Insert(BST, x);
}
Out(BST);
Pan(BST);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
vector<int> ll;
int num;
cin>>num;
int input;
int i;
for(i=0;i<num;++i){
cin>>input;
ll.push_back(input);
}
sort(ll.begin(),ll.end());
for(i=0;i<ll.size();++i){
if(i==ll.size()-1){
cout<<ll[i];
}else{
cout<<ll[i]<<" ";
}
}
return 0;
}