C. Infinity Table

博主分享了解决代码竞赛中关于数列排列问题的解题策略,通过找到给定数值所在正方形的最大边长,确定其在网格中的精确行列位置。关键步骤包括计算最大正方形边长和剩余部分的分布。

https://codeforces.com/contest/1560/problem/C
在这里插入图片描述
INPUT

7
11
14
5
4
1
2
1000000000

OUTPUT

2 4
4 3
1 3
2 1
1 1
1 2
31623 14130

题意
看图应该能理解图内数的排列规律(就每次沿一个右上角正方形扩张)然后给出一个数,求出该数在图的第几行第几列

做法
先找出该数内的最大正方形,然后就从列开始数,如果列超出了最大正方形的边长,就稍微更改一下行列输出即可

下面拿个举例 : 8
在这里插入图片描述
所以我设cnt从1开始遍历,然后直到cnt * cnt(边长的平方代表面积内含的数)大于8,即cnt = 2.
所以8内最大正方形为 2×2.然后将8 - (cnt * cnt)即剩下的数,然后从x轴为cnt + 1开始遍历下去y值,
当剩下的数大于cnt + 1(在列中装不完)所以y轴固定为cnt + 1,反向从x轴cnt+1开始数x轴上的位置。

AC代码
ps:13行中 cnt * 2 - n 是化简后的 ,原式为cnt - (n - cnt ) ε=ε=ε=┏(゜ロ゜;)┛

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int T,n;cin>>T;
	while(T--)
	{
		cin>>n;
		int cnt = 1;
		while(cnt * cnt < n) {	cnt++;	}	
		n -= (cnt - 1) * (cnt - 1);
		if(n <= cnt)	cout<<n<<" "<<cnt<<endl;
		else	cout<<cnt<<" "<<cnt * 2 - n<<endl;	
	}
	return 0;
}

如果有问题尽情提出哈

几句牢骚
暑假培训后咸鱼了两周,现在开始补cf,发现连1200分的题切得都有点吃力 orz
假期的st表,树状数组,线段树,各种数据结构感觉都还给学长了,得好好补题了
暑期选拔进集训队还是很nice的

ε=ε=ε=┏(゜ロ゜;)┛

