Codeforces Round 65 C. Round Table Knights(71)

https://codeforces.com/contest/71/problem/C

这道题,比较难,现在还没有完全吃透,参考别人的代码,大概意思

1)将圆桌n个骑士,分成 i (>3) 部分,每一份 step 个骑士。

2)然后每一部分执行step步,寻找是否=0,如果最后一个是0,那么结果就是0

注意:代码还没有完全理解,我参考别人的代码,理解到这里,还是不完全理解,暂时存放这里,方便手机查看和再思考。

C. Round Table Knights

代码

分析代码

#include <iostream>

using namespace std;

int main()

{

int n;

bool mood_good = true;

int step = 0;

int *list;

cin >> n;

list=new int[n];

for (int i = 0; i < n; i++)

{

cin >> list[i];

}

for (int i = 3; i <= n; i++)

{

//You must make N elements split i equally into parts. 将元素分i份。

if (n % i == 0)

{

//cout << "n%i= "<<n<<" % "<<i<<" = "<< n%i << endl;

step = n / i;//每一份step个

//cout <<"i="<< i << ",step= " << step << endl;

//every part: to check

for (int start = 0; start < step; start++)

{ //然后在每份的元素里操作

mood_good = true;

//cout << "start=" << start << endl;

// i is i parts

//这里是每份的元素。

for (int k = 0; k < i; k++)

{

int pos = start + k * step;

//cout << "list[pos("<<pos<<")]=" << list[pos] << endl;

if (pos >= n)

{

pos -= n;

//cout << "restart begin pos=" << pos << endl;

}

if (1!=list[pos])

{

//cout << "bad" << endl;

mood_good = false;

}

}

if (true == mood_good)

{

cout <<"YES"<<endl;

i = n + 10000; //to exit

break;

}

}

}

}

if (mood_good == false)

{

cout << "NO" << endl;

}

delete[] list;

return 0;

}

cpp 复制代码
/*
ref code:DhvanitVaghani

#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];

for (int d = 3; d <= n; d++) {
if (n % d == 0) {
int step = n / d;
for (int start = 0; start < step; start++) {
bool ok = true;
for (int i = 0; i < d; i++) {
int pos = start + i * step;
if (pos >= n) pos -= n;
if (a[pos] != 1) ok = false;
}
if (ok) {
cout << "YES\n";
return 0;
}
}
}
}
cout << "NO\n";
return 0;
}
*/
#include <iostream>
using namespace std;

int main() 
{
	int n;
	bool mood_good = true;
	int step = 0;
	int *list;
	
	
	cin >> n;

	list=new int[n];
	
	for (int i = 0; i < n; i++)
	{
		cin >> list[i];
	}

	for (int i = 3; i <= n; i++) 
	{
		//You must make N elements split i equally into parts.
		if (n % i == 0) 
		{ 
			//cout << "n%i= "<<n<<" % "<<i<<" = "<< n%i << endl;
			step = n / i;
			
			//cout <<"i="<< i << ",step= " << step << endl;

			//every part: to check
			for (int start = 0; start < step; start++) 
			{
				mood_good = true;
				//cout << "start=" << start << endl;
				// i is i parts
				for (int k = 0; k < i; k++) 
				{
					int pos = start + k * step;
					//cout << "list[pos("<<pos<<")]=" << list[pos] << endl;

					if (pos >= n)
					{
						pos -= n;
						//cout << "restart begin pos=" << pos << endl;
					}
					
					if (1!=list[pos])
					{
						//cout << "bad" << endl;
						mood_good = false;
					}
				}

				if (true == mood_good)
				{
					cout <<"YES"<<endl;
					i = n + 10000; //to exit
					break;
				}
			}
		}
	}
	if (mood_good == false)
	{
		cout << "NO" << endl;
	}
	delete[] list;
	return 0;
}

/*
Examples
InputCopy
3
1 1 1
OutputCopy
YES
InputCopy
6
1 0 1 1 1 0
OutputCopy
YES
InputCopy
6
1 0 0 1 0 1
OutputCopy
NO


*/
相关推荐
Larry_Yanan8 小时前
Qt多进程(三)QLocalSocket
开发语言·c++·qt·ui
醒过来摸鱼8 小时前
Java classloader
java·开发语言·python
superman超哥8 小时前
仓颉语言中元组的使用:深度剖析与工程实践
c语言·开发语言·c++·python·仓颉
小鸡吃米…8 小时前
Python - 继承
开发语言·python
JIngJaneIL9 小时前
基于java+ vue农产投入线上管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
祁思妙想9 小时前
Python中的FastAPI框架的设计特点和性能优势
开发语言·python·fastapi
LYFlied9 小时前
【每日算法】LeetCode 153. 寻找旋转排序数组中的最小值
数据结构·算法·leetcode·面试·职场和发展
唐装鼠9 小时前
rust自动调用Deref(deepseek)
开发语言·算法·rust
Lucas555555559 小时前
现代C++四十不惑:AI时代系统软件的基石与新征程
开发语言·c++·人工智能
源代码•宸9 小时前
goframe框架签到系统项目(BITFIELD 命令详解、Redis Key 设计、goframe 框架教程、安装MySQL)
开发语言·数据库·经验分享·redis·后端·mysql·golang