跳到正文

个人记录

2026.3.14习题总结

huanzhenyang0420约 4 分钟
原文
文章信息与版本

文章信息

收录
2026年3月15日
更新
2026年3月20日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1

P1146 硬币翻转

题目描述

在桌面上有一排硬币,共 NN 枚,每一枚硬币均为正面朝上。现在要把所有的硬币翻转成反面朝上,规则是每次可翻转任意 N−1N-1 枚硬币(正面向上的被翻转为反面向上,反之亦然)。求一个最短的操作序列(将每次翻转 N−1N-1 枚硬币成为一次操作)。

数据范围:NN 为不大于 100 的偶数

思路

经过多组数据可以得出,最少的次数是 n 次。接着我们可以发现第 i-1 行的第 j 个和第 i 行的第 j 个是相同的。 这是因为题目说:

对于有多种操作方案的情况,则只需操作的字典序最小输出一种。

正确代码:

C++
#include<bits/stdc++.h>
#include<math.h>
#include<cmath>
using namespace std;
typedef long long ll;
bool vis[105];
int main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	cout<<n<<'\n';
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(j!=i) vis[j]=(!vis[j]);
			cout<<vis[j];
		}
		cout<<'\n';
	}
	return 0;
}
/*
4

4
0111
1100
0001
1111

*/
//大:localStorage.cp6_player_id='1f88476d-6223-4bd3-b3c9-199ca12066e0'
//小:localStorage.cp6_player_id='60bb16e0-3c7c-4351-af7f-7549a250d4c6'

P1611 循环的数字

题目描述

两个不同的正整数 (n,m)(n, m) 是循环的,当且仅当你能通过将 nn 末端的几个数字移到它的首端而不改变移动的数字的顺序并使整个数字变成 mm 。举个例子,(12345,34512)(12345, 34512) 就是一对循环的数字,因为你能把 1234512345 中末尾的 345345 移到 1212 前面,从而得到 3451234512。注意,为了成为一对循环的数字,nn 和 mm 位数必须相同。无论 nn 或 mm 都没有前置的 00。

现在给定正整数 AA 和 BB,并保证 AA 和 BB 位数相同且均没有前置 00,求存在多少循环的正整数对 (n,m)(n, m),使得 A≤n≤m≤BA \leq n \le m \leq B ?

数据范围:1≤A,B≤2×1061\le A,B \leq 2\times 10^6。

思路:

这一题没有什么太复杂的思路

重要部分:

C++
for(int i=a;i<b;i++){
    n=i;
    m=(n%10)*num[cnt-1]+n/10;
    while(n!=m){
        ans+=n<m&&m<=b;
        m=(m%10)*num[cnt-1]+m/10;
    }
}

正确代码:

C++
#include<bits/stdc++.h>
#include<math.h>
#include<cmath>
using namespace std;
typedef long long ll;
int num[10]={1,10,100,1000,10000,100000,1000000};
int main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int a,b,n,m,cnt=0,pre,ans=0;
    cin>>a>>b;
    pre=a;
    while(pre){
		pre/=10;
		cnt++;
	}
    for(int i=a;i<b;i++){
        n=i;
        m=(n%10)*num[cnt-1]+n/10;
        while(n!=m){
            ans+=n<m&&m<=b;
            m=(m%10)*num[cnt-1]+m/10;
        }
    }
	cout<<ans;
	return 0;
}
/*

*/
//大:localStorage.cp6_player_id='1f88476d-6223-4bd3-b3c9-199ca12066e0'
//小:localStorage.cp6_player_id='60bb16e0-3c7c-4351-af7f-7549a250d4c6'
原文