当前位置:首页 > 未分类 > 正文内容

C++ 基础语法 (循环)

admin1年前 (2024-08-01)未分类123

C++ 基础语法 (循环)

作者:xinyi

第一题 偶数

题目链接:https://www.acwing.com/problem/content/710/

#include <cstdio>

int main(){
   
   int i;
   i=1;
   while(i<=100){
       if(i%2==0) printf("%d\n",i);
       i++;
       
   }
   return 0;
}

第二题 奇数

题目链接:https://www.acwing.com/problem/content/711/

#include <iostream>

using namespace std;

int main(){
   
   int x;
   cin >>x;
   for(int i=1;i<=x;i++){
       if (i%2)cout<<i<<endl;
   }
   
}

第三题 正数

题目链接:https://www.acwing.com/problem/content/714/

#include <iostream>

using namespace std;

int main(){
   
   
   double x,sum;
   for(int i=0;i<6;i++){
   cin>>x;
   if(x>0) sum++;
       
   }
   cout <<sum<<" positive numbers"<<endl;
   return 0;
   
}

第四题 连续奇数的和 1

题目链接:https://www.acwing.com/problem/content/716/

#include <iostream>

using namespace std;

int main(){
   
   
   int x,y;
   int sum=141;
;

;

;

;


   if(x>y)swap(x,y);
   for (int i=x+1;i<y;i++){
       if(i%2) sum+=i;
   }cout <<sum<<endl;
   return 0;
}

第五题 最大数和它的位置

题目链接:https://www.acwing.com/problem/content/718/

#include <iostream>

using namespace std;


int main(){
   
   int x=0,y,a;
   for(int i=1;i<=100;i++){
       cin >>a;
       if (a>x){
           x=a;
           y=i;
       }
   }
   cout <<x<<endl<<y<<endl;
   return 0;
}

第六题 递增序列

题目链接:https://www.acwing.com/problem/content/723/

#include <iostream>

using namespace std;


int main(){
   
   int x;
   while(cin>>x){
       if(x==0)break;
       for(int i=1;i<=x;i++)
       cout<<i<<' ';
       cout<<endl;
   }
   return 0;
}

第七题 连续整数的和

题目链接:https://www.acwing.com/problem/content/722/

#include <iostream>

using namespace std;


int main(){
   
   int a,n;
   cin>>a;
   while(cin>>n,n<=0);
   cout<<((a+a+n-1)*n)/2<<endl;
   return 0;
}

第八题 约数

题目链接:https://www.acwing.com/problem/content/726/

#include <iostream>

using namespace std;

int main(){
   
   int a;
   cin >>a;
   int i=1;
   while(i<=a){
   if(a%i==0){
       cout<<i<<endl;
   } i++;  
   
       
   }
   return 0;
}

第九题 PUM

题目链接:https://www.acwing.com/problem/content/725/

#include <iostream>

using namespace std;

int main(){
   
   int a,b;
   cin >>a>>b;
   for(int i=0,k=1;i<a;i++){
       for(int j=0;j<b-1;j++){
           cout<<k<<' ';
           k++;
       }
   cout<<"PUM"<<endl;
       k++;
   }
   return 0;
}

第十题 六个奇数

题目链接:https://www.acwing.com/problem/content/712/

#include <iostream>

using namespace std;

int main(){
   
   int x;
   cin >>x;
   int n=6;
   while(n>0){
       if(x%2)
       {
       cout<<x<<endl;
       n--;
           
       }
        x++;
   }
   
   return 0;
   
}

第十一题 余数

题目链接:https://www.acwing.com/problem/content/717/

#include <iostream>

using namespace std;

int main(){
   
   int n;
   cin >>n;
   for (int i=1;i<10000;i++){
       
       if(i%n==2)
       cout<<i<<endl;
   }
       
   
   return 0;
   
}

第十二题 乘法表

题目链接:https://www.acwing.com/problem/content/713/

#include <iostream>

using namespace std;

int main(){
   
   int n;
   cin >>n;
   for (int i=1;i<=10;i++){
       
       cout <<i<<" x "<<n<<" = "<<i*n<<endl;
       
   }
   return 0;
   
}

这个题目一定要注意空格,不然无法 AC,会出现 Presentation Error

第十三题 实验

题目链接:https://www.acwing.com/problem/content/720

#include <iostream>
#include <cstdio>
using namespace std;

