不挂科two

标题多项式求和#include<stdio.h>int main(){ int n,i; float x,a[300000],s; scanf("%d %f",&n,&x); for(i=0;i<=n;i++) { scanf("%f",&a[i]); } s=a[0]; for(i=1;i<=n;i++) { s=a[i]+xs; } printf("%.3f\n",s); return 0;}最大子列和问题#include<stdio.h>int main(){ int n,i,max=0,lmax=0; int a[100000]; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { lmax=lmax+a[i]; if(lmax<0) lmax=0; if(lmax>max) max=lmax; } printf("%d",max); return 0;} 两个有序序列的中位数#include<stdio.h>int main(){ int n,i,j=0,k=0; scanf("%d",&n); int a[100000],b[100000]; 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<stdio.h>int main(){ int i,n,a[100],b[100],m; scanf("%d %d",&m,&n); for(i=0;i<m;i++) scanf("%d",&a[i]); for(i=0;i<m;i++) { b[i]=a[(i+n)%m]; } for(i=0;i<m;i++) if(i0) printf("%d",b[i]); else printf(" %d",b[i]); return 0;}数列求和-加强版#include<stdio.h>int main(){ int a,n,i,k,x,y; scanf("%d %d",&a,&n); char s[100001]; k=0; y=0; if(n0) {printf(“0”); return 0; } while(n>0) { x=na; s[k++]=(x+y)%10+‘0’; y=(x+y)/10; n–; } if(y>0) {s[k++]=y+‘0’;s[k]=’\0’;} else s[k]=’\0’; for(i=k-1;i>=0;i–) printf("%c",s[i]); return 0;}一元多项式求导#include<stdio.h>typedef struct{ int n,b;}POLY;int main(){ POLY a[1000]; int i; while(scanf("%d %d",&a[i].b,&a[i].n)2) { if(i0) { if(a[i].n0) { printf(“0 0”); return 0; } if(a[i].n!=0) printf("%d %d",a[i].ba[i].n,a[i].n-1); } else if(a[i].n!=0) printf(" %d %d",a[i].ba[i].n,a[i].n-1); i++; } return 0;}顺序表的建立及遍#include<stdio.h>int main(){ int n,i,a[1000]; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) if(i0) printf("%d",a[i]); else printf(" %d",a[i]); return 0;}最长连续递增子序列#include<stdio.h>int main(){ int n,i,max=1,lmax=0,a[100000],start,lstart=0; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { start=i; lmax=1; while(a[i]<a[i+1]&&i<n-1) { i++; lmax++; if(lmax>max) {max=lmax; lstart=start;} } } for(i=lstart;i<lstart+max;i++) if(ilstart) printf("%d",a[i]); else printf(" %d",a[i]); return 0;}堆栈操作合法性#include <bits/stdc++.h>using namespace std;int main() { int n,m; string a; cin>>n>>m; while(n–) { int flag=1; int sum=0; cin>>a; for(int i=0;i<a.size()/a[i]也可,但是不知是什么意思/;i++) { if(a[i]‘S’) { sum++; if(sum>m)//入栈判断是否栈满 { flag=0; break; } } if(a[i]‘X’) { sum–; if(sum<0)//出栈判断是否栈空 { flag=0; break; } } } if(flag0||sum!=0) cout<<“NO”<<endl; else cout<<“YES”<<endl; } return 0;}银行业务队列简单模拟#include <bits/stdc++.h>using namespace std;queue q1, q2;int main(){ int n; cin >> n; while(n–){ int m; cin >> m; if(m%2 != 0) q1.push(m); else q2.push(m); } while(!q1.empty()){ int cnt = 2; while(cnt >= 1 && !q1.empty()) { if(cnt != 2) cout<<" “; cout<<q1.front(); q1.pop(); cnt–; }if(!q2.empty()){ cout<<” “<<q2.front()<<” “; q2.pop(); } } int i = 0; while(!q2.empty()){ if(i++) cout<<” “; cout<<q2.front(); q2.pop(); } return 0;}二分查找#include<stdio.h>int main(){ int n,a[1005],x,i,sum=0; scanf(”%d",&n); int left=0,right=n-1,mid; for(i=0;i<n;i++) {scanf("%d",&a[i]);} scanf("%d",&x); while(left<=right) { sum++; mid=(left+right)/2; if(xa[mid]) break; else if(x<a[mid]) right=mid-1; else left=mid+1; } if(left>right) printf("-1\n%d",sum); else printf("%d\n%d",mid,sum); return 0;} 顺序存储的二叉树的最近的公共祖先问题#include<stdio.h>int findFather(int n, int m);int main(void){ int binTree[1011] = { ‘\0’ }; int N; int result = 0; scanf("%d", &N); int i = 0; for (i = 0; i < N; i++) { scanf("%d", &binTree[i]); } int num_1 = 0, num_2 = 0; scanf("%d %d", &num_1, &num_2); result = findFather(num_1, num_2); if (num_1 == num_2) printf("%d %d\n", num_1, binTree[num_1 - 1]); else if (binTree[num_1 - 1] == 0) printf(“ERROR: T[%d] is NULL\n”, num_1); else if (binTree[num_2 - 1] == 0) printf(“ERROR: T[%d] is NULL\n”, num_2); else printf("%d %d\n", result, binTree[result - 1]); return 0;}int findFather(int n, int m){ while (1) { if (n > m) n /= 2; else m /= 2; if (n == m) break; } return n;} 列出叶结点#include<bits/stdc++.h>using namespace std;struct tree{ int left,right;}a[15];int main(){ int n,check[15] = {0},head,p[10],j = 0; queue q; cin>>n; for (int i = 0; i < n; i++) { char l,r; cin>>l>>r; a[i].left = l != ‘-’ ? l-‘0’ : 12;//12表示空 a[i].right = r != ‘-’ ? r-‘0’ : 12; check[a[i].left] = 1; check[a[i].right] = 1; } for (head = 0; head < n;head++) if (!check[head]) break; q.push(head); while(!q.empty()) { int i = q.front(); q.pop(); if(i == 12) continue; if(a[i].left == 12 && a[i].right == 12) p[j++] = i; q.push(a[i].left); q.push(a[i].right); } cout<<p[0]; for (int i = 1; i < j; i++) cout<<’ ‘<<p[i]; return 0;}修理牧场#include<bits/stdc++.h>using namespace std;int n,x,a,b,sum;priority_queue<int, vector, greater >q;int main(){ cin>>n; while(n–) { cin>>x; q.push(x); } while(q.size()>1) { a=q.top(),q.pop(); b=q.top(),q.pop(); sum+=a+b; q.push(a+b); } cout<<sum<<’\n’; system(“pause”);}树的遍历#include<stdio.h>#include<malloc.h>int N = 0;int Prer[33] = { ‘\0’ };int Post[33] = { ‘\0’ };typedef struct node { int num; struct node* left; struct node* right;}node;struct node *create(int *pos, int *pre, int n);void sequence(node *T);int main(void){ node *T = NULL; scanf("%d", &N); int i = 0; for (i = 0; i < N; i++) scanf("%d", &Post[i]); for (i = 0; i < N; i++) scanf("%d", &Prer[i]); T = create(Post, Prer, N); sequence(T); return 0;}void sequence(node *T){ struct node *p[33], *q; int f = 0, r = 0; int num = 0; if (T) { p[r++] = T; while (f != r) { q = p[f++]; printf("%d", q->num); num++; if (num < N) printf(" “); if (q->left) p[r++] = q->left; if (q->right) p[r++] = q->right; } }}struct node *create(int *pos, int *pre, int n) { int i = 0, k = 0, root = 0; if (n == 0) return NULL; struct node *T; T = (struct node *)malloc(sizeof(struct node)); if (T == NULL) return -1; root = pos[n - 1]; T->num = root; for (i = 0; i<n; i++) { if (pre[i] == root) { k = i; break; } } T->left = create(pos, pre, k); T->right = create(pos + k, pre + k + 1, n - k - 1); return T;}求二叉树的叶子结点个数#include <stdio.h>#include <stdlib.h>#include <string.h>char a[1000];int ant = 0,c = 0;typedef struct Node{ char Data; struct Node *Left,*Right;}Node;Node *CreatNode(){ Node *p = (Node *)malloc(sizeof(Node)); p -> Left = p -> Right = NULL; return p;}Node *CreatTree(){ if(a[ant] == ‘#’) { ant ++; return NULL; } Node *head = CreatNode(); head -> Data = a[ant ++]; head -> Left = CreatTree(); head -> Right = CreatTree(); if(head -> Left == NULL && head -> Right == NULL)c ++; return head;}void InOrder(Node *T){ if(T == NULL)return; InOrder(T -> Left); putchar(T -> Data); InOrder(T -> Right);}int main(){ scanf(”%s",a); Node *head = CreatTree(); InOrder(head); putchar(’\n’); putchar(‘0’+c);}推断学生所属学校的人数#include<bits/stdc++.h>using namespace std;int n,m,f[1010];string s1,s2;unordered_map<string, int>mp;unordered_map<int, int>cut;int find(int x){ if(xf[x]) return x; return f[x]=find(f[x]);}void merge(int a,int b){ a=find(a); b=find(b); if(a!=b) f[a]=b;}int main(){ for(int i=0;i<1010;i++) f[i]=i; cin>>n; for(int i=1;i<=n;i++) { cin>>s1; mp[s1]=i; } cin>>m; while(m–) { cin>>s1>>s2; merge(mp[s1],mp[s2]); } int ans=0; for(int i=1;i<=n;i++) { if(f[i]i) ans++; cut[find(f[i])]++; } int mx=0; for(auto &it : cut) { mx=max(mx,it.second); } cout<<ans<<’ '<<mx;}部落#include<bits/stdc++.h>using namespace std;int n,m,k,a;int f[10010];unordered_map<int,int> mp;int find(int x){ if(xf[x]) return x; return f[x]=find(f[x]);}void merge(int a,int b){ a=find(a); b=find(b); if(a!=b) f[a]=b;}int main(){ for(int i=0;i<10010;i++) f[i]=i; cin>>n; while(n–) { cin>>m>>a; mp[a]=1; for(int i=1;i<m;i++) { cin>>k; mp[k]=1; merge(a,k); } } cout<<mp.size()<<’ ‘; int ans=0; for(int i=1;i<=mp.size();i++) { if(f[i]i) ans++; } cout<<ans<<’\n’; cin>>n; int l,r; while(n–) { cin>>l>>r; if(find(l)find®) cout<<“Y\n”; else cout<<“N\n”; }}树种统计#include<bits/stdc++.h>using namespace std;int main(){ int n; cin>>n; getchar(); map<string, int> s; for(int i=0;i<n;i++) { string a; getline( cin, a); s[a]++; } map<string, int>::iterator it; for(it=s.begin();it!=s.end();it++) { cout<first<<" “; double num=100.0*it->second/n; printf(”%.4lf%%\n",num); } return 0;}二叉树的遍历#include<bits/stdc++.h>using namespace std;struct tree{ int left; int right;}a[10005];int printf(int head){ cout<<head+1<<’ ‘; if(a[head].left) printf(a[head].left-1); if(a[head].right) printf(a[head].right-1);}int main(){ int n; cin>>n; for(int i = 0; i< n; i++) cin>>a[i].left>>a[i].right; printf(0); return 0;}列出所有祖先结点#include <stdio.h>#include <stdlib.h>typedef struct TNode BinTree;struct TNode{ int Left; int Right; int parent;};void Create();void Find();BinTree T[10];int N;int main(){ Create(); if(N > 1) Find();}void Create(){ int i; char L,R; scanf("%d",&N); getchar(); for(i = 0; i < N; i++){ T[i].parent = -1; T[i].Left = -1; T[i].Right = -1; } for(i = 0; i < N; i++){ scanf("%c %c",&L,&R); getchar(); if(L == ‘-’){ T[i].Left = -1; } else{ T[i].Left = L - ‘0’; T[T[i].Left].parent = i; } if(R == ‘-’){ T[i].Right = -1; } else{ T[i].Right = R - ‘0’; T[T[i].Right].parent = i; } }}void Find(){ int a[N],n = 0,i,R; scanf("%d",&R); while(T[R].parent != -1){ a[n] = T[R].parent; n++; R = T[R].parent; } for(i = n-1; i >=0 ; i–){ if(i == n-1){ printf("%d",a[i]); } else{ printf(" %d",a[i]); } }}根据后序和中序遍历输出先序遍历 #include<bits/stdc++.h>using namespace std;typedef struct Binode{ int data; struct Binode *lchild,*rchild;}Binode ,*BiTree;BiTree CreatTree(int *h,int *z,int n){ if(n<=0)return NULL; else { int i; BiTree T = new Binode; T->data = h[n-1]; for(i = 0;i<n-1;i++) { if(h[n-1] == z[i]) break; } T->lchild = CreatTree(h,z,i); T->rchild = CreatTree(h+i,z+i+1,n-i-1); return T; }}void coutpreorder(BiTree T){ if(T){ cout<<" “<data; coutpreorder(T->lchild); coutpreorder(T->rchild); }}int main(){ int n,hou[50],zh[50]; std::ios::sync_with_stdio(false); cin>>n; for(int i = 0;i<n;i++) cin>>hou[i]; for(int i = 0;i<n;i++) cin>>zh[i]; BiTree T; T = CreatTree(hou,zh,n); cout<<“Preorder:”; coutpreorder(T); return 0;} 邻接矩阵表示法创建无向图#include<bits/stdc++.h>using namespace std;int main(){ map<char, int> m; int i, j; char G[15], a, b; scanf(”%d %d",&i,&j); getchar(); scanf("%s",G); getchar(); while(j–) { scanf("%c%c",&a,&b); getchar(); m[a]++; m[b]++; } cout<<m[G[0]]; for(int k = 1; k < i; k++) cout<<’ '<<m[G[k]]; return 0;}城市间紧急救援#include#include#include#include#include#include#include#include#include#includeusing namespace std;typedef long long ll;#define N 550#define INF1 0x3f3f3f3f#define INF2 0xc0c0c0c0int n,m,s,d;int dis[N],res[N],pathcnt[N],path[N],vis[N],edge[N][N],num[N];void dijkstra(){ for(int i=0;i<n;i++) dis[i] = edge[s][i]; int e = s;path[s] = -1;dis[e] = 0;pathcnt[e]=1; while(e!=d){ vis[e] = 1; for(int j=0;j<n;j++){ if(!vis[j]){ if(dis[j]>dis[e]+edge[e][j]){ dis[j]=dis[e]+edge[e][j]; res[j] = num[j] + res[e]; pathcnt[j] = pathcnt[e]; path[j] = e; } else if(dis[j]dis[e]+edge[e][j]){ pathcnt[j] += pathcnt[e]; if(res[j]<num[j] + res[e]){ res[j] = num[j] + res[e]; path[j] = e; } } } } int mindis = INF1;e = n; for(int j=0;j<n;j++){ if(!vis[j]&&dis[j]<mindis){ mindis = dis[j]; e = j; } } if(en) break; } }int main() { cin >> n >> m >> s >> d; for(int i=0;i<n;i++) { scanf("%d",num+i); res[i] = num[i]; } int x,y,z,h; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ edge[i][j]=edge[i][j]=INF1; } edge[i][i] = 0; } for(int i=0;i<m;i++){ scanf("%d%d%d",&x,&y,&z); edge[x][y]=edge[y][x]= z; } dijkstra(); vector p; h = d; while(h!=-1){ p.push_back(h); h = path[h]; } cout << pathcnt[d] << " " << res[d] << endl; for(int i=p.size()-1;i>=0;i–){ if(ip.size()-1) cout << p[i]; else cout << " " << p[i]; } return 0;}公路村村通#include<bits/stdc++.h>using namespace std;int n,m,f[1010];struct node{ int w,u,v; bool operator<(const node& f) const { return w<f.w; }}p[3010];int find(int x){ if(xf[x]) return x; return f[x]=find(f[x]);}int main(){ for(int i=0;i<1010;i++) f[i]=i; cin>>n>>m; for(int i=0;i<m;i++) { cin>>p[i].v>>p[i].u>>p[i].w; } sort(p,p+m); int sum=0,a,b; for(int i=0;i<m;i++) { a=find(p[i].v); b=find(p[i].u); if(a!=b) { f[a]=b; sum+=p[i].w; } } int ans=0; for(int i=1;i<=n;i++) { if(f[i]i) ans++; } if(ans>1) sum=-1; cout<<sum;}电话聊天狂人#include<bits/stdc++.h>using namespace std;int n;string call;map<string, int> mp;int main(){ cin>>n; n*=2; while(n–) { cin>>call; mp[call]++; } int mx=0,ans=0,f=0; string res; for(auto &it : mp) { mx=max(mx,it.second); } for(auto &it : mp) { if(it.secondmx) { ans++; if(f0) res=it.first; f=1; } } cout<<res<<" “<<mx; if(ans!=1) cout<<” "<<ans;}航空公司VIP客户查询#include<bits/stdc++.h>using namespace std;int n,m,k;string name;unordered_map<string, int>mp;int main(){ cin.tie(0),cout.tie(0); ios::sync_with_stdio(false); cin>>n>>m; while(n–) { cin>>name>>k; if(k<=m) mp[name]+=m; else mp[name]+=k; } cin>>n; while(n–) { cin>>name; if(mp[name]>0) cout<<mp[name]<<’\n’; else cout<<“No Info\n”; }}QQ帐户的申请与登陆#include<bits/stdc++.h>using namespace std;int n;char NL;unordered_map<string,int> zt;unordered_map<string,string> password;string name,poss;int main(){ cin>>n; while(n–) { cin>>NL>>name>>poss; if(NL’N’) { if(zt[name]==1) cout<<“ERROR: Exist\n”; else{ cout<<“New: OK\n”; zt[name]=1; password[name]=poss; } } else{ if(zt[name]==0) cout<<“ERROR: Not Exist\n”; else { if(password[name]==poss) cout<<“Login: OK\n”; else cout<<“ERROR: Wrong PW\n”; } } }}A-B #include<stdio.h>int main(){ char str1[10005],str2[10005]; int count[128]={0},i; gets(str1); gets(str2); for(i=0;str2[i]!=’\0’;i++) count[str2[i]]++; for(i=0;str1[i]!=’\0’;i++) if(count[str1[i]]==0) printf("%c",str1[i]); return 0;}旅游规划#include <bits/stdc++.h>using namespace std;#define INF 0x3f3f3f3fint x[501][501];int cost[501][501];int pride[501];int dis[501];int exs[501];int main(){ int n,m,s,d; cin>>n>>m>>s>>d; for (int i=0; i<=n; i++)//初始化 { for (int j=1; j<=n; j++) x[i][j]=x[j][i]=cost[i][j]=cost[j][i]=INF; } for(int i=0; i<m; i++) { int q,w,e,r; cin>>q>>w>>e>>r; x[q][w]=x[w][q]=e;//双向图 cost[q][w]=cost[w][q]=r; } exs[s]=1; for(int i=0; i<n; i++) { dis[i]=x[s][i]; pride[i]=cost[s][i]; } dis[s]=0; int cnt=0; int f=0; while(cnt<n) { int min=INF; for(int i=1; i<n; i++) { if(exs[i]==0&&dis[i]<min) { min=dis[i];//最短距离 f=i; } } exs[f]=1; cnt++; for(int i=1; i<n; i++) { if(exs[i]==0&&(dis[i]>x[f][i]+min||(dis[i]==x[f][i]+min&&pride[i]>pride[f]+cost[f][i]))) { dis[i]=min+x[f][i];//当有更小的权值时才会改变。 pride[i]=pride[f]+cost[f][i]; } } } cout<<dis[d]<<" "<<pride[d]; return 0;}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值