小陈的开学第十一周程序

这篇博客汇总了作者在Wlacm、Atcoder和Vjudge三个平台上的编程竞赛题目,包括曲径通幽、Rectangle Filling、Group the Integers、Arithmetic Progression等算法问题的解题思路和代码实现,涉及字符串操作、正方形填充、整数分组和等差数列等概念。同时,博主分享了在解决这些问题时的心路历程和经验教训。

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

一、Wlacm

1.曲径通幽

题意

在N×N的方格中填入正整数1~N^2,可以有很多种方案。 如果按照迂回的路线(如下图)填数,你能给出比较有效的解法吗?
在这里插入图片描述

输入

正整数N(≤30)

输出

1~N^2构成的N×N数值方阵,输出时每个数值占4个位置。

样例输入

1
2
3
4

样例输出

   1

   1   4
   2   3

   1   4   5
   2   3   6
   9   8   7

   1   4   5  16
   2   3   6  15
   9   8   7  14
  10  11  12  13

解题思路

其实我本来是不会写这道题的,后来别的题写不出来就开始写这道题。刚开始我真的毫无思绪,后来就开始找规律,发现了它走位的小规律,就开始模拟,居然过了,但是应该写的不是很规范。

程序代码:(my_AC)

#include<bits/stdc++.h>
using namespace std;
int res[1000][1000];
int main(){
	int n;
	while(~scanf("%d",&n)){
		int cnt=4,x=1,y=3;
		res[1][1]=1;
		res[2][1]=2;
		res[2][2]=3;
		res[1][2]=4;
		int num=1;
		if(n<=2){
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					printf("%4d",res[i][j]);
				}
				printf("\n");
			}
			continue;
		}
		for(int i=1;i<=(n+1)/2;i++){
	//		cout<<1<<endl;
			res[x][y]=++cnt;x++;
			int temp=x+num;
			while(x<=temp)
				res[x++][y]=++cnt;y--;x--;
			temp=y-num;
			num++;
			while(y>=temp)
				res[x][y--]=++cnt;y++;x++;
			temp=y+num+1;
			while(y<=temp)
				res[x][y++]=++cnt;y--;x--;
			temp=x-num;
			while(x>=temp)
				res[x--][y]=++cnt;y++;x++;
			num++;
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				printf("%4d",res[i][j]);
			}
			printf("\n");
		}
	}
	return 0;
} 

程序代码

#include"bits/stdc++.h"
using namespace std;
int map1[1000][1000];
int main(){
	int n;
	cin>>n;
	map1[1][1]=1;
	map1[2][1]=2;
	int num=3,x=2,y=1;
	for(int i=1;i<n;i++){
		int temp=i;
		if(i%2==1){
			temp=i;
			while(temp--){map1[x][++y]=num++;}
			temp=i;
			while(temp--){map1[--x][y]=num++;}
			map1[x][++y]=num++;
		}else {
			temp=i;
			while(temp--){map1[++x][y]=num++;}
			temp=i;
			while(temp--){map1[x][--y]=num++;}
			map1[++x][y]=num++;
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			printf("%4d",map1[i][j]);
		}
		printf("\n");
	}
	return 0;
}

2.Rectangle Filling

题意

给出一个大小为 n × m 的矩形,现用大小为 a × a 的正方形填充该矩形,且要求正方形的边必须和矩形的边平行。求至多能填入多少正方形。

注意:正方形可以正好碰到矩形边界,但不能超出矩形外,且正方形不可重叠。

输入

本题有多组测试数据。第一行有一个整数 T (1 ≤ T ≤ 100),表示数据组数,对于每组数据:

第一行有三个整数 n, m, a (1 ≤ n, m, a ≤ 109),意义如上所述。

输出

每组数据输出一行一个整数,表示至多能填入多少正方形。

样例输入

3
7 7 3
8 4 2
4 7 5

样例输出

4
8
0

程序代码

#include<bits/stdc++.h>
using namespace std;
long long n,m,a;
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%lld %lld %lld",&n,&m,&a);
		if(a>n||a>m){
			cout<<0<<endl;
			continue;
		}	
		long long t1=n/a;
		long long t2=m/a;
		cout<<t1*t2<<endl;
	}
	return 0;
} 

3.Group the Integers

题意

将前 n 个正整数分成 k 组,使得每组中的正整数两两互质。求 k 的最小值。

注意:两个正整数互质,指的是两个正整数的最大公因数等于 1。

输入

本题有多组测试数据。第一行有一个整数 T (1 ≤ T ≤ 105),表示数据组数,对于每组数据:

第一行只有一个整数 n (1 ≤ n ≤ 109),意义如上所述。

