作者:nw3344_575 | 来源:互联网 | 2023-09-24 19:54
续上一篇文章https:blog.csdn.netleleprogrammerarticledetails127164315https:blog.csdn.netleleprog
续上一篇文章
https://blog.csdn.net/leleprogrammer/article/details/127164315
https://blog.csdn.net/leleprogrammer/article/details/127164315剩下最后5道题,继续鸭~~
目录
41 数字统计
42 画矩形
43 质因数分解
44 第n小的质数
45 金币
41 数字统计
![](https://img.php1.cn/3cd4a/1eebe/cd5/bcafc120671304eb.webp)
#include
using namespace std;int main() {int l,r,result=0;cin>>l>>r;for (int i=l;i<=r;++i) {int i2=i;while (true) {int w=i2%10;i2/=10;if (w==2) {result++;}if (i2==0) {break;}}}cout<}
又是一道分解数位的题~~~~
42 画矩形
![](https://img.php1.cn/3cd4a/1eebe/cd5/6789f68dabde0aed.png)
#include
using namespace std;int main() {short w,h,a;char c;cin>>h>>w>>c>>a;a=(bool)a;for (int i=1;i<=h;++i) {for (int j=1;j<=w;++j) {if ((a)||((!a)&&(i==1||i==h||j==1||j==w)))cout<}
这道题仿佛又回归输入输出那一篇章了~~~
43 质因数分解
![](https://img.php1.cn/3cd4a/94ce/a6e/865776f6fe3d5f59.jpeg)
#include
using namespace std;int main() {int n,z=0;cin>>n;for (int i=2;i<=sqrt(n);++i) {bool a=false;for (int j=2;j<=sqrt(i);++j) {if (i%j==0) {a=true;break;}}if ((!a)&&n%i==0) {cout<}
直接通过开根号对半,减少算法时间, 因为如果不这样写的话,很大几率这样:
![](https://img.php1.cn/3cd4a/1eebe/cd5/a5d7215df572c386.webp)
这里开根号用到头文件,懒得再写一行,直接用万能头文件
44 第n小的质数
![](https://img.php1.cn/3cd4a/1eebe/cd5/6c257b6ba227cc3e.webp)
#include
using namespace std;int main() {int n,c=0,i=1;cin>>n;while (c}
这道题同样sqrt对半来算,高效一点
45 金币
![](https://img.php1.cn/3cd4a/1eebe/cd5/8ad8f3bf8da691df.webp)
#include
using namespace std;int main() {int d,gold=0,c=0;cin>>d;while (d>c) {c++;d-=c;gold+=c*c;}if (d>0) {c++;gold+=d*c;}cout<}
好啦,这样我们OpenJudge NOI 1.5章的所有题目就这样告一段落啦!!
![](https://img.php1.cn/3cd4a/1eebe/cd5/1113165c4904ecc5.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/bcafc120671304eb.webp)
喜欢就关注一下吧~~~