OJ1 哈夫曼树问题描述Huffman树在编码中有着广泛的应用。在这里我们只关心Huffman树的构造过程。给出一列数{pi}{p0,p1, …,pn-1}用这列数构造Huffman树的过程如下1. 找到{pi}中最小的两个数设为pa和pb将pa和pb从{pi}中删除掉然后将它们的和加入到{pi}中。这个过程的费用记为papb。2. 重复步骤1直到{pi}中只剩下一个数。在上面的操作过程中把所有的费用相加就得到了构造Huffman树的总费用。本题任务对于给定的一个数列现在请你求出用该数列构造Huffman树的总费用。例如对于数列{pi}{5, 3, 8, 2, 9}Huffman树的构造过程如下1. 找到{5, 3, 8, 2, 9}中最小的两个数分别是2和3从{pi}中删除它们并将和5加入得到{5, 8, 9, 5}费用为5。2. 找到{5, 8, 9, 5}中最小的两个数分别是5和5从{pi}中删除它们并将和10加入得到{8, 9, 10}费用为10。3. 找到{8, 9, 10}中最小的两个数分别是8和9从{pi}中删除它们并将和17加入得到{10, 17}费用为17。4. 找到{10, 17}中最小的两个数分别是10和17从{pi}中删除它们并将和27加入得到{27}费用为27。5. 现在数列中只剩下一个数27构造过程结束总费用为510172759。输入说明输入的第一行包含一个正整数nn100。接下来是n个正整数表示p0,p1, …,pn-1每个数不超过1000。输出说明输出用这些数构造Huffman树的总费用。输入范例55 3 8 2 9输出范例59个人总结主要出问题的地方是C语法上的细节部分erase左闭右开--删除区间。代码#includebits/stdc.h using namespace std; int main() { int n; cinn; vectorint arr(n); for(int i0;in;i) { cinarr[i]; } int total0; while(arr.size()1) { sort(arr.begin(),arr.end()); int aarr[0]; int barr[1]; int sumab; totalsum; arr.erase(arr.begin(),arr.begin()2); arr.push_back(sum); } couttotalendl; return 0; }2 回文数问题描述1221是一个非常特殊的数它从左边读和从右边读是一样的编程求大于等于n的所有这样的四位十进制数。输入说明输入一个整数nn9999输出说明按从小到大的顺序输出满足条件的四位十进制数。输入范例9888输出范例98899999个人总结int startmax(n,1000);没有考虑到四位数其实是从1000开始算起。代码#includebits/stdc.h using namespace std; bool is_Huiwen(int n) { string sto_string(n); string temps; reverse(temp.begin(),temp.end()); return temps; } int main() { int n; cinn; int startmax(n,1000); for(int xstart;x10000;x) { if(is_Huiwen(x)) coutxendl; } return 0; }3 字母图形问题描述利用字母可以组成一些美丽的图形下面给出了一个例子ABCDEFGBABCDEFCBABCDEDCBABCDEDCBABC这是一个5行7列的图形请找出这个图形的规律并输出一个n行m列的图形。输入说明输入一行包含两个整数n和m分别表示你要输出的图形的行数的列数。1 n, m 26。输出说明输出n行每行m个字符为你的图形。无多余空格或空行。个人总结关键就是找到 str[row][col]Aabs(row-col);即‘A偏移量代码#includebits/stdc.h using namespace std; int main() { int n,m;//row col; cinnm; char str[n][m]; str[0][0]A; for(int row0;rown;row) { for(int col0;colm;col) { str[row][col]Aabs(row-col); } } for(int row 0; row n; row) { for(int col 0; col m; col) { cout str[row][col]; } cout endl; } return 0; } /* ABCDEFG 0123456 BABCDEF 1012345 CBABCDE 2102134 DCBABCD 3210123 EDCBABC 4321012 */4 大阶乘计算问题描述输入一个正整数n输出n!的值。其中n!1*2*3*…*n。n!可能很大而计算机能表示的整数范围有限需要使用高精度计算的方法。参考算法使用一个数组A来表示一个大整数aA[0]表示a的个位A[1]表示a的十位依次类推。将a乘以一个整数k变为将数组A的每一个元素都乘以k请注意处理相应的进位。首先将a设为1然后乘2乘3当乘到n时即得到了n!的值。输入说明输入包含一个正整数nn1000。输出说明输出n!的准确值。个人总结用数组模拟大整数低位在前高位在后逐位乘以当前的i2---n并且处理进位然后逆序输出得到最终结果。代码#includebits/stdc.h #define MAX 10000 using namespace std; int main() { int n; cinn; int A[MAX]{1}; int len1; for(int i2;in;i) { int carry0; for(int j0;jlen;j) { int tempA[j]*icarry; A[j]temp%10; carrytemp/10; } while(carry) { A[len]carry%10; carry/10; len; } } for(int ilen-1;i0;i--) coutA[i]; return 0; }5 回形取数问题描述回形取数就是沿矩阵的边取数若当前方向上无数可取或已经取过则左转90度。一开始位于矩阵左上角方向向下。输入说明输入第一行是两个不超过200的正整数m, n表示矩阵的行和列。接下来m行每行n个整数表示这个矩阵。输出说明输出只有一行共mn个数为输入矩阵回形取数得到的结果。数之间用一个空格分隔行末不要有多余的空格。个人总结输入 m 行 n 列的矩阵从左上角 (0,0) 开始方向按照下、右、上、左 循环遇到边界或已访问的格子就左转90°直到取完所有数。代码#includebits/stdc.h using namespace std; int main() { int m,n; cinmn; int p[m][n]; int visited[m][n]; // 不能用{0}初始化VLA改为手动初始化 // 手动初始化visited为0 for(int i0; im; i) for(int j0; jn; j) visited[i][j] 0; for(int row0; rowm; row) { for(int col0; coln; col) { cinp[row][col]; } } int totalm*n; int count0; int r0, c0; int direction0; // 0:下1右2上3左 while(counttotal) { coutp[r][c]; //输出当前值 visited[r][c]1; count; if(counttotal) cout ; // 尝试找到下一个可走的方向和位置 int found 0; for(int try_dir 0; try_dir 4; try_dir) // 最多试4个方向 { int nr r, nc c; if(direction 0) // 下 { nr r 1; nc c; } else if(direction 1) // 右 { nr r; nc c 1; } else if(direction 2) // 上 { nr r - 1; nc c; } else if(direction 3) // 左 { nr r; nc c - 1; } // 检查这个方向是否可走 if(nr 0 nr m nc 0 nc n visited[nr][nc] 0) { // 找到了可走的方向 r nr; c nc; found 1; break; } // 不可走左转 direction (direction 1) % 4; } // 理论上一定找得到因为total个格子还没取完 if(!found) break; // 安全退出 } return 0; }英语The Transformer model is a neural network architecture based on the attention mechanism and has achieved great success in the field of natural language processing. Unlike traditional recurrent neural networks, Transformers do not rely on step-by-step sequence processing. Instead, they use self-attention mechanisms to process the entire sequence simultaneously. This architecture not only improves the model’s ability to perform parallel computation but also enables it to capture long-range dependencies more effectively. In machine translation tasks, the Transformer model can dynamically assign attention weights according to the relationships between different words in a sentence, thereby producing more accurate translations. In addition, Transformer architectures have been widely applied to tasks such as text generation, speech recognition, and even image processing. In recent years, most large-scale pre-trained language models have been built upon the Transformer architecture, which has significantly accelerated the development of artificial intelligence technologies.
3月17日打卡
OJ1 哈夫曼树问题描述Huffman树在编码中有着广泛的应用。在这里我们只关心Huffman树的构造过程。给出一列数{pi}{p0,p1, …,pn-1}用这列数构造Huffman树的过程如下1. 找到{pi}中最小的两个数设为pa和pb将pa和pb从{pi}中删除掉然后将它们的和加入到{pi}中。这个过程的费用记为papb。2. 重复步骤1直到{pi}中只剩下一个数。在上面的操作过程中把所有的费用相加就得到了构造Huffman树的总费用。本题任务对于给定的一个数列现在请你求出用该数列构造Huffman树的总费用。例如对于数列{pi}{5, 3, 8, 2, 9}Huffman树的构造过程如下1. 找到{5, 3, 8, 2, 9}中最小的两个数分别是2和3从{pi}中删除它们并将和5加入得到{5, 8, 9, 5}费用为5。2. 找到{5, 8, 9, 5}中最小的两个数分别是5和5从{pi}中删除它们并将和10加入得到{8, 9, 10}费用为10。3. 找到{8, 9, 10}中最小的两个数分别是8和9从{pi}中删除它们并将和17加入得到{10, 17}费用为17。4. 找到{10, 17}中最小的两个数分别是10和17从{pi}中删除它们并将和27加入得到{27}费用为27。5. 现在数列中只剩下一个数27构造过程结束总费用为510172759。输入说明输入的第一行包含一个正整数nn100。接下来是n个正整数表示p0,p1, …,pn-1每个数不超过1000。输出说明输出用这些数构造Huffman树的总费用。输入范例55 3 8 2 9输出范例59个人总结主要出问题的地方是C语法上的细节部分erase左闭右开--删除区间。代码#includebits/stdc.h using namespace std; int main() { int n; cinn; vectorint arr(n); for(int i0;in;i) { cinarr[i]; } int total0; while(arr.size()1) { sort(arr.begin(),arr.end()); int aarr[0]; int barr[1]; int sumab; totalsum; arr.erase(arr.begin(),arr.begin()2); arr.push_back(sum); } couttotalendl; return 0; }2 回文数问题描述1221是一个非常特殊的数它从左边读和从右边读是一样的编程求大于等于n的所有这样的四位十进制数。输入说明输入一个整数nn9999输出说明按从小到大的顺序输出满足条件的四位十进制数。输入范例9888输出范例98899999个人总结int startmax(n,1000);没有考虑到四位数其实是从1000开始算起。代码#includebits/stdc.h using namespace std; bool is_Huiwen(int n) { string sto_string(n); string temps; reverse(temp.begin(),temp.end()); return temps; } int main() { int n; cinn; int startmax(n,1000); for(int xstart;x10000;x) { if(is_Huiwen(x)) coutxendl; } return 0; }3 字母图形问题描述利用字母可以组成一些美丽的图形下面给出了一个例子ABCDEFGBABCDEFCBABCDEDCBABCDEDCBABC这是一个5行7列的图形请找出这个图形的规律并输出一个n行m列的图形。输入说明输入一行包含两个整数n和m分别表示你要输出的图形的行数的列数。1 n, m 26。输出说明输出n行每行m个字符为你的图形。无多余空格或空行。个人总结关键就是找到 str[row][col]Aabs(row-col);即‘A偏移量代码#includebits/stdc.h using namespace std; int main() { int n,m;//row col; cinnm; char str[n][m]; str[0][0]A; for(int row0;rown;row) { for(int col0;colm;col) { str[row][col]Aabs(row-col); } } for(int row 0; row n; row) { for(int col 0; col m; col) { cout str[row][col]; } cout endl; } return 0; } /* ABCDEFG 0123456 BABCDEF 1012345 CBABCDE 2102134 DCBABCD 3210123 EDCBABC 4321012 */4 大阶乘计算问题描述输入一个正整数n输出n!的值。其中n!1*2*3*…*n。n!可能很大而计算机能表示的整数范围有限需要使用高精度计算的方法。参考算法使用一个数组A来表示一个大整数aA[0]表示a的个位A[1]表示a的十位依次类推。将a乘以一个整数k变为将数组A的每一个元素都乘以k请注意处理相应的进位。首先将a设为1然后乘2乘3当乘到n时即得到了n!的值。输入说明输入包含一个正整数nn1000。输出说明输出n!的准确值。个人总结用数组模拟大整数低位在前高位在后逐位乘以当前的i2---n并且处理进位然后逆序输出得到最终结果。代码#includebits/stdc.h #define MAX 10000 using namespace std; int main() { int n; cinn; int A[MAX]{1}; int len1; for(int i2;in;i) { int carry0; for(int j0;jlen;j) { int tempA[j]*icarry; A[j]temp%10; carrytemp/10; } while(carry) { A[len]carry%10; carry/10; len; } } for(int ilen-1;i0;i--) coutA[i]; return 0; }5 回形取数问题描述回形取数就是沿矩阵的边取数若当前方向上无数可取或已经取过则左转90度。一开始位于矩阵左上角方向向下。输入说明输入第一行是两个不超过200的正整数m, n表示矩阵的行和列。接下来m行每行n个整数表示这个矩阵。输出说明输出只有一行共mn个数为输入矩阵回形取数得到的结果。数之间用一个空格分隔行末不要有多余的空格。个人总结输入 m 行 n 列的矩阵从左上角 (0,0) 开始方向按照下、右、上、左 循环遇到边界或已访问的格子就左转90°直到取完所有数。代码#includebits/stdc.h using namespace std; int main() { int m,n; cinmn; int p[m][n]; int visited[m][n]; // 不能用{0}初始化VLA改为手动初始化 // 手动初始化visited为0 for(int i0; im; i) for(int j0; jn; j) visited[i][j] 0; for(int row0; rowm; row) { for(int col0; coln; col) { cinp[row][col]; } } int totalm*n; int count0; int r0, c0; int direction0; // 0:下1右2上3左 while(counttotal) { coutp[r][c]; //输出当前值 visited[r][c]1; count; if(counttotal) cout ; // 尝试找到下一个可走的方向和位置 int found 0; for(int try_dir 0; try_dir 4; try_dir) // 最多试4个方向 { int nr r, nc c; if(direction 0) // 下 { nr r 1; nc c; } else if(direction 1) // 右 { nr r; nc c 1; } else if(direction 2) // 上 { nr r - 1; nc c; } else if(direction 3) // 左 { nr r; nc c - 1; } // 检查这个方向是否可走 if(nr 0 nr m nc 0 nc n visited[nr][nc] 0) { // 找到了可走的方向 r nr; c nc; found 1; break; } // 不可走左转 direction (direction 1) % 4; } // 理论上一定找得到因为total个格子还没取完 if(!found) break; // 安全退出 } return 0; }英语The Transformer model is a neural network architecture based on the attention mechanism and has achieved great success in the field of natural language processing. Unlike traditional recurrent neural networks, Transformers do not rely on step-by-step sequence processing. Instead, they use self-attention mechanisms to process the entire sequence simultaneously. This architecture not only improves the model’s ability to perform parallel computation but also enables it to capture long-range dependencies more effectively. In machine translation tasks, the Transformer model can dynamically assign attention weights according to the relationships between different words in a sentence, thereby producing more accurate translations. In addition, Transformer architectures have been widely applied to tasks such as text generation, speech recognition, and even image processing. In recent years, most large-scale pre-trained language models have been built upon the Transformer architecture, which has significantly accelerated the development of artificial intelligence technologies.