提示:本题输入数据较大,请使用较为快速的输入输出方式。例如,使用 C++ 的同学可以使用 scanf/printf 代替 cin/cout。

输出

每组数据输出一行一个整数,表示 k 的最小值。

样例输入

2
3
4

样例输出

1
2

解题思路

刚开始我根本不知道怎么算,就一直没写出来,比赛完了,告诉我答案居然是n/2…无语了,找规律大赛!!

程序代码

#include<bits/stdc++.h>
using namespace std;
long long n;
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%lld",&n);
		if(n==1){
			printf("1\n");
		}else{
			printf("%lld\n",n/2);
		}
	}
	return 0;
} 

4.Arithmetic Progression

题意

给出 n 个整数 a1, a2, …, an,现在请你再加入一个整数 x,使得这 n + 1 个整数可以构成一个等差数列。求所有符合要求的 x。

输入

本题有多组测试数据。第一行有一个整数 T (1 ≤ T ≤ 10),表示数据组数,对于每组数据:

第一行只有一个整数 n (1 ≤ n ≤ 50),意义如上所述。

第二行有 n 个整数 a1, a2, …, an (-108 ≤ ai ≤ 108),表示给出的整数。

输入数据保证每组数据至少能找到一个满足要求的整数 x。

输出

每组数据输出两行。第一行输出一个整数,表示符合要求的整数 x 的数量。第二行按升序输出所有符合要求的整数 x,两个数之间用一个空格分隔。

如果该组数据有无数个整数 x 满足要求,则在第一行和第二行都输出 “INF”(不含引号)。

请不要在行末输出多余空格!

样例输入

2
4
1 3 5 7
1
0

样例输出

2
-1 9
INF
INF

解题思路

这道题真是说起来一把辛酸泪,比赛的时候我想错了,就一直没写出来,回来补题,我已经知道为什么了,但是我的第一种方法就一直错,把我气坏了,这道题我改了三个晚上,每天在气愤中入睡,气死我了。后来用了第二种方法才过,但是我还是不知道为什么我第一种方法错了!
正确的方法就是:找到中间差值最大的地方。就是要插入的地方,如果没有就在两边,但是要考虑差值等于1时只有一个,其他有两个。n=2时要注意有3种情况,一定都要考虑到,刚开始只考虑了两个就一直WA。注意,n=1时是无穷多的。

程序代码:(AC)

#include<bits/stdc++.h>
using namespace std;
long long a[100];
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		if(n==1){
			printf("INF\nINF\n");
			continue;
		}
		sort(a+1,a+n+1);
		long long temp=a[2]-a[1],temp1=a[2]-a[1];
		int d=1,flag1=0;
		for(int i=2;i<n;i++){
			long long t=a[i+1]-a[i];
			if(t!=temp){
				flag1=1;
			}
			if(temp<t){
				temp1=temp;//temp1常规等差 
				temp=t;//temp是需要插入的等差 
				d=i;
			}
			if(temp>t) temp1=t;
		}
		if(n==2){
			if(a[1]==a[2]){
				printf("1\n%lld\n",a[1]);
			}else if(temp%2==0){
				printf("3\n");
				printf("%lld %lld %lld\n",a[1]-temp,a[1]+temp/2,a[2]+temp);
			}else if(temp%2==1){
				printf("2\n");
				printf("%lld %lld\n",a[1]-temp,a[2]+temp);
			}
		}else if(flag1==1){
			printf("1\n");
			printf("%lld\n",a[d]+temp1);
		}else if(flag1==0){
			if(temp==0){
				printf("1\n%lld\n",a[1]);
			}else{
				printf("2\n%lld %lld\n",a[1]-temp,a[n]+temp);
			}
		}
	}
	return 0;
}

