D. Carousel

该博客讨论了一个关于旋转木马上动物座椅涂色的问题,旨在使用最少的颜色种类且保证相邻动物颜色不同。当木马座椅数量为偶数时,只需要两种颜色交替涂色;当数量为奇数时,根据相邻座椅是否存在同种动物进行分类讨论,可能需要三种颜色。如果只有同一种动物,所有座椅涂同一种颜色即可。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

题目大意:一个旋转木马上有n只编号不同的动物座椅围成一圈(一种编号一种动物 ) ,现在给他涂色,要求颜色种类用最少且前后不同种动物的颜色不能相同

解题思路: 分类讨论:
一:n是偶数,则只需两种颜色按1,2重复涂下去即可
二:n是奇数:
1)若存在相邻的同种动物座椅,则两个座椅可以看作一个来涂色,按n是偶数来涂。
2)不存在相邻的同种动物座椅,则按1,2顺序涂下去,最后一个涂第三种即可
特殊情况:只有一种动物,则全部涂1即可。

#include<iostream>
#include<cstring>
#define maxn 1000010
int n,t[maxn],ans[maxn],s[maxn],count=0;
using namespace std;
int main()
{
	int q;
	cin>>q;
	while(q--)
	{
		count=0;
		cin>>n;
		memset(s,0,n);
		for(int i=0;i<n;i++)
		{
			cin>>t[i];
			if(!s[t[i]])
			{
				s[t[i]]=1;
				count++;//count用来记录动物种数
			}
		}
		if(count==1)
		{
			cout<<count<<endl;
			for(int i=0;i<n;i++)
			{
				cout<<1<<" ";
			}
			cout<<endl;continue;
		}//特殊情况
		if(n%2)//n为奇数
		{
			int flag=0;
			for(int i=0;i<n;i++)
			{
				if(t[i]==t[(i+1)%n])//存在相邻的同种动物座椅
				{
					flag=1;
					cout<<2<<endl;
					for(int pos=i+1; pos<n;++pos)
					{
						if((pos-1)%2) ans[pos]=1;//pos-1即把i和i+1看作一个座位
						else ans[pos]=2;
					}
					for (int pos=i;pos>=0;--pos)
					{
						if(pos%2) ans[pos]=1;
						else ans[pos]=2;
					}
					for(int j=0;j<n;++j) cout<<ans[j]<<" ";
					cout<<endl;break;
				}
			}
			if(flag) continue;
			else//二 2)
			{
				cout<<3<<endl;
				for(int i=0;i<n-1;i++)
				{
					if(i%2) cout<<"2 ";
					else cout<<"1 ";
				}
				cout<<"3 ";	
				cout<<endl;
			}
		}
		else//n为偶数
		{	
			cout<<2<<endl;
			for(int i=0;i<n;i++) 
			{
				if(i%2) cout<<"2 ";
				else cout<<"1 ";
			}
			cout<<endl;
			continue;
		}
	}
	return 0;
 } 