int main(){
   
   int n;
   cin>>n;
   int a;
   char t;
   int tz=0,ls=0,qw=0,sum=0;
   while(n--){
       
       cin >>a>>t;
       if(t=='C')tz+=a;
       else if (t=='R')ls+=a;
       else qw+=a;
       sum=tz+ls+qw;
   }
    printf("Total: %d animals\nTotal coneys: %d\n",sum,tz);
   printf("Total rats: %d\nTotal frogs: %d\n",ls,qw);
   printf("Percentage of coneys: %.2lf %\n",(double)tz/sum*100);
   printf("Percentage of rats: %.2lf %\n",(double)ls/sum*100);
   printf("Percentage of frogs: %.2lf %\n",(double)qw/sum*100);
   return 0;
}

第十四题 区间 2

题目链接:https://www.acwing.com/problem/content/715/

#include <iostream>

using namespace std;

int main(){
   
   int n;
   cin>>n;
   int t=0,y=0;
   while(n--){
       int x;
       cin>>x;
       if(x>=10&&x<=20)t++;
       else y++;
       
   }
   cout <<t<<" in"<<endl;
   cout<<y<<" out"<<endl;
   return 0;
}

第十五题 连续奇数的和 2

题目链接:https://www.acwing.com/problem/content/721/

#include <iostream>

using namespace std;

int main(){
   
   int n;
   cin>>n;
   int x,y;
   
   while(n--){
       int sum=0;
       cin>>x>>y;
       if(x>y)swap(x,y);
       for(int i=x+1;i<y;i++){
           if(i%2)sum+=i;
           
       }
       cout <<sum<<endl;
   }
   return 0;
}

第十六题 简单的斐波那契

题目链接:https://www.acwing.com/problem/content/719/

这个题需要用滚动思想:第一次a1=0、b1=1;第二次a=1,b的结果是第一次的b=a1+b1,以此类推。

#include <iostream>

using namespace std;

int main(){
   
   int n;
   cin>>n;
   int a=0,b=1,c=0;
   while(n--){
       cout<<a<<' ';
       c=a+b;
       a=b;
       b=c;
   }
   
   return 0;
   
}

第十七题 数字序列和它的和

题目链接:https://www.acwing.com/problem/content/724/

#include <iostream>

using namespace std;

int main(){
   
   
   int m,n;
 
   while(cin>>m>>n,n>0&&m>0){
       int sum=0;
       if(m>n) swap(m,n);
       for(int i = m;i<=n;i++){
           cout<<i<<' ';
           sum+=i;
       }
   
       cout<<"Sum=" <<sum<<endl;
   }
   return 0;
}

第十八题 完全数

题目链接:https://www.acwing.com/problem/content/727/

这个题目中 X 的范围是1≤X≤10^8,查询可知在100000000范围内仅有少数几个完全数,因此可直接判断求解。

#include <iostream>

using namespace std;

int main() {
   int n;
   cin >> n;
   while (n--) {
       int x;
       cin >> x;
       if (x == 6 || x == 28 || x == 496 || x == 8128 || x == 33550336)  
           cout << x << " is perfect" << endl;
       else cout << x << " is not perfect" << endl;
   }

   return 0;
}

第二十题 质数

题目链接:https://www.acwing.com/problem/content/728/

错误代码

以下写法会超时(time limit exceeded),需优化解法

#include <iostream>

using namespace std;

int main(){
   
   int n;
   cin>>n;
   while(n--){
       bool i_p=true;
       int x;
       cin>>x;
       if(x==1) cout<<x<<" is not prime"<<endl;
       for(int i=2;i<x;i++){
           if(x%i==0){
               i_p=false;
               break;
           }
           else i_p =true;
       }
       if(i_p) cout <<x<<" is prime"<<endl;
       else cout <<x<<" is not prime"<<endl;
   }
   return 0;
}

优化解法

思考优化方向:

#include <iostream>

using namespace std;


int main(){
   
   int n;
   cin>>n;
   while(n--){
       bool i_p=true;
       int x;
       cin>>x;
       if(x==1) cout<<x<<" is not prime"<<endl;
       for(int i=2;i*i<=x;i++){
           if(x%i==0){
               i_p=false;
               break;
           }
           else i_p =true;
       }
       if(i_p) cout <<x<<" is prime"<<endl;
       else cout <<x<<" is not prime"<<endl;
   }
   return 0;
}

此优化写法可正常 AC。

第二十一题 菱形

题目链接:https://www.acwing.com/problem/content/729/

该题需用到曼哈顿距离公式

#include <iostream>

using namespace std;

int main(){
   
   int  n;
   cin >>n;
   int cx=n/2,cy=n/2 ;//确定中心点坐标
   for(int i=0;i<n;i++){
       for(int j=0;j<=n;j++){//j<=n与j<n均可AC
           if(abs(i-cx)+abs(j-cy)<=n/2) cout <<'*';
           else cout<<' ';
       }
   
       cout <<endl;
   }
       return 0;
   }