<template> <div class="container"> <!-- 卡片区域 --> <div class="card"> <div class="card-header">学习计划表</div> <div class="card-body"> <form @submit.prevent="add"> <div class="row g-4"> <!-- 学习科目 --> <div class="col-auto"> <div class="input-group mb-3"> <span class="input-group-text">学习科目</span> <input type="text" class="form-control" placeholder="请输入学习科目" v-model.trim="subject" /> </div> </div> <!-- 学习内容 --> <div class="col-auto"> <div class="input-group mb-3"> <span class="input-group-text">学习内容</span> <textarea class="form-control" v-model.trim="content" placeholder="请输入学习内容" :style="{ height: '38px' }" ></textarea> </div> </div> <!-- 学习地点 --> <div class="col-auto"> <div class="input-group mb-3"> <span class="input-group-text">学习地点</span> <select class="form-select form-select-sm" v-model="selectedOption"> <option v-for="option in options" :value="option.place" :key="option.placeCode" > {{ option.place }} </option> </select> </div> </div> <!-- 添加按钮 --> <div class="col-auto"> <button type="submit" class="btn btn-primary">添加</button> </div> </div> </form> </div> </div> <!-- 表格区域 --> <table class="table table-striped table-hover table-bordered"> <thead> <tr> <th scope="col">序号</th> <th scope="col">学习科目</th> <th scope="col">学习内容</th> <th scope="col">学习地点</th> <th scope="col">完成状态</th> <th scope="col">操作</th> </tr> </thead> <tbody> <tr v-for="item in list" :key="item.id"> <td>{{ item.id }}</td> <td>{{ item.subject }}</td> <td>{{ item.content }}</td> <td>{{ item.place }}</td> <td> <div class="form-check form-switch"> <input class="form-check-input" type="checkbox" role="switch" :id="'cb' + item.id" v-model="item.status" /> <label class="form-check-label" :for="'cb' + item.id" v-if="item.status" > 已完成 </label> <label class="form-check-label" :for="'cb' + item.id" v-else > 未完成 </label> </div> </td> <td> <a href="javascript:;" @click="remove(item.id, item.status)">删除</a> </td> </tr> </tbody> </table> </div> </template> <script setup> import { ref, reactive } from 'vue' // 表格数据 const list = ref([ { id: '1', subject: 'Vue.js 前端实战开发', content: '学习指令,例如 v-if、v-for、v-model 等', place: '自习室', status: false, }, ]) // 表单数据 let subject = ref('') let content = ref('') let nextId = ref('') let selectedOption = ref('自习室') let options = reactive([ { placeCode: 0, place: '自习室' }, { placeCode: 1, place: '图书馆' }, { placeCode: 2, place: '宿舍' }, ]) // 添加学习计划 let add = () => { if (subject.value === '') { alert('学习科目为必填项!') return } // 计算下一个ID if (list.value.length > 0) { nextId.value = Math.max(...list.value.map(item => parseInt(item.id))) + 1 } else { nextId.value = 1 } const obj = { id: nextId.value.toString(), subject: subject.value, content: content.value, place: selectedOption.value, status: false, } list.value.push(obj) // 重置表单 subject.value = '' content.value = '' selectedOption.value = '自习室' } // 删除学习计划 let remove = (id, status) => { if (status) { list.value = list.value.filter(item => item.id !== id) } else { alert('请完成该学习计划后再进行删除操作!') } } </script> <style scoped> .container { max-width: 1200px; margin: 0 auto; padding: 20px; } .card { width: 100%; border: 1px solid #999; margin-bottom: 20px; border-radius: 8px; overflow: hidden; } .card-header { width: 100%; height: 50px; background-color: aliceblue; line-height: 50px; font-size: 24px; font-weight: 700; text-indent: 30px; border-bottom: 1px solid #999; } .card-body { width: 100%; padding: 20px; } .row { display: flex; flex-wrap: wrap; align-items: center; gap: 15px; } .col-auto { flex: 1; min-width: 200px; } .input-group { display: flex; align-items: center; } .input-group-text { background-color: #f8f9fa; border: 1px solid #ced4da; border-right: none; padding: 6px 12px; font-weight: 500; } .form-control, .form-select { flex: 1; border: 1px solid #ced4da; padding: 6px 12px; border-radius: 0 4px 4px 0; } .btn-primary { background-color: #007bff; border-color: #007bff; padding: 6px 20px; } .btn-primary:hover { background-color: #0056b3; border-color: #0056b3; } /* 表格样式 */ .table { width: 100%; border-collapse: collapse; margin-top: 20px; } .table th, .table td { padding: 12px; text-align: center; vertical-align: middle; border: 1px solid #dee2e6; } .table th { background-color: #f8f9fa; font-weight: 600; } .table-striped tbody tr:nth-of-type(odd) { background-color: rgba(0, 0, 0, 0.02); } .table-hover tbody tr:hover { background-color: rgba(0, 0, 0, 0.075); } /* 开关样式 */ .form-check.form-switch { display: flex; align-items: center; justify-content: center; gap: 8px; } .form-check-input { width: 3em; height: 1.5em; } .form-check-label { margin: 0; cursor: pointer; } /* 删除链接样式 */ a { color: #dc3545; text-decoration: none; font-weight: 500; } a:hover { color: #c82333; text-decoration: underline; } /* 响应式设计 */ @media (max-width: 768px) { .container { padding: 10px; } .row { flex-direction: column; } .col-auto { min-width: 100%; } .table { font-size: 14px; } .table th, .table td { padding: 8px 4px; } } </style> 有问题没
最新发布
11-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值