程序代码:(WA)

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int a[55];
int ans[5];
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		if(n==1){
			printf("INF\nINF\n");
			continue;
		}
		int cnt=2;
		sort(a+1,a+n+1);
		int temp=INF,temp1=INF;
		int flag=0,flag1=0,flag2=0;
		for(int i=1;i<n;i++){
			if(i==1){
				temp=a[i+1]-a[i];
			}else{
				int t=a[i+1]-a[i];
				if(temp!=t) flag2=1;
				if(flag1==1){
					if(t==temp1){
						temp=temp1;
						cnt++;
						ans[cnt]=(a[2]+a[1])/2;
						break;
					}else{
						cnt++;
						ans[cnt]=(a[i]+a[i-1])/2;
						break;
					}
				}
				if(t!=temp && flag==1){
					cnt++;
					ans[cnt]=(a[i+1]+a[i])/2;
					break;
				}else if(t!=temp && flag==0){
					temp1=t;flag1=1;
				}else{
					flag=1;
				}
			}
		}
		if(n==3 && (flag1)){
			printf("1\n");
			if(a[1]+temp1<a[2]){
				printf("%d\n",a[1]+temp1);
			}else{
				printf("%d\n",a[2]+temp);
			}
		}else if(flag2==1){
			printf("1\n%d\n",ans[3]);
		}else if(n==2&&temp==2){
			printf("3\n");
			ans[1]=a[1]-temp;ans[2]=a[2]+temp;
			printf("%d %d %d\n",ans[1],a[2]-1,ans[2]);
		}else if(n==2){
			if(temp%2==0){
				printf("3\n");
				printf("%d %d %d\n",a[1]-temp,(a[1]+a[2])/2,a[2]+temp);
			}else{
				printf("2\n");
				printf("%d %d\n",a[1]-temp,a[2]+temp);
			}
			
		}else{
			ans[1]=a[1]-temp;ans[2]=a[n]+temp;
			sort(ans+1,ans+cnt+1);
			if(ans[2]==ans[1])cnt--;
			printf("%d\n",cnt);
			for(int i=1;i<=cnt;i++){
				if(i==1)
					printf("%d",ans[i]);
				else{
					if(ans[i]==ans[1]){
						continue;
					}else{
						printf(" %d",ans[i]);
					}
				}
			}
			printf("\n");
		}
	}
	return 0;
} 

二、Atcoder

1.ROT N

题意

We have a string S consisting of uppercase English letters. Additionally, an integer N will be given.

Shift each character of S by N in alphabetical order (see below), and print the resulting string.

We assume that A follows Z. For example, shifting A by 2 results in C (A → B → C), and shifting Y by 3 results in B (Y → Z → A → B).

输入

Input is given from Standard Input in the following format:
N
S

输出

Print the string resulting from shifting each character of S by N in alphabetical order.

样例输入1

2
ABCXYZ

样例输出1

CDEZAB

Note that A follows Z.

样例输入2

0
ABCXYZ

样例输出2

ABCXYZ

样例输入3

13
ABCDEFGHIJKLMNOPQRSTUVWXYZ

样例输出3

NOPQRSTUVWXYZABCDEFGHIJKLM

解题思路

就是给你一个数字和字符串,要求将字符串的每一个字符向右移n位,然后输出。
用char数组存即可

程序代码

#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
char ans[N];
int main(){
	int n;
	string s;
	cin>>n;
	cin>>s;
	int len=s.length();
	for(int i=0;i<len;i++){
		int temp=s[i]+n;
		if(temp>90){
			temp=temp-26;
		}
		ans[i]=temp;
	}
//	for(int i=0;i<len;i++)
		cout<<ans<<endl;
//	cout<<endl;
	return 0;
}

2.Buy an Integer

题意

Takahashi has come to an integer shop to buy an integer.

The shop sells the integers from 1 through 10^9. The integer N is sold for A×N+B×d(N) yen (the currency of Japan), where d(N) is the number of digits in the decimal notation of N.

Find the largest integer that Takahashi can buy when he has X yen. If no integer can be bought, print 0.

注意:

All values in input are integers.
1≤A≤10^9
1≤B≤10^9
1≤X≤10^18

输入

Input is given from Standard Input in the following format:

A B X

输出

Print the greatest integer that Takahashi can buy. If no integer can be bought, print 0.

样例输入1

10 7 100

样例输出1

9

样例输入2

2 1 100000000000

样例输出2

1000000000

样例输入3

1000000000 1000000000 100

样例输出3

0

样例输入4

1234 56789 314159265

样例输出4

254309

解题思路

给你a、b、x,a让你求A×N+B×d(N)=x,所满足的最大的N,d(N)代表N的位数。
因为N太大了。所以我写的代码超时了,而且最后两个点还WA了,我去看了别人写的代码,他们是用二分的方法写的,其实我也想到了二分,但是我没写出来,我气!!

程序代码:(AC)

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
int dig(LL x)	//算位数
{
	int ans = 0;
	while(x != 0){
		x /= 10;
		ans++;
	}
	return ans;
}
int main()
{
	LL a,b,x;
	cin >> a >> b >> x ;
	int l = 1,r = 1e9;
	if(x >= 1e9)
		r = 1e9;
	else
		r = x;
	int mid;
	while(l < r){
		mid = (l + r) / 2;
		LL c = mid * a + dig(mid) * b;
		if(c <= x)
			l = mid + 1;
		else
			r = mid;
	}
	if(l * a + dig(l) * b > x)
		l--;
	cout << l <<endl;
	return 0;
}