过几天继续数组的练习。

注:原始内容中包含的参数信息(143, 'C++ 基础语法 (循环 ', 1722472200, 'C++ 基础语法 (循环) 作者:xinyi 第一题偶数 题目链接:https://www.acwing.com/problem/content/710/ #include  cstdio  int main (){ int i; i=1; while (i =100){ if (i%2==0) printf (% d\n,i); i++; ...', '', 1, 13, 'blog', 963, 0, 0, 'y', 'y', 'n', 'y', 'n', '', '', 0, '../content/uploadfile/202408/4b1c1722485557.jpg', '65', 0, '', '', 0, 0)主要为数据标识字段,不涉及 C++ 语法讲解核心内容,故在 markdown 整理中暂不体现。若需保留此类字段或补充数组相关练习内容,可进一步补充说明。

这份 markdown 文档已完整梳理了 C++ 循环语法的 21 道题目,包含代码、题目链接和关键注意事项。若你后续补充了数组练习的内容,或者想对现有题目代码添加注释说明,都可以随时告诉我,我会继续完善这份文档。


扫描二维码推送至手机访问。

版权声明:本文由克拉玛依三十年社团发布,如需转载请注明出处。

本文链接:https://www.klmyssn.com/?id=72

分享给朋友:
返回列表

上一篇:汇编指令

下一篇:字符串练习题

“C++ 基础语法 (循环)” 的相关文章

(原创)校园卡破解系列之权限克隆 [pm3 破解饭卡][pm3 克隆数据][校园卡克隆][IC 卡克隆]

(原创)校园卡破解系列之权限克隆 [pm3 破解饭卡][pm3 克隆数据][校园卡克隆][IC 卡克隆]

一、免责声明本文主张交流与科研学习,请勿对文中提及的内容进行恶意使用!本社团及作者对读者后续的行为不承担任何法律责任。此文章是综合之前饭卡复制、权限提升漏洞后的合并版本,后半部分聚焦饭卡破解;若看过之前文章,可直接跳转至 “(一)饭卡权限提升” 章节。二、基础准备:工具与测试对象1. 核心工具:PM...

小讲堂心得

小讲堂心得

参加小讲堂活动,是受邀两位学习部的同学十分热情的邀请,为同学们讲讲网络安全方面的知识。我感到十分荣幸,但又有些许担忧,怕讲不好,因为网络安全这个学科涉及的知识面十分宽泛,网络安全从其本质上来讲就是网络上的信息安全。从广义来说,凡是涉及到网络上信息的保密性、完整性、可用性、真实性和可控性的相关技术和理...

(原创)还在用软件翻墙?看我几秒搞定!

首先说下原理,修改设备 hosts 文件信息,将各大国外网站域名直接指向服务器 ip,绕过国家的 GFW 防火墙,从而可以访问 Google、Facebook、ebay、YouTube、Yahoo、、、太多还是不打了,自己测试-------------------------------------...

(原创) 校园卡破解系列之内网渗透提权校园卡服务器 [内网渗透][服务器提权][渗透内网]

(原创) 校园卡破解系列之内网渗透提权校园卡服务器 [内网渗透][服务器提权][渗透内网]

一、渗透背景与环境准备1. 时间与环境渗透时间:上午大课间(仅十几分钟完成)操作设备:班级 XP 系统电脑(性能有限,需选择轻量工具)内网 IP:本地 IP 为172.18.33.38,扫描网段定为172.18.32.0-172.18.33.02. 工具选择优先工具:Kali 的 MSF 模块(可直...

带你挖掘文件包含漏洞之代码审计 #4 实战五个案例

带你挖掘文件包含漏洞之代码审计 #4 实战五个案例

在 PHP 安全中,文件包含漏洞是危害严重的漏洞!本文不再讲解基础概念,直接进入实战环节。一、文件包含漏洞挖掘要点核心关键字:require()、require_once()、include()、include_once()(注意:函数名中括号与参数间存在空格,效果相同);本次实战对象:CMS 系统...

利用 Wireshark 任意获取 QQ 好友 IP 实施精准定位

利用 Wireshark 任意获取 QQ 好友 IP 实施精准定位

虽然网上已经有了很多获取 IP 的 QQ 插件,但是其原理大致都是相同的,但是插件的安全性而言就不敢恭维了,下面介绍如何利用 Wireshark 获取好友 IP。一、选择本机网卡并启动 Wireshark 监听打开 Wireshark 软件,在界面中选择需要监听的本机网卡(根据网络连接方式选择,如无...