acwing刷题指南2.2

循环语句

708.偶数

题意:编写一个程序,输出 11 到 100100 之间(包括 11 和 100100)的全部偶数。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    for(int i = 2;i<=101;i+=2)
    {
        cout<<i<<endl;
    }
    return 0;
}

709.奇数

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x;
    cin>>x;
    
    for(int i = 1;i<=x;i++)
    {
        if(i%2!=0)  cout<<i<<endl;
       
    }
    return 0;
}

712.正数

#include<bits/stdc++.h>
using namespace std;
int main()
{
   int a = 0,x = 6;
   while(x --)
   {
       double b;
       cin>>b;
       if(b > 0) a++;
   }
       printf("%d positive numbers",a);
   
    return 0;
}

714.连续奇数的和

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x,y;
    cin>>x>>y;
    int n = 0;
    if(x > y) swap(x,y);
    for(int i = x + 1;i < y;i++)
    {
        if(i%2 != 0) n+=i;
    }
    printf("%d\n",n);
    return 0;
}

716. 最大数和它的位置

题意:给定 100 个整数,请你找出其中最大的数字,以及它的输入位置(位置从 1 开始)。

输出格式

第一行输出最大的数字。

第二行输出该数字的输入位置。

思路:遍历一遍整列数,设计一个计数器,记录每次交换的值,只要设计的另外一个变量>cnt,那么就交换他们俩的值,并且再设置一个变量y,存储数所在位置

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int cnt = 0,y,z;
    for(int i = 1;i<=100;i++)
    {
        cin>>z;
        if(z > cnt)
        {
            cnt = z;
            y = i;
        }
    }
    cout<<cnt<<endl;
    cout<<y<<endl;
}

721. 递增序列

#include<bits/stdc++.h>
using namespace std;
int main()
{
   int n;
   while(cin>>n,n)//逗号运算符,整个表达式的值是最后一个表达式的值。

//也可以写成 while(cin>>n && n) cin自带0和-1的判断,只需要一直读就好了,读到就执行下面操作

//scanf("%d",&n) != -1;或者~scanf("%d",&n);(学习位运算就会懂) scanf()自动有个判断-1的
   {
       for(int i = 1;i<=n;i++)
       cout<<i<<" ";
       cout<<endl;
   }
    return 0;
}

720.连续整数相加

解法1

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int A,N;
    cin>>A;
   while(cin>>N,N<=0)
   {
   }
   cout<<(A+A+N-1)*N/2;
    return 0;
}

解法2:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int A,N;
    cin>>A;
   while(~scanf("%d",&N))
   {
   }
   cout<<(A+A+N-1)*N/2;
    return 0;
}

解法三

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int A,N;
    cin>>A;
   while(~scanf("%d",&N));
  int s = 0;
  for(int i = 0;i<=N;i++)
     s += A+i;
    cout<<s;
    return 0;
}

AcWing 724. 约数

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int N;
    cin>>N;
    for(int i = 1;i<=N;i++)
    {
        if(N%i == 0) cout<<i<<endl;//直接看是否是这个数的约数
    }

return 0;
}

723.PUM(相当于报数游戏)双重循环

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i = 0,k = 1;i < n;i++)
    {
         for(int j = 0;j < m - 1;j++)
        {
            cout<<k<<" ";
            k++;
        }
        cout<<"PUM"<<endl;
        k++;//相当于跳过了这个PUM这个数
    }
    return 0;
}

715.余数

(判断是否除以N得余数为2即可)记得10000可不算在里面

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int N;
    cin>>N;
    for(int i = 2;i <= 9999;i++)
    {
        if(i%N == 2)
        cout<<i<<endl;
    }
    return 0;
}

710. 六个奇数

用偶数判断奇数!只要是偶数直接+1变成奇数,然后再输出六个奇数即可(遍历一边)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    if(n%2 == 0) n++;
    for(int i = 1;i<=6;i++)
    {
        cout<<n<<endl;
        n += 2;
    }
    
    return 0;
 } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值