#include #include #include #include using namespace std; int main(){ int n; double sum=0; cin>>n; if(n<=0)//判断 { cout<'表达式的值='<<sum<<endl; return 0; } for(int i=1;i<=n;i++)//累加 { sum+=pow(i,2); } sum/=n;//求平均值 cout<<fixed<<setprecision(4)<<'表达式的值='<<sum<<endl;//格式化输出,保留四位小数 return 0; }

C++程序:计算表达式 (1^2 + 2^2 + ... + n^2) / n

原文地址: https://www.cveoy.top/t/topic/njnD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录