一、
题目:
循环数
作者: xxx
时间限制: 1s
章节: 一维数组
问题描述
循环数是那些不包括0这个数字的没有重复数字的整数 (比如说, 81362) 并且同时具有一个有趣的性质, 就像这个例子:
如果你从最左边的数字( 记为n,在这个例子中是8) 开始往右边数,一直数n个数字(如果已经到了最右边则回到最左边),你会停在另一个不同的数字上。如果停在一个相同的数字上,这个数就不是循环数。
就像: 8 1 3 6 2 从最左边接下去数8个数字: 1 3 6 2 8 1 3 6 所以下一个数字是6. 重复这样做 (这次从'6'开始数6个数字) 并且你会停止在一个新的数字上: 2 8 1 3 6 2, 也就是2. 再这样做 (这次数两个): 8 1。 再一次 (这次数一个): 3。 又一次: 6 2 8, 这时你回到了起点。
此时,我们数到的数字依次是:8 6 2 1 3,即每一个数字都被数了1次,并且回到了起点。
如果你将每一个数字都数了1次以后没有回到起点, 你的数字不是一个循环数。
给你一个数字 M (在1到9位之间), 找出第一个比 M大的循环数(输入的M保证这个循环数能保存在4位的有符号整数中)。
输入说明
仅仅一行, 包括M
输出说明
仅仅一行,包括第一个比M大的循环数。
cpp
#include <bits/stdc++.h>
using namespace std;
bool n_r(int a)
{
string b=to_string(a);
int len=b.length();
for(int i=0;i<len-1;i++)
{
for(int j=i+1;j<len;j++)
{
if(b[i]==b[j]||b[i]=='0')
return false;
}
}
return true;
}
bool pd(int a)
{
if(!n_r(a)) return false;
string b=to_string(a);
int count[10000]={0};
count[0]=1;
int len=b.length();
char c=b[0];
int num=(b[0]-'0')%len;
char d=b[num];
while(b[0]!=d)
{
c=d;
count[num]++;
num=(num+(b[num]-'0'))%len;
d=b[num];
for(int i=0;i<len;i++)
{
if(count[i]>1) return false;
}
}
for(int i=0;i<len;i++)
{
if(count[i]!=1) return false;
}
return true;
}
int main()
{
int n;
cin>>n;
int a=n+1;
while(!pd(a))
{
a++;
}
cout<<a;
return 0;
}
总结:1.我设计了两个函数 一个判断是否重复和含0 一个判断循环
2.用到了约瑟夫环的公式 同时复习了i=1开始是-1+1,i=0开始不用这样
3.利用count数组来判断是否只用了一次
二、
题目:
棋盘游戏
作者: xxx
时间限制: 1s
章节: 宽度优先搜索
问题描述
大小为3的棋盘游戏里有3个白色棋子,3个黑色棋子,和一个有7个格子一线排开的木盒子。3个白棋子被放在一头,3个黑棋子被放在另一头,中间的格子空着。
初始状态: WWW_BBB
目标状态: BBB_WWW
在这个游戏里有两种移动方法是允许的:
-
你可以把一个棋子移到与它相邻的空格;
-
你可以把一个棋子跳过一个(仅一个)与它不同色的棋子到达空格。
大小为N的棋盘游戏包括N个白棋子,N个黑棋子,还有有2N+1个格子的木盒子。
这里是3-棋盘游戏的解,包括初始状态,中间状态和目标状态:
WWW BBB
WW WBBB
WWBW BB
WWBWB B
WWB BWB
W BWBWB
WBWBWB
BW WBWB
BWBW WB
BWBWBW
BWBWB W
BWB BWW
B BWBWW
BB WBWW
BBBW WW
BBB WWW
请编一个程序解大小为N的棋盘游戏(1 <= N <= 12)。要求用最少的移动步数实现。
输入说明
一个整数N。
输出说明
用空格在棋盘的位置(位置从左到右依次为1, 2, ..., 2N+1)表示棋盘的状态。输出棋盘的状态变换序列,每行20个数(除了最后一行)。 输出的解还应当有最小的字典顺序(即如果有多组移动步数最小的解,输出第一个数最小的解;如果还有多组,输出第二个数最小的解;...)。
cpp
#include <bits/stdc++.h>
using namespace std;
int a[10000];//存最终空格位置
int b[10000];//存此次路径的空格位置
int sum=99999;
//基本思想DFS+剪枝 递归能移动的四种情况 当不能移动时返回 最终now=tar代表匹配成果
string now=" ";
string tar=" ";
int n;
void dfs(int step,int pos)
{
if(step>=sum) return;
if (now == tar)//更新答案
{
sum = step;
for (int i = 1;i <= step;i++)
{
a[i] = b[i];
}
return ;
}
if (pos - 2 > 0 && now[pos - 2] == 'W' && now[pos - 1] == 'B')//白子跳一格
{
b[step + 1] = pos - 2;
now[pos - 2] = ' ';
now[pos] = 'W';
dfs(step + 1,pos - 2);
b[step + 1] = 0;//回溯
now[pos - 2] = 'W';
now[pos] = ' ';
}
if (pos - 1 > 0 && now[pos - 1] == 'W')//白子挪一格
{
b[step + 1] = pos - 1;
now[pos - 1] = ' ';
now[pos] = 'W';
dfs(step + 1,pos - 1);
b[step + 1] = 0;
now[pos - 1] = 'W';
now[pos] = ' ';
}
if (pos + 1 <= n * 2 + 1 && now[pos + 1] == 'B')//黑子挪一格
{
b[step + 1] = pos + 1;
now[pos + 1] = ' ';
now[pos] = 'B';
dfs(step + 1,pos + 1);
b[step + 1] = 0;
now[pos + 1] = 'B';
now[pos] = ' ';
}
if (pos + 2 <= n * 2 + 1 && now[pos + 2] == 'B' && now[pos + 1] == 'W')//黑子跳一格
{
b[step + 1] = pos + 2;
now[pos + 2] = ' ';
now[pos] = 'B';
dfs(step + 1,pos + 2);
b[step + 1] = 0;
now[pos + 2] = 'B';
now[pos] = ' ';
}
return ;
}
int main()
{
cin >> n;
for (int i = 1;i <= n;i++)//产生初始状态与目标状态
{
now += 'W';
tar += 'B';
}
now += ' ';
tar += ' ';
for (int i = 1;i <= n;i++)
{
now += 'B';
tar += 'W';
}
dfs(0,n + 1);
int flag=1;
for (int i = 1;i <= sum;i++)
{
if(flag==1)
{
cout<<a[i];
flag=0;
}else
{
cout<< " "<<a[i];
}
if (i % 20 == 0)//每行20个数
{
cout << endl;
flag=1;
}
}
cout << endl;
return 0;
}
总结:基本思想DFS+剪枝 递归能移动的四种情况 当不能移动时返回 最终now=tar代表匹配成果
三、
分数化小数
作者: xxx
时间限制: 1s
章节: 字符串
问题描述
写一个程序,输入一个形如N/D的分数(N是分子,D是分母),输出它的小数形式。
如果小数有循环节的话,把循环节放在一对圆括号中。
例如, 1/3 = .33333333 写成0.(3)
41/333 = 0.123123123... 写成0.(123)
用xxx.0 表示整数
典型的转化例子: 1/3 = 0.(3)
22/5 = 4.4
1/7 = 0.(142857)
2/2 = 1.0
3/8 = 0.375
45/56 = 0.803(571428)
输入说明
单独的一行包括被空格分开的 N和D, 1 <= N,D <= 100000。
输出说明
小数的表示方法上面说的很明白了,如果输出的长度超过76个字符,每行输出76个字符(包括小数点、括号等)。
cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d;
cin >> n >> d;
// 处理整数部分
int integer = n / d;
int remainder = n % d;
// 特殊情况:整数
if (remainder == 0) {
cout << integer << ".0" << endl;
return 0;
}
// 存储小数部分
string decimal = "";
// 用数组记录每个余数第一次出现的位置
// 余数范围是 0 到 d-1,所以数组大小为 d
int pos[100000] = {0}; // 初始化为0,0表示未出现过
// 注意:余数0是结束标志,所以实际余数从1开始记录
int index = 1; // 位置从1开始,避免和0混淆
bool repeating = false;
int repeatStart = 0;
while (remainder != 0) {
// 如果余数重复出现,找到循环节
if (pos[remainder] != 0) {
repeating = true;
repeatStart = pos[remainder]; // 循环开始的位置
break;
}
// 记录余数位置
pos[remainder] = index;
index++;
// 模拟除法
remainder *= 10;
int digit = remainder / d;
decimal += char(digit + '0');
remainder %= d;
}
// 输出结果
string result = to_string(integer) + ".";
if (repeating) {
// 输出不循环部分(位置从1开始,所以索引要减1)
result += decimal.substr(0, repeatStart - 1);
// 输出循环部分
result += "(" + decimal.substr(repeatStart - 1) + ")";
} else {
result += decimal;
}
// 处理输出长度限制(每行76个字符)
for (int i = 0; i < result.length(); i++) {
cout << result[i];
if ((i + 1) % 76 == 0 && i != result.length() - 1) {
cout << endl;
}
}
cout << endl;
return 0;
}
总结:利用了模拟除数法,具体思路是如1/3,当小数位的本质是1%3余数 *10/3,所以这道题的思路是整数和小数位置分别计算,而小数位置的计算是当前余数*10/d,为了找到循环节,也就是余数不会重复出现,所以记录每次余数出现的位置,当余数第二次出现,代表有重复的循环节。
四、
翻译:
In recent years, pre-trained models have played an important role in artificial intelligence research. Researchers typically first train a general model using large-scale datasets and then fine-tune it on specific tasks. In this way, the model can utilize the knowledge learned during the pre-training stage to improve the performance of downstream tasks . For example, in the field of natural language processing, many language models are pre-trained on massive text corpora and achieve excellent results in tasks such as text classification, machine translation, and question answering. The pre-training and fine-tuning framework not only reduces training costs but also improves the generalization ability of models. Therefore, this approach has become an important paradigmin modern artificial intelligence research.
在最近几年中,预训练模型已经在人工智能的研究中发挥了重要的作用。研究通常 先使用大规模的数据集训练通用 模型之后再在具体任务上进行微调。 在这种方式下,模型可以在预训练阶段中运用所学知识 并改善下游任务的 表现。例如,在自然语言处理领域中,很多语言模型在大型的文本语料库中进行预训练,并在一些任务中取得极好的成就例如文本分类,机器翻译,还有问题回答。这种预训练和微调的框架不仅能减少训练的花费,而且能提高模型的**泛化能力。**因此,这个方法已经变成了一个重要的范例在现代的人工智能研究中。
修改:
近年来,预训练模型在人工智能研究中发挥着重要作用。研究者通常先用大规模数据集训练通用模型,再针对具体任务进行微调。通过这种方式,模型能利用预训练阶段学到的知识来提升下游任务性能。例如在自然语言处理领域,许多语言模型通过海量文本语料库预训练后,在文本分类、机器翻译和问答系统等任务中取得优异效果。预训练-微调框架不仅能降低训练成本,还可增强模型泛化能力,因而成为现代人工智能研究的重要范式。
