1.有序数组的插入
代码:
bool Insert( List L, ElementType X ){
//溢出
if(L->Last==MAXSIZE-1)
return false;
//插入在最后一位
if(L->Data[L->Last] > X){
L->Data[L->Last+1]=X;
L->Last++;
return true;
}
int tp=0;int find=0;int tmp=0;
for(int i=0;i <= L->Last;i++){
//判断相等时为false
if(L->Data[i] == X) return false;
//当大于时插入,只执行一次
if(L->Data[i] < X && find==0){
tp = L->Data[i];
L->Data[i]=X;
find=1;
}
//将插入位置后所有数据向后移动一位
if(find==1){
tmp=L->Data[i+1];
L->Data[i+1]=tp;
tp=tmp;
}
}
L->Last++;
return true;
}
2.带头结点链栈的操作
代码如下(示例):
Status Push(LinkList L,ElemType e){
LNode *node = (struct LNode*)malloc(sizeof(struct LNode));
node->data = e;
node->next = L->next;
L->next = node;
}
Status Pop(LinkList L,ElemType *e){
*e = L->next->data;
L->next = L->next->next;
}
3.优美的括号序列
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
string input;
while (cin >> input)
{
stack<char>st;
bool perfect = true;
for (auto ch : input)
{
if (ch == '(')
{
st.push(ch);
}
else if (ch == ')')
{
if (st.empty())
{
perfect = false;
break;
}
// else if(st.top()=='(')
// st.pop();
else
{
st.pop();
// perfect = false;
// break;
}
}
}
if (perfect) perfect = st.empty();
cout << (perfect ? "YES" : "NO") << endl;
}
return 0;
}
4.单链表逆转
#下列代码的功能是返回带头结点的单链表L的逆转链表。
List Reverse( List L )
{
Position Old_head, New_head, Temp;
New_head = NULL;
Old_head = L->Next;
while ( Old_head ) {
Temp = Old_head->Next;
Old_head->Next=New_head
;
New_head = Old_head;
Old_head = Temp;
}
L->Next=New_head
;
return L;
}
5. 分糖果(循环线性表)
#include<stdio.h>
#include<stdbool.h>
bool ping(int* a,int n){
for(int i=1;i<n;i++)
if(a[i]!=a[i-1])
return false;
return true;
}
void xun(int* a,int n){
int b[n];
for(int i=0;i<n;i++){
b[i]=a[i]/2;
a[i]=b[i];
}
for(int i=0;i<n;i++){
a[i]+=b[(i+n-1)%n];
}
return;
}
int bu(int* a,int n){
int sum=0;
for(int i=0;i<n;i++){
if(a[i]%2!=0){
a[i]++;
sum++;
}
}
return sum;
}
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++) scanf("%d",&a[i]);
int sum=0;
while(!ping(a,n)){
xun(a,n);
sum+=bu(a,n);
}
printf("%d",sum);
}
6.表达式转换
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char str[27];
int len;
char stack[27];
int top=0;
int index=0;
int IsNum(char c)
{
if(c>='0'&&c<='9'||c=='.')
return 1;
else
return 0;
}
int compare(char a,char b)
{
if(b==')')return 1;
if(a=='('||b=='(')return 0;
switch(b)
{
case '+':
case '-':
return 1;
case '*':
case '/':
switch(a){
case '+':
case '-':
return 0;
case '*':
case '/':
return 1;
}
}
}
int ZhengFu(char a)
{
if(a=='+'||a=='-')
return 1;
else
return 0;
}
void Mainfun()
{
int space=0;
for(index=0;index<len;index++)
{
//先判断是否为数字,为数字就输出
if(IsNum(str[index]))
{
if(space==1)
{
printf(" ");
space =0;
}
printf("%c",str[index]);
}
//+ - 这两个符号要进一步判断是否是正负号
//判断符号为+-号 并且是在第一位 或者 前一位不是数字和‘)’ 那么这个就是正负号
else if(ZhengFu(str[index])&&( (index==0) || (IsNum(str[index-1])!=1&&str[index-1]!=')') ))
{
//如果是正负号,那么符号需要输出,正号不需要输出
if(str[index]=='-')
{
if(space==1)
{
printf(" ");
space=0;
}
printf("%c",str[index]);
}
}
//如果是运算符
else
{
if(top!=0) //栈内有运算符,需要比较
{
if(str[index]==')') //如果是')',就要输出'('之前的所有运算符
{
while(top--)
{
if(stack[top]=='(')break;
else printf(" %c",stack[top]);
}
}
else
{
while(top!=0)
{
if(compare(stack[top-1],str[index])) //来比较运算符
printf(" %c",stack[--top]);
else
break;
}
stack[top++]=str[index];
}
}
else //栈为空,直接入栈
{
stack[top++]=str[index];
}
int j;
for(j=0;j<top;j++)
if(stack[j]!='(')
{
space=1;
break;
}
}
}
while(top!=0)
printf(" %c",stack[--top]);
}
int main()
{
scanf("%s",str);
len=strlen(str);
Mainfun();
return 0;
}
7. 扩展的先序遍历序列创建二叉树
#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct BiTNode{
ElementType data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
BiTree CreatBinTree();
void preorder( BiTree T );
int main()
{
BiTree T = CreatBinTree();
preorder( T );
return 0;
}
void preorder( BiTree T )
{
if(T)
{
printf("%c",T->data);
preorder(T->lchild);
preorder(T->rchild);
}
}
BiTree CreatBinTree()
{
char ch;BiTree T;
scanf("%c",&ch);
if(ch=='#') return
c
;
T=
(BiTree*)malloc(sizeof(BiTree))
;
T->data=ch;
T->lchild=
CreatBinTree()
;
T->rchild=
CreatBinTree()
;
return T;
}
8. 计算二叉树的深度
#include<stdio.h>
#include<stdlib.h>
typedef struct BiNode
{
char data;
struct BiNode *lchild, *rchild;
}BiTNode, *BiTree;
void CreateBiTree(BiTree * T)
{
char ch;
scanf("%c", &ch);
if (ch == '#')
*T = NULL;
else
{
*T = (BiTree)malloc(sizeof(BiTNode));
(*T)->data = ch;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
int Depth(BiTree T)
{
int m, n;
if (
T==NULL
)
return 0;
else
{
m =
Depth(T->lchild)
;
n =
Depth(T->rchild)
;
if (m > n)
return(m + 1);
else
return (n + 1);
}
}
int main()
{
BiTree tree = NULL;
CreateBiTree(&tree);
int depth = Depth(tree);
printf("%d", depth);
return 0;
}
9. 是不是二叉树?
int arr[1000001];
int isTree(TreeNode Head){
memset(arr,0,sizeof(arr));
return dfs(Head);
}
int dfs(TreeNode root){
if(root == NULL){
return 1;
}
if(arr[root->data]){
return 0;
}
arr[root->data] = 1;
return dfs(root->lchild) && dfs(root->rchild);
}
10. 二叉排序树查找操作
BSTree SearchBST(BSTree T,ElemType e){
if(!T||T->data == e)
return T;
else if(e<T->data)
return SearchBST(T->lchild,e);
else
return SearchBST(T->rchild,e);
}
哈夫曼编码
#include<stdio.h>
void f(int *a,int n){
for(int i=n-1;i>0;i--){
for(int j=0;j<i;j++){
if(a[j]<a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return;
}
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++) scanf("%d",&a[i]);
//*a=f(a,n); for(int i=0;i<n;i++) printf("%d ",a[i]);
f(a,n);
int sum=0;
while(1){
if(n==1) break;
sum+=(a[n-1]+a[n-2]);
int t=a[n-1]+a[n-2];
for(int i=n-3;i>=0;i--){
if(a[i]<t){
a[i+1]=a[i];
if(i==0) a[i]=t;
}
else{
a[i+1]=t;
break;
}
}
n--;
}
printf("%d",sum);
}
新浪微博热门话题
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define MAXLENGTH 1000000 // 散列表大小,要保证装填因子小于一定阈值,否则会因频繁的哈希冲突而超时
typedef struct Node
{
char *topic; // 话题
int count; // 当前话题被提及的次数
int last; // 最后一次提及该话题的微博下标(用于去重)
}*Node;
Node hashTable[MAXLENGTH]; // 散列表
Node indices[MAXLENGTH]; // 将散列表紧凑化
int sumOfTopics; // 话题总数
void solution();
void handle(char *text, int numberOfWeibo); // 从文本中查找话题
void normalize(char *topic, int numberOfWeibo); // 将话题形式规范化
int hash(char *topic); // 计算散列值(可能越界)
int mod(int n); // 对散列值取余
void insert(char *topic, int numberOfWeibo); // 将话题插入到散列表
int main()
{
solution();
return 0;
}
void solution()
{
int n;
scanf("%d\n", &n);
char buf[141];
for (int i = 0; i < n; i++)
{
gets(buf);
handle(buf, i);
}
Node maxTitle = indices[0];
int num = 0;
for (int i = 1; i < sumOfTopics; i++)
{
if (indices[i]->count > maxTitle->count)
{
maxTitle = indices[i];
num = 0;
}
else if (indices[i]->count == maxTitle->count)
{
if (strcmp(indices[i]->topic, maxTitle->topic) < 0)
{
maxTitle = indices[i];
}
num++;
}
}
if (maxTitle->topic[0] >= 'a' && maxTitle->topic[0] <= 'z') maxTitle->topic[0] -= 32;
printf("%s\n%d\n", maxTitle->topic, maxTitle->count);
if (num)
{
printf("And %d more ...\n", num);
}
}
void handle(char *text, int numberOfWeibo)
{
char *first;
char *second;
char buf[141];
while ((first = strchr(text, '#')) != NULL && (second = strchr(first + 1, '#')) != NULL)
{
strncpy(buf, first + 1, second - first - 1);
buf[second - first - 1] = '\0';
normalize(buf, numberOfWeibo);
text = second + 1;
}
}
void normalize(char *topic, int numberOfWeibo)
{
char *text = topic;
if (!text || !*text) return;
// 将非字母、数字的字符替换为空格
while (*text)
{
if (*text >= 'A' && *text <= 'Z') *text += 32;
else if (!(*text >= 'a' && *text <= 'z') && !(*text >= '0' && *text <= '9')) *text = ' ';
text++;
}
char *first = NULL;
char *second = NULL;
char *rear = topic + strlen(topic);
// 头部去空格
text = topic;
while (*text == ' ') text++;
memmove(topic, text, rear - text + 1);
rear -= text - topic;
text = topic;
// 中间和尾部去空格
for (; *text; text++)
{
if (*text == ' ')
{
first = second = text;
while (*second == ' ') second++;
if (second - first != 1 && second != rear)
{
memmove(first + 1, second, rear - second + 1);
rear -= second - first - 1;
}
else if(second == rear)
{
*first = '\0';
}
}
}
// 将话题插入散列表中
insert(topic, numberOfWeibo);
}
int hash(char *topic)
{
unsigned n = 0;
while (*topic)
{
n += *topic - 'a';
n <<= 5;
topic++;
}
return n;
}
int mod(int n)
{
while (n < 0) n += MAXLENGTH;
return n % MAXLENGTH;
}
void insert(char *topic, int numberOfWeibo)
{
int key = hash(topic);
int i = 0;
int j = 0;
// 从中间往两侧探测,处理哈希冲突,找空位
for (; i < MAXLENGTH / 2; i++)
{
j = mod(key + i); // 往右侧查找
if (hashTable[j])
{
if (!strcmp(topic, hashTable[j]->topic))
{
if (hashTable[j]->last == numberOfWeibo) return;
++hashTable[j]->count;
hashTable[j]->last = numberOfWeibo;
}
}
else break;
j = mod(key - i); // 往左侧查找
if (hashTable[j])
{
if (!strcmp(topic, hashTable[j]->topic))
{
if (hashTable[j]->last == numberOfWeibo) return;
++hashTable[j]->count;
hashTable[j]->last = numberOfWeibo;
}
}
else break;
}
// 找到空位后,将新的结点插入到散列表中
hashTable[j] = (Node)malloc(sizeof(struct Node));
hashTable[j]->topic = (char*)malloc(strlen(topic) + 1);
strcpy(hashTable[j]->topic, topic);
hashTable[j]->count = 1;
hashTable[j]->last = numberOfWeibo;
indices[sumOfTopics++] = hashTable[j];
}
那就别担心了
#include<stdio.h>
int vis[502], path[502],vec[502][502];
int n, m, cnt, a, b;
int DFS(int x) {
vis[x] = 1;
if(path[x]) return path[x];
for(int i=1;i<=n;i++)
if(vec[x][i])
path[x] += DFS(i);
return path[x];
}
int main(){
int i;
scanf("%d %d",&n,&m);
for(i=1;i<=m;i++) {
scanf("%d %d",&a,&b);
vec[a][b]=1;
}
scanf("%d %d",&a,&b);
path[b] = 1;
cnt = DFS(a);
int flag = 1;
for(i=1;i<=n;i++) {
if(vis[i] && !path[i]) {
flag = 0;
break;
}
}
printf("%d ",cnt);
if(!flag)
printf("No\n");
else
printf("Yes\n");
return 0;
}
地下迷宫探索
#include<stdio.h>
#include<stdlib.h>
#define INFINITY 100000
#define MAXN 1000
typedef int Vertex;
typedef int WeightType;
typedef int DataType;
typedef struct GNode *PtrToGNode;
typedef PtrToGNode MGraph;
struct GNode
{
int Nv;
int Ne;
WeightType **G;
};
typedef struct ENode *PtrToENode;
typedef PtrToENode Edge;
struct ENode
{
Vertex V1, V2;
WeightType W;
};
MGraph CreateGraph(int VertexNum)
{
int i,j;
MGraph Graph = (MGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
Graph->G = (WeightType **)malloc(Graph->Nv*sizeof(WeightType *));
for(i=0; i<Graph->Nv; i++)
{
Graph->G[i] = (WeightType*)malloc(Graph->Nv*sizeof(WeightType));
}
for(i=0; i<Graph->Nv; i++)
{
for(j=0; j<Graph->Nv; j++)
Graph->G[i][j] = INFINITY;
}
return Graph;
}
void InsertEdge(MGraph Graph, Edge E)
{
Graph->G[E->V1][E->V2] = E->W;
Graph->G[E->V2][E->V1] = E->W;
}
MGraph BuildGraph(int N, int M)
{
int i;
MGraph Graph = CreateGraph(N);
Graph->Ne = M;
if(Graph->Ne != 0)
{
Edge E = (Edge)malloc(sizeof(struct ENode));
for(i=0; i<Graph->Ne; i++)
{
scanf("%d %d", &E->V1, &E->V2);
E->V1--;
E->V2--;
E->W = 1;
InsertEdge(Graph, E);
}
}
return Graph;
}
void Visit(Vertex V)
{
printf("%d ", V+1);
}
int *Visited;
int SP; //start point. set the global variable to satisfy the output format
void DFS(MGraph Graph, Vertex V, void(*Visit)(Vertex))
{
Visit(V);
Visited[V] = 1;
int i;
for(i=0; i<Graph->Nv; i++)
{
if(Graph->G[V][i] == 1 && Visited[i] == 0) //V is adjacent to i, and i is not visited
{
DFS(Graph, i, Visit);
if(V==SP)
printf("%d", SP+1);
else
Visit(V);
}
}
}
int main()
{
int i,N,M;
scanf("%d %d %d", &N, &M, &SP);
SP = SP-1;
MGraph Graph = BuildGraph(N, M);
Visited = (int *)malloc(Graph->Nv*sizeof(int));
for(i=0; i<Graph->Nv; i++)
Visited[i] = 0;
DFS(Graph, SP, Visit);
int flag = 1;
for(i=0; i<Graph->Nv; i++)
{
if(Visited[i] == 0)
{
flag = 0;
break;
}
}
if(flag == 0)
printf(" %d", 0);
return 0;
}
深入虎穴
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
vector<int>s[100010];
int track[100010]={0};//这个数组用来标记有前驱的点
int index=0;
void find(int root)//层序搜索函数,搜到不能再往下搜就算找到了最远的门
{
queue<int>q;//建立一个队列
q.push(root);//把根节点加入队列
while(!q.empty())//这就开始时循环着往下遍历了
{
int now = q.front();
q.pop();
index=now;
for(int i=0;i<s[now].size();i++)
{
int sn=s[now][i];
q.push(sn);
}
}
}
int main()
{
int k,m,n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&k);
for(j=0;j<k;j++)
{
scanf("%d",&m);
s[i].push_back(m);
track[m]=1;
}
}
for(i=1;i<=n;i++)
{
if(track[i]==0)//如果没被标记就是无前驱的节点了,从此节点往后遍历用时短
{
find(i);
break;
}
}
printf("%d\n",index);
return 0;
}
公路村村通
#include<stdio.h>
#include<string.h>
int map[1001][1001];
int minCost[1001];//记录与该顶点邻接的边最小权值
int visited[1001];
int N,M;
int min(int a,int b)
{
return a > b ? b : a;
}
int prime()
{
int i,sum=0,v;
for(i=1;i<=N;i++)
minCost[i]=99999999;//初始化为最大
minCost[1]=0;
while(1)
{
v = -1;
for(i=1;i<=N;i++)//找到权值最小的边所对应的顶点
{
if(!visited[i]&&(v==-1||minCost[v]>minCost[i]))
v = i;
}
if(v==-1)
break;//说明顶点都已经经过了
if(minCost[v]==99999999)
return 0;//如果有个孤立顶点,最小权值不会变,可以判断图是否连通
visited[v]=1;
sum+=minCost[v];
for(i=1;i<=N;i++)
{
if(map[v][i])
minCost[i]=min(minCost[i],map[v][i]);//每次更新与顶点邻接的边的最小权值
}
}
for(i=1;i<=N;i++)
{
if(!visited[i])
return 0;
}
return sum;
}
int main()
{
int i;
int city1,city2,cost;
scanf("%d %d",&N,&M);
for(i=0;i<M;i++)
{
scanf("%d %d %d",&city1,&city2,&cost);
map[city1][city2]=cost;//为0的值都是不关联的
map[city2][city1]=cost;
}
int sum = prime();
if(sum)
printf("%d\n",sum);
else
printf("-1\n");
return 0;
}
选择排序
#include<stdio.h>
int main()
{
int n,i=0,min,index,k,h,flag=0;;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
min = a[i];
for(k=i+1;k<n;k++)
{
if (min>a[k])
{
min=a[k];
index = k;
flag=1;
}
}
if(flag==1)
{
a[index] = a[i];
a[i] = min;
}
flag=0;
printf("step %d: ",i+1);
for(h=0;h<n;h++)
printf("%d ",a[h]);
printf("\n");
h=0;
}
printf("sorted array: ");
for(h=0;h<n;h++)
printf("%d ",a[h]);
return 0;
}
寻找大富翁
#include<stdio.h>
int main(){
long long n,i,j,index;
int m,t,count=0;
int a[1000000];
scanf("%lld%d",&n,&m);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++){
if(count==m) break;
if(n<m) {
if(count==n) break;
}
index=i;
for(j=i+1;j<n;j++)
if(a[index]<a[j]) index=j;
t=a[index];
a[index]=a[i];
a[i]=t;
count++;
}
if(n<m){
for(i=0;i<n;i++){
if(i==0) printf("%d",a[0]);
else printf(" %d",a[i]);
}
}
else{
for(i=0;i<m;i++){
if(i==0) printf("%d",a[0]);
else printf(" %d",a[i]);
}
}
return 0;
}
冒泡法排序
#include<stdio.h>
main(){
int n,k,i,j,tmp;
scanf("%d%d",&n,&k);
int data[n];
for(i=0;i<n;i++){
scanf("%d",&data[i]);
}
for(i=0;i<k;i++){
for(j=0;j<n-i-1;j++){
if(data[j]>data[j+1]){
tmp=data[j];
data[j]=data[j+1];
data[j+1]=tmp;
}
}
}
for(i=0;i<n;i++){
printf("%d",data[i]);
if(i<n-1){
printf(" ");
}
}
}
德才论
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
int ID_Number;
int Morality;
int Knowledge;
int Score_Sum;
}student;
int cmp(student stu1,student stu2)
{
if(stu1.Score_Sum>stu2.Score_Sum)
return 1;
else if(stu1.Score_Sum<stu2.Score_Sum)
return 0;
else if(stu1.Morality>stu2.Morality)
return 1;
else if(stu1.Morality<stu2.Morality)
return 0;
else if(stu1.ID_Number<stu2.ID_Number)
return 1;
else return 0;
}
void sort(student stu[],int low,int high)
{
if(low>=high) return;
int i=low,j=high;
student pivot=stu[i];
while(i<j)
{
while(cmp(pivot,stu[j])&&i<j) j--;
stu[i]=stu[j];
while(cmp(stu[i],pivot)&&i<j) i++;
stu[j]=stu[i];
}
stu[i]=pivot;
sort(stu,low,i-1);
sort(stu,i+1,high);
}
int main()
{
int N,L,H;
int qualify;
if(scanf("%d %d %d",&N,&L,&H)==3)
{
student *stu=(student *)malloc(sizeof(student)*N);
for(int i=0,j=i;i<N;i++)
{
scanf("%d %d %d",&stu[j].ID_Number,&stu[j].Morality,&stu[j].Knowledge);
stu[j].Score_Sum=stu[j].Morality+stu[j].Knowledge;
if(stu[j].Morality>=L&&stu[j].Knowledge>=L) //如果不及格,后一项覆盖此项
j++;
qualify=j; //因为j的作用域在for内,只能这样读,当然也可以在for中用qualify替换j
}
int end1=0; //这里开始分类,并找每类考生的首尾下标
for(int i=0;i<qualify;i++)
if(stu[i].Morality>=H&&stu[i].Knowledge>=H)
{
student temp=stu[end1];
stu[end1]=stu[i];
stu[i]=temp;
end1++;
}
int end2=end1;
end1=end1-1;
for(int i=end2;i<qualify;i++)
if(stu[i].Morality>=H)
{
student temp=stu[end2];
stu[end2]=stu[i];
stu[i]=temp;
end2++;
}
int end3=end2;
end2=end2-1;
for(int i=end3;i<qualify;i++)
if(stu[i].Morality>=stu[i].Knowledge)
{
student temp=stu[end3];
stu[end3]=stu[i];
stu[i]=temp;
end3++;
}
int end4=qualify-1;
end3=end3-1;
sort(stu,0,end1);
sort(stu,end1+1,end2);
sort(stu,end2+1,end3);
sort(stu,end3+1,end4);
printf("%d\n",qualify);
for(int i=0;i<qualify;i++)
{
printf("%d %d %d",stu[i].ID_Number,stu[i].Morality,stu[i].Knowledge);
if(i!=qualify-1)
printf("\n");
}
}
return 0;
}
冒泡排序(函数题)
void bubbleSort(int arr[], int n)
{
int flag;
for (int i = n - 1; i >= 0; i--)
{
flag = 0;
for (int j = 0; j<i; j++)
{
if (arr[j]>arr[j + 1])
{
swap(&arr[j], &arr[j + 1]);
flag = 1;
}
}
if (flag == 0)
break;
}
}
简单选择排序(函数题)
void SelectSort(SqList L)
{
int i,j,k;
for(i=1;i<L.Length;i++)
{
k=i;
for(j=i+1;j<=L.Length;j++)
{
if(L.elem[j]<L.elem[k]) k=j;
}
if(k!=i)
{
int t=L.elem[k];
L.elem[k]=L.elem[i];
L.elem[i]=t;
}
}
}
口罩发放
#include<bits/stdc++.h>
using namespace std;
const int N=30010;
struct node
{
int id;
string name;
string sfz;//身份证
int stqk;//身体健康情况
int h,m;
bool st;//身份证是否合法
}E[N];
map<string,int> M;
int idx;
bool cmp(node x1,node x2)
{
if(x1.h==x2.h)
{
if(x1.m==x2.m)
return x1.id<x2.id;
return x1.m<x2.m;
}
return x1.h<x2.h;
}
bool test(string op)
{
if(op.size()!=18)
return false;
for(int i=0;i<18;i++)
if(op[i]<'0'||op[i]>'9')
return false;
return true;
}
int main()
{
int D,P;
cin>>D>>P;
for(int i=1;i<=D;i++)
{
int t,s;
cin>>t>>s;
node e[t+10];
for(int j=1;j<=t;j++)
{
cin>>e[j].name>>e[j].sfz>>e[j].stqk;
scanf("%d:%d",&e[j].h,&e[j].m);
e[j].id=j;
e[j].st=test(e[j].sfz);
if(e[j].stqk&&e[j].st)
E[idx++]=e[j];
}
sort(e+1,e+1+t,cmp);
for(int j=1;j<=t&&s;j++)
{
if(M[e[j].sfz]==0)
M[e[j].sfz]=-2*P;
if(e[j].st&&(i-M[e[j].sfz]>=P+1))
{
cout<<e[j].name<<" "<<e[j].sfz<<endl;
M[e[j].sfz]=i;
s--;
}
}
}
for(int i=0;i<idx;i++)
{
if(M[E[i].sfz]!=-1)
{
cout<<E[i].name<<" "<<E[i].sfz<<endl;
M[E[i].sfz]=-1;
}
}
return 0;
}
统计专业人数
int countcs( struct ListNode *head )
{
int count = 0;
struct ListNode *p;
p = head;
while (p)
{
if (p->code[1] == '0' && p->code[2] == '2')
count++;
p = p->next;
}
return count;
}
求二叉树高度
int GetHeight( BinTree BT ){
if(BT==NULL)return 0;
int x,y;
x=GetHeight(BT->Left)+1;
y=GetHeight(BT->Right)+1;
return x>y?x:y;
}
先输出叶节点
void PreorderPrintLeaves( BinTree BT ){
if(BT!=NULL){
if(BT->Left==NULL&&BT->Right==NULL)printf(" %c",BT->Data);
PreorderPrintLeaves(BT->Left);
PreorderPrintLeaves(BT->Right);
}
}
邻接矩阵存储图的深度优先遍历
void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) )
{
Vertex j;
Visited[V] = true;//表示从V开始访问
Visit(V);//访问V,其实抛开题来讲这里一般都是打印V
for(j = 0;j < Graph->Nv;j++)
{
if(Graph->G[V][j] == 1 && !Visited[j])//邻接矩阵等于1代表有边,此时如果还没被访问就递归调用
DFS(Graph,j,Visit);
}
}
二分查找
目录