第四次周赛参考代码
第一题:
1 #include<iostream> 2 using namespace std; 3 string a,b; 4 int len1,len2,len; 5 string str; 6 int main() 7 { 8 cin>>a; 9 cin>>b; 10 len1 = a.length(); 11 len2 = b.length(); 12 if(len1>len2)//补齐两个代码的长度,不足的用'0'补齐 13 { 14 for(int i=1;i<=len1-len2;i++) 15 { 16 b = "0" + b; 17 } 18 }else{ 19 for(int i=1;i<=len2-len1;i++) 20 { 21 a = "0" + a; 22 } 23 } 24 int temp;//记录每个位数相加后数目的大小 25 int cf = 0;//记录每次进位 26 len = a.length();// 记录最终的长度 27 for(int i=len-1;i>=0;i--)//需要从后往前遍历 28 { 29 temp = a[i]-'0' +b[i]-'0'+cf; 30 cf = temp/10; 31 temp %=10; 32 str = char(temp+'0') +str; 33 } 34 if(cf !=0) str = char(cf+'0') +str;//最后如果还有进位,则需要再次相加 35 cout<<str<<endl; 36 }
第二题:
1 #include<iostream> 2 using namespace std; 3 string a,b,str; 4 int main() 5 { 6 cin>>a; 7 cin>>b; 8 int sign = 0; 9 if(a==b) 10 { 11 cout<<"0"<<endl; 12 return 0; 13 } 14 if((a < b && a.size()==b.size()) || (a.size()<b.size())) 15 { 16 sign = 1; 17 swap(a,b); 18 } 19 int len1 = a.length(); 20 int len2 = b.length(); 21 int tep = len1 - len2; 22 int cf = 0; 23 for(int i=len2-1;i>=0;i--) 24 { 25 if(a[i+tep]<b[i]+cf) 26 { 27 str = char(a[i+tep]-b[i]-cf+'0'+10) + str; 28 cf = 1; 29 }else{ 30 str = char(a[i+tep]-b[i]-cf+'0')+str; 31 cf = 0; 32 } 33 } 34 for(int i =tep-1;i>=0;i--) 35 { 36 if(a[i]-cf>='0') 37 { 38 str = char(a[i]-cf)+str; 39 cf = 0; 40 }else{ 41 str = char(a[i]-cf+10)+str; 42 cf = 1; 43 } 44 } 45 str.erase(0,str.find_first_not_of('0')); 46 if(sign == 1) cout<<"-"; 47 cout<<str<<endl; 48 }
第三题:
看题解可能理解的更详细
https://www.luogu.com.cn/problem/solution/P1714
第四题:思路:从左到右求最大上升序列,从右向左求最大下降序列,然后逐个枚举中间值,使出队人数最少
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 int g[105],f[105],a[105],s[105]; 5 int main() 6 { 7 int n; 8 cin>>n; 9 for(int i=1;i<=n;i++) 10 { 11 cin>>a[i]; 12 f[i]=1; 13 g[i]=1; 14 }//输入并赋初值 15 for(int i=n-1;i>=1;i--) 16 { 17 for(int j=i+1;j<=n;j++) 18 { 19 if(a[i]>a[j]&&f[i]<=f[j]+1) 20 { 21 f[i]=f[j]+1; 22 } 23 } 24 }//从右往左,按左高右低顺序找出每一个位置右边有几个从高到低的数 (包括自己) 25 for(int i=2;i<=n;i++) 26 { 27 for(int j=1;j<i;j++) 28 { 29 if(a[i]>a[j]&&g[i]<=g[j]+1) 30 { 31 g[i]=g[j]+1; 32 } 33 } 34 }//从左往右,按左低右高顺序找出每一个位置左边有几个从低到高的数 (包括自己) 35 int maxx=0; 36 for(int i=1;i<=n;i++) 37 { 38 s[i]=f[i]+g[i]-1;//把每个数的左边从低到高的数和右边从高到低的数相加 39 //注意!!自己加了两次要-1 40 if(s[i]>maxx) 41 { 42 maxx=s[i]; 43 } 44 } 45 cout<<n-maxx;//是求出列的人数 46 }
第五题:数据结构问题
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=100000; 4 char a[maxn],b[maxn],c[maxn]; 5 void dfs(int l1,int r1,int l2,int r2) 6 { 7 if(l1>r1) 8 { 9 return; 10 } 11 printf("%c",b[r2]); 12 int p=l1; 13 while(a[p]!=b[r2])p++; 14 int ent = p-l1; 15 dfs(l1,p-1,l2,l2+ent-1); 16 dfs(p+1,r1,l2+ent,r2-1); 17 } 18 int main() 19 { 20 int n; 21 scanf("%s",a); 22 scanf("%s",b); 23 n=a.length(); 24 dfs(0,n-1,0,n-1); 25 }
第六题:
#include<bits/stdc++.h>
using namespace std;
const int N=5100;
int q[N][N];
int mark[N];
int dis[N];
int n,m;
int res;
int prim()
{
memset(dis,0x3f,sizeof(dis));
for(int i=0;i<n;i++)
{
int t=0,flag=0x3f3f3f3f;
for(int j=1;j<=n;j++)
{
if(dis[j]<=flag&&mark[j]==0)
{
t=j;
flag=dis[j];
}
}
mark[t]=1;
if(i&&flag==0x3f3f3f3f)
return -1;
if(i) res+=dis[t];
for(int j=1;j<=n;j++)
{
if(mark[j]==0&&dis[j]>q[j][t])
dis[j]=q[j][t];
}
}
return res;
}
int main()
{
memset(q,0x3f,sizeof(q));
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
q[a][b]=min(q[a][b],c);
q[b][a]=min(q[b][a],c);
}
int t=prim();
if(t==-1)
cout<<"orz"<<endl;
else
cout<<t<<endl;
return 0;
}
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cmath> #include<iomanip> #include<stack> #include<queue> #include<set> #include<map> using namespace std; struct all { int x, y, z; }; int n, m; all e[410000]; int f[210000]; bool cmp(all u, all v) { return u.z < v.z; } int getf(int s) { if (f[s] == s) return s; getf(f[s]); } void he(int u, int v) { int h = getf(u), g = getf(v); if (h < g) f[g] = h; else f[h] = g; } int main() { cin >> n >> m; for (int i = 1; i <= m * 2; i+=2) { int a, b, c; cin >> a >> b >> c; e[i].x = a; e[i].y = b; e[i].z = c; e[i + 1].x = a; e[i + 1].y = b; e[i + 1].z = c; } for (int i = 1; i <= n; i++) f[i] = i; sort(e + 1, e + 1 + 2 * m, cmp); int sum = 0, num = 0; for (int i = 1; i <= 2 * m; i++) { int a, b; a = getf(e[i].x), b = getf(e[i].y); if (a != b) { he(a, b); sum += e[i].z; if (++num == n - 1) { cout << sum << endl; break; } } } if (num < n - 1) cout << "orz" << endl; return 0; }
第七题:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cmath> #include<iomanip> #include<stack> #include<queue> #include<set> #include<map> using namespace std; string s; stack<char> q; int main() { cin >> s; bool f = 1; for (int i = 0; i < s.size(); i++) { if (s[i] == '(' || s[i] == '{' || s[i] == '[') q.push(s[i]); if (s[i] == ')') { if (q.size() == 0) { f = 0; break; } if (q.top() == '(') q.pop(); else { f = 0; break; } } if (s[i] == '}') { if (q.size() == 0) { f = 0; break; } if (q.top() == '{') q.pop(); else { f = 0; break; } } if (s[i] == ']') { if (q.size() == 0) { f = 0; break; } if (q.top() == '[') q.pop(); else { f = 0; break; } } } if (f == 0) cout << "false" << endl; if (f == 1) { if (q.size() == 0) cout << "true" << endl; else cout << "false" << endl; } return 0; }