发布时间:2024-01-13 08:00
代码:
#include
using namespace std;
const int MAXN = 1010;
int arr[MAXN];
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i ++)
cin >> arr[i];
for(int i = 0; i < n; i ++){
//头尾特殊处理
if(!i){
cout << (arr[0] + arr[1]) / 2 << \" \";
}else if(i == n - 1){
cout << (arr[n - 2] + arr[n - 1]) / 2 << \" \";
}else{
cout << (arr[i - 1] + arr[i] + arr[i + 1]) / 3 << \" \";
}
}
return 0;
}
代码:
#include
using namespace std;
const int MAXN = 2010;
typedef pair<int, int> PII;
PII a[MAXN], b[MAXN];
int fun(int i, int j){
if(a[i].second <= b[j].first || b[j].second <= a[i].first)
return 0;
return min(a[i].second, b[j].second) - max(a[i].first, b[j].first);
}
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i ++)
cin >> a[i].first >> a[i].second;
for(int i = 0; i < n; i ++)
cin >> b[i].first >> b[i].second;
int ans = 0;
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
ans += fun(i, j);
cout << ans;
return 0;
}
#include
#include
#include
#include
#include
#include
using namespace std;
int n, m;
vector<string> strs;
struct Node
{
int tab;
string tag, id;
int k; // 当前节点最多可以匹配到第几个位置
Node(string str)
{
int i = 0;
while (str[i] == \'.\') i ++ ;
tab = i;
while (i < str.size() && str[i] != \' \')
tag += tolower(str[i ++ ]);
i ++ ; // 过滤掉空格
while (i < str.size()) id += str[i ++ ];
k = 0;
}
bool check(string& word)
{
if (word[0] == \'#\') return word == id;
return word == tag;
}
};
void work(vector<string>& ws)
{
vector<int> res;
stack<Node> stk;
for (int i = 0; i < strs.size(); i ++ )
{
string str = strs[i];
Node t(str);
while (stk.size() && stk.top().tab >= t.tab) stk.pop();
if (stk.size()) t.k = stk.top().k;
if (t.k == ws.size()) t.k -- ;
if (t.check(ws[t.k]))
{
t.k ++ ;
if (t.k == ws.size()) res.push_back(i + 1);
}
stk.push(t);
}
cout << res.size();
for (auto x: res) cout << \' \' << x;
cout << endl;
}
int main()
{
cin >> n >> m;
getchar();
string str;
while (n -- )
{
getline(cin, str);
strs.push_back(str);
}
while (m -- )
{
getline(cin, str);
stringstream ssin(str);
vector<string> ws;
while (ssin >> str)
{
if (str[0] != \'#\')
for (auto& c: str)
c = tolower(c);
ws.push_back(str);
}
work(ws);
}
return 0;
}
代码:
// 80分暴力解法
#include
using namespace std;
const int MAXN = 310;
int a[MAXN], b[MAXN], n;
//深搜
bool dfs(int k){
//判断最后一个数字对不对
if(k == n + 1){
if((a[n] + a[n - 1]) / 2 == b[n]){
for(int i = 1; i <= n; i ++)
cout << a[i] << \" \";
return true;
}
return false;
}
int j = 3 * b[k - 1] - a[k - 1] - a[k - 2] + 2;
int i = max(1, 3 * b[k - 1] - a[k - 1] - a[k - 2]);
for(; i <= j; i ++){
a[k] = i;
if(dfs(k + 1))
return true;
}
return false;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> b[i];
if(n == 2){
cout << 1 << \" \" << 2 * b[1] - 1 << endl;
return 0;
}
//先确定好前两个数字
for(int i = 1; i <= 101; i ++){
a[1] = i;
for(int j = 1; j <= 101; j ++){
a[2] = j;
if((i + j) / 2 > b[1])
break;
if((i + j) / 2 == b[1])
if(dfs(3))
return 0;
}
}
return 0;
}
更多历年题解戳这里:ccf-csp 历年真题题解