三、Vjudge

1.ID (AtCoder - 4267 )

题意

In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.

City i is established in year Yi and belongs to Prefecture Pi.

You can assume that there are no multiple cities that are established in the same year.

It is decided to allocate a 12-digit ID number to each city.

If City i is the x-th established city among the cities that belong to Prefecture i, the first six digits of the ID number of City i is Pi, and the last six digits of the ID number is x.

Here, if Pi or x (or both) has less than six digits, zeros are added to the left until it has six digits.

Find the ID numbers for all the cities.

Note that there can be a prefecture with no cities.

注意

1≤N≤10^5
1≤M≤10^5
1≤Pi≤N
1≤Yi≤10^9
Yi are all different.
All values in input are integers.

输入

Input is given from Standard Input in the following format:

N M
P1 Y1
:
PM YM

输出

Print the ID numbers for all the cities, in ascending order of indices (City 1, City 2, …).

样例输入1

2 3
1 32
2 63
1 12

样例输出1

2 3
1 32
2 63
1 12

As City 1 is the second established city among the cities that belong to Prefecture 1, its ID number is 000001000002.
As City 2 is the first established city among the cities that belong to Prefecture 2, its ID number is 000002000001.
As City 3 is the first established city among the cities that belong to Prefecture 1, its ID number is 000001000001.

样例输入2

2 3
2 55
2 77
2 99

样例输出2

000002000001
000002000002
000002000003

解题思路

题意就是第一个数字代表他所属的类别,第二个代表他的年份,你要判断他在他所属类别中的排名,然后输出他所属的类别和排名,每个占6位。
看着挺简单的,但是写的时候还是有点难,最后还是问了zmh才知道怎么写。
要先记录他们的序号,然后先按年份排序,然后得出他们的排名,最后再按序号排序,就又回到原来的顺序了,就可以输出了。

程序代码

#include<iostream>
#include<algorithm>
#include<cmath>
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct node{
	int x,y;
	int index,pre;
}a[N];
bool cmp(node a,node b){
	if(a.x==b.x)
		return a.y<b.y;
	return a.x<b.x;
}
bool cmp1(node a,node b){
	return a.index<b.index;
}
int main(){
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=1;i<=m;i++){
		scanf("%d %d",&a[i].x,&a[i].y);
		a[i].index=i;
	}
	sort(a+1,a+m+1,cmp);
	int cnt=1;
	int t=a[1].x;
	for(int i=1;i<=m;i++){
		if(a[i].x != t){
			t = a[i].x;
			cnt = 1;
			a[i].pre = cnt;
			cnt++;
		}
		else{
			a[i].pre = cnt;
			cnt++;
		}
	}
	sort(a+1,a+m+1,cmp1);
	for(int i=1;i<=m;i++){
		printf("%06d%06d\n",a[i].x,a[i].pre);
	}
	return 0;
} 

2.Cave Painting (CodeForces - 922C )

题意

Imp is watching a documentary about cave painting.

Some numbers, carved in chaotic order, immediately attracted his attention. Imp rapidly proposed a guess that they are the remainders of division of a number n by all integers i from 1 to k. Unfortunately, there are too many integers to analyze for Imp.

Imp wants you to check whether all these remainders are distinct. Formally, he wants to check, if all 在这里插入图片描述, 1 ≤ i ≤ k, are distinct, i. e. there is no such pair (i, j) that:

1 ≤ i < j ≤ k,
在这里插入图片描述, where 在这里插入图片描述 is the remainder of division x by y.

输入

The only line contains two integers n, k (1 ≤ n, k ≤ 10^18).

输出

Print “Yes”, if all the remainders are distinct, and “No” otherwise.

You can print each letter in arbitrary case (lower or upper).

样例输入1

4 4

样例输出1

No

样例输入2

5 3

样例输出2

Yes

解题思路

题意就是n取余1-k,有没有相同的余数,有就输出No,没有就输出Yes。
这个数也太大了,后来发现就是n%i!=(i-1),暴力就行了。

程序代码

#include<iostream>
using namespace std;
long long n,t;
int main(){
	scanf("%lld %lld",&n,&t);
	for(int i=1;i<=t;i++){
		if(n%i!=(i-1)){
			printf("No\n");
			return 0;
		}
	}
	printf("Yes\n");
	return 0;
}

3.Tricky Alchemy (CodeForces - 912A )

题意

During the winter holidays, the demand for Christmas balls is exceptionally high. Since it’s already 2018, the advances in alchemy allow easy and efficient ball creation by utilizing magic crystals.

