代码哪里出错了,刚开始学c++,不知道编码哪里出错了,希望可以帮忙指正错误
应改为90<=a&&a<=100的形式
#include<iostream>
using namespace std;
int main()
{
int a;
cin>>a;
if(90<=a&&a<=100)
cout<<"A";
else if(80<=a&&a<90)
cout<<"B";
else if(70<=a&&a<80)
cout<<"C";
else if(60<=a&&a<70)
cout<<"D";
else if(a<60)
cout<<"E";
else
cout<<"Error";
return 0;
}
代码中的一个值在一个范围内,需要用&&运算符
#include<iostream>
using namespace std;
int main()
{
int a;
cin>>a;
if(90<=a&&a<=100)
cout<<"A"<<std::endl;
else if(80<=a&&a<90)
cout<<"B"<<std::endl;
else if(70<=a&&a<80)
cout<<"C"<<std::endl;
else if(60<=a&&a<70)
cout<<"D"<<std::endl;
else if(a<60)
cout<<"E"<<std::endl;
else
cout<<"Error"<<std::endl;
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:广度优先搜索和 Dijkstra 算法默认会探索整张地图。如果我们只是寻找某一个目标点,我们可以增加一个判断if (current == goal)
,一旦我们找到目标点,便立即退出循环。
#include "redblobgames/pathfinding/a-star/implementation.cpp"
template<typename Location, typename Graph>
std::unordered_map<Location, Location>
breadth_first_search(Graph graph, Location start, Location goal) {
std::queue<Location> frontier;
frontier.push(start);
std::unordered_map<Location, Location> came_from;
came_from[start] = start;
while (!frontier.empty()) {
Location current = frontier.front();
frontier.pop();
if (current == goal) {
break;
}
for (Location next : graph.neighbors(current)) {
if (came_from.find(next) == came_from.end()) {
frontier.push(next);
came_from[next] = current;
}
}
}
return came_from;
}
int main() {
GridLocation start{8, 7};
GridLocation goal{17, 2};
SquareGrid grid = make_diagram1();
auto came_from = breadth_first_search(grid, start, goal);
draw_grid(grid, 2, nullptr, &came_from);
}
运行结果:
. > > > v v v v v v v v v v v v < . . . . ####. . . . . . .
> > > > > v v v v v v v v v v < < < . . . ####. . . . . . .
> > > > > v v v v v v v v v < < < < . . . ####. . . . . . .
> > ^ ####v v v v v v v v < < < < < < . . ####. . . . . . .
. ^ ^ ####> v v v v v v < ####^ < < . . . ####. . . . . . .
. . ^ ####> > v v v v < < ####^ ^ . . . . ##########. . . .
. . . ####> > > v v < < < ####^ . . . . . ##########. . . .
. . . ####> > > * < < < < ####. . . . . . . . . . . . . . .
. . . ####> > ^ ^ ^ < < < ####. . . . . . . . . . . . . . .
. . v ####> ^ ^ ^ ^ ^ < < ####. . . . . . . . . . . . . . .
. v v ####^ ^ ^ ^ ^ ^ ^ < ####. . . . . . . . . . . . . . .
> v v ####^ ^ ^ ^ ^ ^ ^ ^ ####. . . . . . . . . . . . . . .
> > > > > ^ ^ ^ ^ ^ ^ ^ ^ ####. . . . . . . . . . . . . . .
> > > > ^ ^ ^ ^ ^ ^ ^ ^ ^ ####. . . . . . . . . . . . . . .
. > > ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ####. . . . . . . . . . . . . . .
从结果来看可以发现,现在算法并没有探索整张地图,而是提前结束了。