Codeforces C. Almost Equal (数学规律)
2024-09-06 01:30:35
题目链接:http://codeforces.com/contest/1206/problem/C
题解 : 观察可以发现当n为偶数时,1 - 2n是不满足题意的,可以举例n = 2,n = 4试一试。那么当n为奇数时,则满足题意,通过n = 3和n = 5的情况可以发现一个规律,当a[ 1 ] = 1时候,a[ n + 1] = 2 ,a[ 2 ] = 4,此时 a[ n + 1] = 3,a[ 3 ] = 5,则a[ n+ 3] = 6,发现a[ i ] 和a[ i + n] 是连续的两个数,而且其奇偶性是交错的,所以猜一下结论:当 i 为奇数时,a[ i ] = 2 * i -1, a[ i + n] = 2 * i;i为偶数时, a[ i ] = 2 * i , a[ i + n ] = 2 * i - 1 , 模拟一下这个过程即可。
AC代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#define maxn 100005
#define inf 0x3f3f3f3f
using namespace std;
int main(){
int n;
cin>>n;
if(n%2 == 0){
cout<<"NO";
return 0;
}
cout<<"YES"<<endl;
long long int a[n+2];
int cnt = 1;
for(int i = 1;i<=n;i++){
if(i%2 == 0){
cout<<2*i<<" ";
a[cnt] = 2*i-1;
cnt++;
}
else{
cout<<2*i-1<<" ";
a[cnt] = 2*i;
cnt++;
}
}
for(int i = 1;i<=n;i++){
if(i == n){
cout<<a[i];
}
else{
cout<<a[i]<<" ";
}
}
return 0;
}
最新文章
- OS.js – 开源的 Web OS 系统,赶快来体验
- mysql 创建索引和删除索引
- 一周试用yii开发一个带各种该有功能的web程序(二)
- [07]APUE:进程环境
- 《BI那点儿事》数据流转换——字符映射表
- SharePoint 错误集 3
- 存储过程优点&;缺点
- MVC 中DropDownList 用法
- Jquery判断滚动条是否到达窗口顶部和底部
- android开发之service详解
- 转载:C#中事件的由来
- HTTP协议初级入门
- 主备(keepalived+haproxy)
- T-SQL实用查询之查询字段所属的数据库表
- pytest学习 一
- [随笔][Golang][golang nil 相关]
- JS stringObject.Match()
- Python安装scrapy过程中出现“Failed building wheel for xxx”
- tensorflow如何正确加载预训练词向量
- Loadrunner使用键盘快捷键