我正在尝试在标准输出中编写图,程序返回“#”行,在另一个函数更改它们后,这些行应加到初始数量上。我保证修改数字的功能不会出错。这是我的代码:
struct mystruct {
long long int s;
long long int i;
long long int r;
}
mystruct initial_;
void draw_row(mystruct P)
{
long long int Ntotal = initial_.s + initial_.i;
int scale = round(Ntotal / 10);
std::string a(Ntotal / scale, '#');
std::string b(round(P.s / scale), '#');
std::string c(round(P.i / scale), '#');
std::string d(round(P.r / scale), '#');
std::cout << P.s << " " << P.i << " " << P.r << '\n';
std::cout << scale << '\n';
std::cout << a << '\n' << b << c << d << '\n';
}
这些是其某些输出的示例:499 1 0
##########
#########
0 450 50
##########
##########
0 249 251
##########
#########
最佳答案
您正在行中进行整数除法
int scale = round(Ntotal/10) ;
std::string a(Ntotal/scale,'#') ;
std::string b(round(P.s/scale),'#') ;
std::string c(round(P.i/scale),'#') ;
std::string d(round(P.r/scale),'#') ;
并且其余部分被截断,因此此处使用的round()
无法正常工作。您可以执行
(A + B/2) / B
来取整两个正整数A / B
的除法结果,因此行应为int scale = (Ntotal+5)/10 ;
std::string a(Ntotal/scale,'#') ;
std::string b((P.s+scale/2)/scale,'#') ;
std::string c((P.i+scale/2)/scale,'#') ;
std::string d((P.r+scale/2)/scale,'#') ;
关于c++ - 定义比例因子以图形化三个量,其总和为常数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63560386/