# 1.小易的升级之路

```#include <stdio.h>

#include  <iostream>
using namespace std;

//辗转相除法求两数最大公约数（欧几里德算法）
int euclid(int a,int b){
//要求a和b是正整数
if(a<=0||b<=0)
return -1;
int tmp=a%b;
while(tmp!=0){
a=b;
b=tmp;
tmp=a%b;
}
return b;
}

int monsterNum_easePower[2]={2};
int monsterPower[100000]={0};

int main(int argc,char* argv[]){
//while(cin.peek()!=EOF){ //不行
//while(cin.peek()!=EOF&&cin.peek()!='\n'&&cin.peek()!=' '){不行
while(cin>>monsterNum_easePower[0]>>monsterNum_easePower[1]){
//cin>>monsterNum_easePower[0]>>monsterNum_easePower[1];
for(int i=0;i<monsterNum_easePower[0];++i){
cin>>monsterPower[i];
if(monsterPower[i]>monsterNum_easePower[1])
monsterNum_easePower[1]+=euclid(monsterNum_easePower[1],monsterPower[i]);
else
monsterNum_easePower[1]+=monsterPower[i];
}
cout<<monsterNum_easePower[1]<<endl;
}
}```

```operator void*() const
{ return this->fail() ? 0 : const_cast<basic_ios*>(this); }

bool operator!() const
{ return this->fail(); }  ```

```while(cin)  =====> while(!cin.fail())              //while the stream is OK
while(!cin) =====> while(cin.fail())               //while the stream is NOT OK```

（3）判断测试数据是否读取完，不能使用下面两行，真不知道其输入的测试数据是以什么样的形式结束的，有知道的往右也可留言告知。

```    //while(cin.peek()!=EOF)
//while(cin.peek()!=EOF&&cin.peek()!='\n'&&cin.peek()!=' ')```

# 2.炮台攻击

```#include  <iostream>
using namespace std;

int R,x1,y1,x2,y2,x3,y3,x0,y0;

//func:判断两点间的具体是否在范围R内
//para:R:炮台攻击范围;敌人坐标:(x0,y0);炮台坐标:(x1,y1)
bool withinRange(int R,int x0,int y0,int x1,int y1){
if((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)>R*R)
return false;
else
return true;
}

int main(int argc,char* argv[]){
int barbetteNum=0;
while(cin>>R>>x1>>y1>>x2>>y2>>x3>>y3>>x0>>y0){
barbetteNum=0;
if(withinRange(R,x0,y0,x1,y1))
++barbetteNum;
if(withinRange(R,x0,y0,x2,y2))
++barbetteNum;
if(withinRange(R,x0,y0,x3,y3))
++barbetteNum;
cout<<barbetteNum<<'x'<<endl;
}
}```

# 3.扫描透镜

```#include <string.h>

#include <vector>
#include  <iostream>
using namespace std;

int mushroomNum=0;                  //蘑菇总数
int N,M;                            //草地长宽（格数）:N行M列

//func:给定3*3方格最下角方格坐标和草地的长宽和蘑菇分布计算蘑菇数目求3*3方格内的蘑菇数目
//para:(x,y):第x行y列的方格；N：草地行数，M：草地列数；grassLandMushroom:草地蘑菇数量
int getMushroomGridNum(int x,int y,int N,int M,int grassLandMushroom[][20]){
int mushroomGridNum=0;
for(int i=0;i<3;++i)
for(int j=0;j<3;++j){
if(x+i<N&&y+j<M&&grassLandMushroom[x+i][y+j]>0)
++mushroomGridNum;
}
return mushroomGridNum;
}

//func:获取草地中3*3方格中拥有蘑菇的最多方格数
//para:N：草地行数，M：草地列数；grassLandMushroom:草地蘑菇数量
vector<int> getMaxMushroomNumPos(int N,int M,int grassLandMushroom[][20]){
//构造三个0,pos[0]:行,pos[1]:列,pos[2]:有蘑菇的方格数
vector<int> pos(3,0);
int maxMushroomGridNum=0;
//maxMushroomNum=getMushroomNum(0,0,N,M,grassLandMushroom);

int tmp=0;
for(int i=0;i<N;++i){
for(int j=0;j<M;++j){
tmp=getMushroomGridNum(i,j,N,M,grassLandMushroom);
if(tmp>maxMushroomGridNum){
maxMushroomGridNum=tmp;
pos[0]=i;
pos[1]=j;
pos[2]=maxMushroomGridNum;
}
if(j>=M-3) break;
}
if(i>=N-3) break;
}
return  pos;
}

//func:更新3*3方格内草地蘑菇数量
//para:(x0,y0):第x0行y0列的方格；N：草地行数，M：草地列数；grassLandMushroom:草地蘑菇数量
void updateMushroomNum(int x,int y,int N,int M,int grassLandMushroom[][20]){
for(int i=0;i<3;++i)
for(int j=0;j<3;++j){
if(x+i<N&&y+j<M&&grassLandMushroom[x+i][y+j]>0)
--grassLandMushroom[x+i][y+j];
}
}

int main(int argc,char* argv[]){
int x=0,y=0;
while(cin>>N>>M>>mushroomNum){

int grassLandMushroom[20][20]={0};  //草地蘑菇分布

for(int i=0;i<mushroomNum;++i){
cin>>x>>y;
++grassLandMushroom[x-1][y-1];
}
//扫描镜第一次扫描
vector<int> pos1=getMaxMushroomNumPos(N,M,grassLandMushroom);
updateMushroomNum(pos1[0],pos1[1],N,M,grassLandMushroom);

//扫描镜第二次扫描
vector<int> pos2=getMaxMushroomNumPos(N,M,grassLandMushroom);

cout<<pos1[2]+pos2[2]<<endl;
}
}```

# 参考文献

[1]while(cin)的结束判断. [2]牛客网:http://www.nowcoder.com/test/question/analytic?tid=2679679. [3]另一位网友的实现.

471 篇文章39 人订阅

0 条评论

## 相关文章

38830

29160

11430

50280

13400

98140

222100

### 算法之美——算法复杂性

《趣学算法》在线章节：http://www.epubit.com.cn/book/details/4825

29410

19550

22290