数列分段
思路
遍历统计有多少数和前一个数不同
实现
#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int n;
int a[N];
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int ans = 1;
for(int i=1;i<n;i++)
{
if(a[i]!=a[i-1])
ans++;
}
cout<<ans;
}
日期计算
思路
简单模拟
实现
#include<bits/stdc++.h>
using namespace std;
int y,d;
int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool is_run(int y)
{
if((y%4==0 && y%100)||y%400==0) return 1;
return 0;
}
int get_day(int y,int m)
{
if(m==2) return months[m]+is_run(y);
return months[m];
}
int main()
{
cin>>y>>d;
int day = 0;
for(int i=1;i<=12;i++)
{
if(day>=d) break;
if(day+get_day(y,i)<d)
{
;
}
else if(day+get_day(y,i)>d)
{
cout<<i<<"\n"<<d-day<<endl;
}
else if(day+get_day(y,i)==d)
{
cout<<i<<"\n"<<get_day(y,i)<<endl;
}
day+=get_day(y,i);
}
}
模板生成系统
思路
字符串模拟
实现
#include<bits/stdc++.h>
using namespace std;
int m,n;
const int N = 110;
vector<string> strs;
map<string,string> a;
int main()
{
cin>>m>>n;
getchar();
string str;
for(int i=0;i<m;i++)
{
getline(cin, str);
strs.push_back(str);
}
for(int i=0;i<n;i++)
{
string s1,s2;
getline(cin, str);
int j=0;
while(str[j]!=' ') s1+=str[j],j++;
j+=2;
while(str[j]!='\"') s2+=str[j],j++;
//cout<<"s1: "<<s1<<" s2: "<<s2<<endl;
a[s1] = s2;
}
// for(auto aa:a)
// {
// // cout<<aa.first<<" "<<aa.second<<endl;
// }
for(auto s:strs)
{
for(int i=0;i<s.size();i++)
{
if(s[i]=='{' && s[i+1]=='{' &&s[i+2]==' ')
{
string t;
i+=3;
while(!(s[i]==' ' && s[i+1]=='}' &&s[i+2]=='}'))
{
t+=s[i];
i++;
}
//cout<<"\n\nt::"<<t<<endl;
cout<<a[t];
i+=2;
}
else
cout<<s[i];
}
cout<<endl;
}
}
高速公路
思路
强连通分量
相关:Pecco的知乎文章
实现
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010, M = 100010;
int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N];
int stk[N], top, ts;
bool in_stk[N];
int ans;
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
void tarjan(int u)
{
dfn[u] = low[u] = ++ ts;
stk[ ++ top] = u, in_stk[u] = true;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (!dfn[j])
{
tarjan(j);
low[u] = min(low[u], low[j]);
}
else if (in_stk[j]) low[u] = min(low[u], dfn[j]);
}
if (dfn[u] == low[u])
{
int y, cnt = 0;
do
{
y = stk[top -- ];
in_stk[y] = false;
cnt ++ ;
} while (y != u);
ans += cnt * (cnt - 1) / 2;
}
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while (m -- )
{
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
}
for (int i = 1; i <= n; i ++ )
if (!dfn[i])
tarjan(i);
printf("%d\n", ans);
return 0;
}
最佳文章
不会