$(document).ready(function(){ let currentIndex = 0; const $items = $('.carousel-inner img'); const totalItems = $items.length; let autoPlay = setInterval(nextSlide, 3000); // 初始化指示点 function initDots() { for(let i=0; i<totalItems; i++){ // $('.carousel-dots').append('<div class="dot"></div> '); // $('.carousel-dots').append($('<div>').text(i+1).addClass('dot').append($('<span>').addClass('heng'))); // var $span = $('<span>').text(123); if (i + 1 < totalItems) { $('.carousel-dots').append($('<div>').text((i+1).toString().padStart(2, '0')).addClass('dot').append($('<span>').addClass('heng'))); }else{ $('.carousel-dots').append($('<div>').text((i+1).toString().padStart(2, '0')).addClass('dot')); } } $('.dot').eq(0).addClass('active'); } initDots(); // 切换幻灯片 function showSlide(index) { $items.removeClass('active').eq(index).addClass('active'); $('.dot').removeClass('active').eq(index).addClass('active'); } // 下一张 function nextSlide() { currentIndex = (currentIndex + 1) % totalItems; showSlide(currentIndex); } // 上一张 function prevSlide() { currentIndex = (currentIndex - 1 + totalItems) % totalItems; showSlide(currentIndex); } // 事件绑定 $('.next').click(function(){ clearInterval(autoPlay); nextSlide(); autoPlay = setInterval(nextSlide, 3000); }); $('.prev').click(prevSlide); $('.carousel-dots').on('click', '.dot', function(){ const index = $(this).index(); currentIndex = index; showSlide(currentIndex); }); // 悬停暂停 $('.carousel').hover( () => clearInterval(autoPlay), () => autoPlay = setInterval(nextSlide, 3000) ); // //平移动画效果 // $(document).ready(function() { // // 方法1:使用animate()方法 // $(".jianji").animate({ // left: "720px" // 移动到距离左侧250px的位置 // }, 1000 ,); // 动画持续时间2000ms(2秒) // // 方法2:使用CSS transform(更流畅的硬件加速方案) // // $("#movingObject").css({ // // transform: "translateX(600px)", // // transition: "2s" // // }); // }); // function qiehuan() { // // 方法1:使用animate()方法 // $(".jianji").animate({ // left: "720px" // 移动到距离左侧250px的位置 // }, 1000 ,); // 动画持续时间2000ms(2秒) // }; // qiehuan() // $(document).ready(function() { // var animationTriggered = false; // $(window).scroll(function() { // var scrollPosition = $(window).scrollTop(); // var triggerPosition = $(".jianji").offset().top - $(window).height() +50; // if (scrollPosition >= triggerPosition && !animationTriggered) { // animationTriggered = true; // $(".jianji") // .animate({ left: "300px" }, 800) // 向右滑动 // .animate({ height: "200px" }, 600); // 再调整高度 // } // }); // }); var animationTriggered = false; $(window).scroll(function() { var scrollPosition = $(window).scrollTop(); var triggerPosition = $(".jianji").offset().top - $(window).height() +5200; if (scrollPosition >= triggerPosition && !animationTriggered) { animationTriggered = true; $(".jianji") .animate({ left: "750px" }, 800) // 向右滑动 // .animate({ height: "200px" }, 600); // 再调整高度 } }); });让这个轮播图的图片切换效果是左右滑动
04-01
$(document).ready(function(){ let currentIndex = 0; const i t e m s items=(‘.carousel-inner img’); const totalItems = $items.length; let autoPlay = setInterval(nextSlide, 3000); // 初始化指示点 function initDots() { for(let i=0; i<totalItems; i++){ // ( ′ . c a r o u s e l − d o t s ′ ) . a p p e n d ( ′ < d i v c l a s s = " d o t " > < / d i v > ′ ) ; / / ( ′ .carousel−dots ′ ).append( ′ <divclass="dot"></div> ′ );//(‘.carousel-dots’).append( ( ′ < d i v > ′ ) . t e x t ( i + 1 ) . a d d C l a s s ( ′ d o t ′ ) . a p p e n d ( ( ′ <div> ′ ).text(i+1).addClass( ′ dot ′ ).append((‘<span>’).addClass(‘heng’))); // var s p a n = span=(‘<span>’).text(123); if (i + 1 < totalItems) { ( ′ . c a r o u s e l − d o t s ′ ) . a p p e n d ( ( ′ .carousel−dots ′ ).append((‘<div>’).text((i+1).toString().padStart(2, ‘0’)).addClass(‘dot’).append(ParseError: KaTeX parse error: Expected 'EOF', got '}' at position 40: …g'))); }̲else{ …(‘.carousel-dots’).append($(‘<div>’).text((i+1).toString().padStart(2, ‘0’)).addClass(‘dot’)); } } $('.dot').eq(0).addClass('active'); } initDots(); // 切换幻灯片 function showSlide(index) { i t e m s . r e m o v e C l a s s ( ′ a c t i v e ′ ) . e q ( i n d e x ) . a d d C l a s s ( ′ a c t i v e ′ ) ; items.removeClass( ′ active ′ ).eq(index).addClass( ′ active ′ );(‘.dot’).removeClass(‘active’).eq(index).addClass(‘active’); } // 下一张 function nextSlide() { currentIndex = (currentIndex + 1) % totalItems; showSlide(currentIndex); } // 上一张 function prevSlide() { currentIndex = (currentIndex - 1 + totalItems) % totalItems; showSlide(currentIndex); } // 事件绑定 $(‘.next’).click(function(){ clearInterval(autoPlay); nextSlide(); autoPlay = setInterval(nextSlide, 3000); }); $(‘.prev’).click(prevSlide); ParseError: KaTeX parse error: Expected '}', got 'EOF' at end of input: … const index = (this).index(); currentIndex = index; showSlide(currentIndex); }); // 悬停暂停 ( ′ . c a r o u s e l ′ ) . h o v e r ( ( ) = > c l e a r I n t e r v a l ( a u t o P l a y ) , ( ) = > a u t o P l a y = s e t I n t e r v a l ( n e x t S l i d e , 3000 ) ) ; / / / / 平移动画效果 / / ( ′ .carousel ′ ).hover(()=>clearInterval(autoPlay),()=>autoPlay=setInterval(nextSlide,3000));////平移动画效果//(document).ready(function() { // // 方法1:使用animate()方法 // $(“.jianji”).animate({ // left: “720px” // 移动到距离左侧250px的位置 // }, 1000 ,); // 动画持续时间2000ms(2秒) // // 方法2:使用CSS transform(更流畅的硬件加速方案) // // ParseError: KaTeX parse error: Expected 'EOF', got '#' at position 3: ("#̲movingObject").…(“.jianji”).animate({ // left: “720px” // 移动到距离左侧250px的位置 // }, 1000 ,); // 动画持续时间2000ms(2秒) // }; // qiehuan() // $(document).ready(function() { // var animationTriggered = false; // ParseError: KaTeX parse error: Expected '}', got 'EOF' at end of input: …rollPosition = (window).scrollTop(); // var triggerPosition = ( " . j i a n j i " ) . o f f s e t ( ) . t o p − (".jianji").offset().top−(window).height() +50; // if (scrollPosition >= triggerPosition && !animationTriggered) { // animationTriggered = true; // $(“.jianji”) // .animate({ left: “300px” }, 800) // 向右滑动 // .animate({ height: “200px” }, 600); // 再调整高度 // } // }); // }); var animationTriggered = false; $(window).scroll(function() { var scrollPosition = $(window).scrollTop(); var triggerPosition = $(".jianji").offset().top - $(window).height() +5200; if (scrollPosition >= triggerPosition && !animationTriggered) { animationTriggered = true; $(".jianji") .animate({ left: "750px" }, 800) // 向右滑动 // .animate({ height: "200px" }, 600); // 再调整高度 } });我希望可以使用jquery通过点击类名为dot的 来实现左右滑动
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值