题22:假设有k个人质和k个绑匪围成一圈。人质的编号从1到k,绑匪的编号从k+1到2k。从编号1开始,每次从其中选出第m个人(隔m-1选出一人)出列。希望求出m的最小值,使得最先出列的k个人都是绑匪,即都是编号从k+1到2k的人。
cpp
#include <bits/stdc++.h>
using namespace std;
bool Yue(vector<int> n,int shu){
int count = 0;
int index = 0;
while(count < n.size()/2){//循环n次
int singleCount=0;
while(singleCount<shu){
if(n[index]==0){
singleCount++;
}
index = (index + 1)%n.size();
}
index = (index - 1 + n.size()) % n.size();
if(index<n.size()/2)return false;
else n[index]=1;
count++;
}
return true;
}
int main(){
int num;
while(cin>>num){
vector<int> people(2*num,0);
for(int i=num;;i++){
if( Yue(people,i) ){
cout<<i<<endl;
break;
}
}
}
}
- 超时
- 每次数位置都是先计数再后移再判断下次循环,缺少了循环后后移回原来位置的语句 index = (index - 1 + n.size()) % n.size();
题25:某商店规定:三个空汽水瓶可以换一瓶汽水,允许借一瓶换一瓶。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?
cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
int num=0;
while(cin>>num){
if(num==0)break;
int ans=0;
while(num>=3){
int NewBottle = num/3;
ans += NewBottle;
num -= NewBottle*3;//换掉空瓶子
num += NewBottle; //喝的新瓶子
}
if(num==2)ans++;//还可以借一个
cout<<ans<<endl;
}
return 0;
}
-
错误之处:用ans += num/3,并用num与ans做运算,但是ans是累积得到的汽水数,而不是当前一轮的汽水数。
-
程序设计要区分循环运算变量num和累积变量ans,不能混淆计算。
题26:你的任务是找到阶乘最后面的非零位。举个例子,5!=1*2*3*4*5=120所以5!的最后面的非零位是2,7!=1*2*3*4*5*6*7=5040,所以最后面的非零位是4。
cpp
#include <bits/stdc++.h>
using namespace std;
int CountNum25(int num,int redix){
int ans=0;
while(num%redix==0){
ans++;num/=redix;
}
return ans;
}
int main(){
int n;
cin>>n;
int count2=0,count5=0;//乘数中的2和5的个数
int ans=1;
for(int i=2;i<=n;i++){
int num=i;
count2+=CountNum25(num,2);
count5+=CountNum25(num,5);
while(num%2==0)num/=2;
while(num%5==0)num/=5;
ans*=num;
ans = ans%10;
}
if(count2>count5){
for(int i=0;i<(count2-count5);i++){
ans*=2;ans = ans%10;
}
}
else if(count2<count5)for(int i=0;i<count5-count2;i++)ans*=5;ans = ans%10;
cout<<ans%10;
system("pause");
}
- 粗心了,最后乘回多余2的时候没有取余,以为int够大,但是输出0,检查了好一会的逻辑,实际上是溢出了输出0
A potential issue with this encoder--decoder approach is that a neural network needs to be able to compress all the necessary information of a source sentence into a fixed-length vector. This may make it difficult for the neural network to cope with long sentences, especially those that are longer than the sentences in the training corpus. Cho et al. (2014b) showed that indeed the performance of a basic encoder--decoder deteriorates rapidly as the length of an input sentence increases.
- corpus 语料库
- 编码-解码器的方法有一个潜在的问题是神经网络需要能够将源语句的所有必要信息压缩成固定长度的向量。这可能让神经网络难以处理长句子,特别是那些比语料库里的句子更长的句子。Cho展示了基础编码-解码器的性能随着一个输入句子长度的增加而不断恶化。
In order to address this issue, we introduce an extension to the encoder--decoder model which learns to align and translate jointly. Each time the proposed model generates a word in a translation, it (soft-)searches for a set of positions in a source sentence where the most relevant information is concentrated. The model then predicts a target word based on the context vectors associated with these source positions and all the previous generated target words.
- 为了处理这一问题,我们引入了编码-解码器模型的一个 扩展,该扩展 学习将对齐与翻译过程联合起来进行 对齐和联合翻译。每次提出的模型产出一个单词的翻译,它会搜索一组 最相关的浓缩信息在源句子中的位置 坐标集。然后模型基于与上下文向量来预测目标单词,上下文向量与这些源坐标和所有先前已生成的目标单词相关联。