Grisha needs to obtain some yellow, green and blue balls. It’s known that to produce a yellow ball one needs two yellow crystals, green — one yellow and one blue, and for a blue ball, three blue crystals are enough.

Right now there are A yellow and B blue crystals in Grisha’s disposal. Find out how many additional crystals he should acquire in order to produce the required number of balls.

输入

The first line features two integers A and B (0 ≤ A, B ≤ 109), denoting the number of yellow and blue crystals respectively at Grisha’s disposal.

The next line contains three integers x, y and z (0 ≤ x, y, z ≤ 109) — the respective amounts of yellow, green and blue balls to be obtained.

输出

Print a single integer — the minimum number of crystals that Grisha should acquire in addition.

样例输入1

4 3
2 1 1

样例输出1

2

样例输入2

3 9
1 1 3

样例输出2

1

样例输入3

12345678 87654321
43043751 1000000000 53798715

样例输出3

2147483648

解题思路

就是黄球需要两个黄色的晶体,绿球需要一个黄色和一个蓝色,蓝球需要三个蓝色晶体。已知有a种黄色,b种蓝色,需要黄、绿、蓝球分别为x、y、z,问还需要多少晶体才可以。
模拟就行啦

程序代码

#include<iostream>
using namespace std;
long long a,b,x,y,z;
long long ans=0,temp;
int main(){
	scanf("%lld %lld %lld %lld %lld",&a,&b,&x,&y,&z);
	temp=x*2;
	if(a<temp){
		ans+=(temp-a);
		a=0;
	}else{
		a=a-temp;
	}
	if(a<y){
		ans+=(y-a);
		a=0;
	}else{
		a=a-y;
	}
	if(b<y){
		ans+=(y-b);
		b=0;
	}else{
		b=b-y;
	}
	temp=z*3;
	if(b<temp){
		ans+=(temp-b);
		b=0;
	}else{
		b=b-temp;
	}
	printf("%lld\n",ans);
	return 0;
}

关于“Designer 小陈”的具体作品或资料,在当前提供的引用内容中并未提及任何与其直接相关的信息。然而,可以从这些引用的内容推测一些可能的方向来探索其潜在的作品领域。 如果假设“小陈”是一名专注于电子设计自动化(EDA)工具使用的工程师或者开发者,则他可能会涉及如下几个方面的工作: ### 1. **Altium Designer 的应用** 根据相关内容描述[^1],可以推断出“小陈”或许擅长利用 Altium Designer 创建并配置 PCB 文档。这是一款功能强大的 EDA 软件,用于电路板的设计与开发。以下是该方向的一些典型工作成果: - 设计高质量的印刷电路板 (PCB),包括多层板布局、信号完整性分析以及电源管理优化。 - 利用 Altium Designer 提供的功能模块完成从原理图绘制到最终生产文件导出的一系列操作。 - 参考安装指南[^4],能够熟练部署 Altium Designer 并为其设置合适的环境变量以便高效开展项目。 ```python # 示例 Python 脚本片段展示如何通过 API 自动化部分 Altium 工作流程 import altium_api def create_pcb_document(project_name, board_dimensions): new_project = altium_api.Project(project_name) pcb_doc = new_project.add_board(board_dimensions['width'], board_dimensions['height']) return pcb_doc ``` --- ### 2. **解决 Visual Studio 开发中的常见问题** 依据另一条参考资料提到的情况[^2],“小陈”也可能具备处理复杂编程环境中遇到的技术难题的能力。例如当尝试加载某个 UI 文件失败时给出合理解释及解决方案。这种技能表明他对微软生态系统下的软件架构有着深刻理解。 对于此类错误消息:“The designer cannot be shown because the document for it was never loaded”,一般建议采取以下措施排查原因: - 确认目标框架版本是否匹配实际运行条件; - 清理重建整个解决方案以消除残留数据干扰; - 更新至最新版 SDK 或补丁包从而修复已知漏洞; --- ### 3. **图形界面开发经验分享** 最后一条参考文献指出存在两种实现 GUI 应用程序的方式——借助可视化编辑器 Qt Designer 和纯编码构建[^3]。“小陈”很可能更倾向于前者,因为这种方法不仅效率高而且灵活性强。凭借这一专长,他应该制作过不少实用型桌面应用程序原型演示视频或是撰写技术博客文章介绍最佳实践案例。 总结而言,虽然目前无法确切得知“Designer 小陈”到底创作了哪些具体的 IT 方面材料,但从上述三个方面可以看出他的兴趣范围广泛涵盖了硬件电路规划、软件调试技巧乃至于交互式前端呈现等多个维度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值