noi寒假刷题之旅_ 1.5编程基础之循环控制(45题)
1.5编程基础之循环控制(45题)
01:求平均年龄
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> using namespace std; int main() { int n; float sum=0; cin>>n; for ( int i=0;i<n;++i) { int temp; cin>>temp; sum+=temp; } printf ( "%.2f" ,sum/n); return 0; } |
02:财务管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<iostream> using namespace std; int main() { float sum=0; float temp; for ( int i=0;i<12;++i) { cin>>temp; sum+=temp; } printf ( "$%.2f" ,sum/12); return 0; } |
03:均值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<iostream> using namespace std; int main() { int n; double sum=0,temp; cin>>n; for ( int i=0;i<n;++i) { cin>>temp; sum+=temp; } printf ( "%.4f" ,sum/( double )n); return 0; } |
04:求整数的和与均值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<iostream> using namespace std; int main() { int n; double sum=0,temp; cin>>n; for ( int i=0;i<n;++i) { cin>>temp; sum+=temp; } printf ( "%.0f %.5f" ,sum,sum/( double )n); return 0; } |
05:最高的分数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> using namespace std; int main() { int n; int max,temp; cin>>n; cin>>max; for ( int i=1;i<n;++i) { cin>>temp; if (max<temp)max=temp; } printf ( "%d" ,max); return 0; } |
06:整数序列的元素最大跨度值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<iostream> using namespace std; int main() { int m; int max,min,temp; cin>>m; cin>>max; min=max; for ( int i=1;i<m;++i) { cin>>temp; if (max<temp)max=temp; if (min>temp)min=temp; } printf ( "%d" ,max-min); return 0; } |
07:奥运奖牌计数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<iostream> using namespace std; int main() { int m; int j,y,t; int a=0,b=0,c=0; cin>>m; for ( int i=0;i<m;++i) { cin>>j>>y>>t; a+=j; b+=y; c+=t; } printf ( "%d %d %d %d" ,a,b,c,a+b+c); return 0; } |
08:多边形内角和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<iostream> using namespace std; int main() { int n; int j,v; int a=0,b=0,c=0; cin>>n; v=(n-2)*180; for ( int i=0;i<n-1;++i) { cin>>j; v-=j; } printf ( "%d" ,v); return 0; } |
09:奇数求和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> using namespace std; int main() { int n,m,sum=0; cin>>m>>n; for ( int i=m;i<=n;++i) { if (i%2) { sum+=i; } } cout<<sum; return 0; } |
10:满足条件的数累加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> using namespace std; int main() { int n,m,sum=0; cin>>m>>n; for ( int i=m;i<=n;++i) { if (!(i%17)) { sum+=i; } } cout<<sum; return 0; } |
11:整数的个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> using namespace std; int main() { int k,temp,one=0,five=0,ten=0; cin>>k; for ( int i=0;i<k;++i) { cin>>temp; if (temp==1)++one; if (temp==5)++five; if (temp==10)++ten; } cout<<one<<endl<<five<<endl<<ten<<endl; return 0; } |
12:与指定数字相同的数的个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<iostream> using namespace std; int main() { int n,m,temp,one=0; cin>>n>>m; for ( int i=0;i<n;++i) { cin>>temp; if (temp==m)++one; } cout<<one; return 0; } |
13:乘方计算
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include<iostream> using namespace std; int main() { long long a,n,temp=1; cin>>a>>n; for ( int i=0;i<n;++i) { temp*=a; } cout<<temp; return 0; } |
14:人口增长问题
1 2 3 4 5 6 7 8 9 10 11 | #include<iostream> #include<cmath> using namespace std; int main() { double x,n,sum=0; cin>>x>>n; sum=x* pow (1+0.1/100.0,n); printf ( "%.4f" ,sum); return 0; } |
15:银行利息
1 2 3 4 5 6 7 8 9 10 11 12 | #include<iostream> #include<cmath> using namespace std; int main() { double r,m,y,sum; cin>>r>>m>>y; r=r/100.0; sum=m* pow (1+r,y); printf ( "%d" ,( int )sum); return 0; } |
16:买房子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include<iostream> #include<cmath> using namespace std; int main() { double n,k,sum=200,cxy; cin>>n>>k; k/=100.0; cxy=n; int count=1,flag=0; while (cxy<sum) { sum*=(1+k); cxy+=n; ++count; if (count>20) { flag=1; break ; } } if (flag) { cout<< "Impossible" ; } else { printf ( "%d" ,count); } return 0; } |
17:菲波那契数列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> #include<cmath> using namespace std; int main() { int k; int o[50]={}; o[1]=1,o[2]=1; cin>>k; for ( int i=3;i<=k;++i) { o[i]=o[i-1]+o[i-2]; } cout<<o[k]; return 0; } |
18:鸡尾酒疗法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include<iostream> #include<cmath> using namespace std; int main() { int n;cin>>n; double a,b; double x,y; cin>>a>>b; x=b/a; for ( int i=1;i<n-1;++i) { cin>>a>>b; y=b/a; if ((y-x)>0.05) { cout<< "better" ; } else if ((x-y)>0.05) { cout<< "worse" ; } else { cout<< "same" ; } cout<<endl; } cin>>a>>b; y=b/a; if ((y-x)>0.05) { cout<< "better" ; } else if ((x-y)>0.05) { cout<< "worse" ; } else { cout<< "same" ; } return 0; } |
19:救援
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<iostream> #include<cmath> using namespace std; int main() { int n;cin>>n; double x,y,z; double sum=0; for ( int i=0;i<n;++i) { cin>>x>>y>>z; sum+= sqrt (x*x+y*y)/50.0; sum+= sqrt (x*x+y*y)/50.0; sum+=z*1.5; } printf ( "%.0f" ,sum+0.5); return 0; } |
20:球弹跳高度的计算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include<iostream> #include<cmath> using namespace std; int main() { double h,ten; double high=0;cin>>h; ten=h* pow (0.5,10); high=h; for ( int i=1;i<10;++i) { h/=2.0; high+=h; high+=h; } printf ( "%g\n" ,high); printf ( "%g" ,ten+0.00000005); return 0; } |
21:角谷猜想
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include<iostream> #include<cmath> #include <limits> using namespace std; int main() { long long n; cin>>n; while (1) { if (n==1) break ; if (n%2) { cout<<n<< "*3+1=" ; n=n*3+1; } else { cout<<n<< "/2=" ; n=n/2; } cout<<n<<endl; } cout<< "End" ; // cout << "最大值:" << (numeric_limits<long long>::max)(); return 0; } /* 这题要B一下,一开始用int老是超时改成long long过了 int 最大:2,47,483,648>2,000,000,但是一旦*3就超了 long long最大:92,23,372,036,854,775,807 */ |
逛了一波博客回来继续撸
22:津津的储蓄计划
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include<iostream> #include<cmath> using namespace std; int main() { int temp,flag=0; double save=0; int jj=0; for ( int i=1;i<=12;++i) { cin>>temp; jj+=300; jj-=temp; if (jj>=300) { save+=300; jj-=300; } else if (jj>=200) { save+=200; jj-=200; } else if (jj>=100) { save+=100; jj-=100; } else if (jj<0) { flag=1; cout<<-1*i; break ; } } if (!flag) { printf ( "%.0f" ,save*1.2+jj); } return 0; } |
23:药房管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include<iostream> #include<cmath> using namespace std; int main() { int m,n,num,c=0; cin>>m>>n; for ( int i=0;i<n;++i) { cin>>num; if (m>=num) { m-=num; } else { ++c; } } cout<<c; return 0; } |
24:正常血压
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include<iostream> #include<cmath> using namespace std; int main() { int n,a,b,c=0,max=0; cin>>n; for ( int i=0;i<n;++i) { cin>>a>>b; if (90<=a&&a<=140&&60<=b&&b<=90) { ++c; } else { c=0; } if (max<c)max=c; } cout<<max; return 0; } |
25:求特殊自然数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 找答案: #include<iostream> #include<cmath> using namespace std; int main() { for ( int i=0;i<7;++i) { for ( int j=0;j<7;++j) { for ( int k=0;k<7;++k) { if ((49*i+7*j+k)==(81*k+9*j+i)) { cout<<(49*i+7*j+k)<<endl; cout<<i<<j<<k<<endl; cout<<k<<j<<i<<endl; cout<<endl; } } } } return 0; } 交的时候: #include<iostream> #include<cmath> using namespace std; int main() { cout<<248<<endl; cout<<503<<endl; cout<<305<<endl; return 0; } |
26:统计满足条件的4位数个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<iostream> #include<string> using namespace std; int main() { int n,c=0;cin>>n; string a; for ( int i=0;i<n;++i) { cin>>a; if ((a[3]- '0' )-(a[2]- '0' )-(a[1]- '0' )-(a[0]- '0' )>0)++c; } cout<<c; return 0; } |
27:级数求和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> #include<string> using namespace std; int main() { double i; double k; cin>>k; double sn=0; for (i=1;!(sn>k);++i) { sn+=1/i; } cout<<i-1; return 0; } |
28:分离整数的各个数位
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<iostream> #include<string> using namespace std; int main() { long long n; cin>>n; while (n) { cout<<n%10<< " " ; n/=10; } return 0; } |
29:数字反转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> #include<string> using namespace std; int main() { long long n,ne=0; cin>>n; while (n) { ne+=n%10; ne*=10; n/=10; } cout<<ne/10; return 0; } |
30:含k个3的数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include<iostream> #include<string> using namespace std; int main() { int m,k,s,t=0,flag=0; cin>>m>>k; s=m%19; if (!s) { s=m; while (s) { if (s%10==3)++t; s/=10; } if (t==k)flag=1; } if (flag) { cout<< "YES" ; } else { cout<< "NO" ; } return 0; } |
31:开关灯
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include<iostream> #include<string> using namespace std; int main() { int a[5001]={},n,m; cin>>n>>m; //0关闭,1打开 for ( int j=1;2*j<=n;++j) { a[2*j]=1; //打开 } for ( int i=3;i<=m;++i) { for ( int j=1;i*j<=n;++j) { if (a[i*j]==1) { a[i*j]=0; } else { a[i*j]=1; } } } int ii=1; for (;ii<=n;++ii) { if (!a[ii]) { cout<<ii; break ; } } ++ii; for (;ii<=n;++ii) { if (!a[ii]) { cout<< "," <<ii; } } return 0; } |
32:求分数序列和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<iostream> #include<string> using namespace std; int main() { int n;cin>>n; double t,a=2,b=1,sum=2; for ( int i=1;i<n;++i) { t=a; a=a+b; b=t; sum+=a/b; } printf ( "%.4f" ,sum); return 0; } |
今天就到这儿,留了点尾巴后天再收
33:计算分数加减表达式的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include<iostream> #include<string> using namespace std; int main() { int n;cin>>n; double sum=0; for ( int i=1;i<=n;++i) { if (i%2) { sum+=1.0/i; } else { sum+=-1.0/i; } } printf ( "%.4f" ,sum); return 0; } |
34:求阶乘的和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<iostream> #include<string> using namespace std; int main() { int n;cin>>n; int t=1,sum=0; for ( int i=1;i<=n;++i) { t*=i; sum+=t; } printf ( "%d" ,sum); return 0; } |
35:求出e的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<iostream> #include<string> using namespace std; int main() { int n;cin>>n; double t=1,sum=1; for ( int i=1;i<=n;++i) { t*=i; sum+=1.0/t; } printf ( "%.10f" ,sum); return 0; } |
36:计算多项式的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> #include<cmath> using namespace std; int main() { int n; float x; cin>>x>>n; float t=1,sum=1; for ( int i=1;i<=n;++i) { sum+= pow (x,i); } printf ( "%.2f" ,sum); return 0; } |
37: 雇佣兵
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include<iostream> #include<cmath> using namespace std; int main() { int n,m,x; int a=0,b=0,c=0; cin>>m>>n>>x; b=n; for ( int i=x-1;i>=0&&m>=b;--i) { a+=b; // cout<<">>体力="<<a<<"战斗力="<<b<<"能量元素="<<i<<endl; if (a>=m) { b+=m/b; a=0; } // cout<<"<<体力="<<a<<"战斗力="<<b<<"能量元素="<<i<<endl; } cout<<b; return 0; } |
38:计算多项式的导函数【这题气死我了,他没有外循环,给的是三个不同情况下的样例!!!】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include<iostream> #include<cmath> using namespace std; int main() { int n,m,x,j; int a=0,b=0,c=0; // cin>>n; // for(int i=0;i<n;++i) // { cin>>m; //输入最高次 if (m==0) { cin>>x; cout<<0<<endl; } else { for (j=m;j>0;--j) { cin>>x; if (j==m) { cout<<x*m; } else { cout<< " " <<x*j; } } cin>>x; cout<<endl; } // } return 0; } |
39: 与7无关的数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include<iostream> #include<cmath> using namespace std; bool function( int i) { while (i) { if (i%10==7) { return true ; } i/=10; } return false ; } int main() { int n,x; long long sum=0; cin>>n; for ( int i=1;i<=n;++i) { if (!(i%7==0||function(i))) { sum+=i*i; } } cout<<sum; return 0; } |
40:数1的个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<iostream> #include<cmath> using namespace std; void function( int i, int &count) { while (i) { if (i%10==1) { ++count; } i/=10; } } int main() { int n,x; int sum=0; cin>>n; for ( int i=1;i<=n;++i) { function(i,sum); } cout<<sum; return 0; } |
41:数字统计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<iostream> #include<cmath> using namespace std; void function( int i, int &count) { while (i) { if (i%10==2) { ++count; } i/=10; } } int main() { int L,R; int sum=0; cin>>L>>R; for ( int i=L;i<=R;++i) { function(i,sum); } cout<<sum; return 0; } |
42:画矩形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include<iostream> #include<cmath> using namespace std; int main() { int w,h;cin>>h>>w; char tag;cin>>tag; int c;cin>>c; if (c) { for ( int i=0;i<h;++i) { for ( int ii=0;ii<w;++ii) { cout<<tag; } cout<<endl; } } else { if (w==1&&h==1) { cout<<tag; } else { for ( int i=0;i<w;++i)cout<<tag; cout<<endl; for ( int i=1;i<h-1;++i) { cout<<tag; for ( int ii=1;ii<w-1;++ii) { cout<< " " ; } cout<<tag<<endl; } for ( int ii=0;ii<w;++ii) { cout<<tag; } } } return 0; } |
43:质因数分解
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include<bits/stdc++.h> using namespace std; int main() { int a;cin>>a; for ( int i=2; i<= sqrt (a); i++) { if (a%i==0) { cout<<a/i; } } } |
44:第n小的质数【纯属套模板】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include<bits/stdc++.h> using namespace std; int a[10000000],i,j,p[10001],t=1,n; int main() { cin>>n; a[1]=1; for (i=2;i<=1000000;i++) { if (!a[i]) { p[t]=i; if (t==n) { cout<<p[n]; return 0; } t++; } for (j=1;j<=t&&i*p[j]<=1000000;j++) { a[i*p[j]]=1; if (i%p[j]==0) break ; } } return 0; } |
45:金币【找规律的题】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<iostream> #include<cmath> #define MAX 10000 using namespace std; int main() { int n,day=0,sum=0;cin>>n; for ( int j=1;day<=n;++j) { day+=j; // cout<<"day:"<<day-j<<"+"<<j<<"="<<day<<endl; if (n>=day) { sum+=j*j; } else { sum+=j*(n-day+j); } } cout<<sum; return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异