循环语句——例题

AcWing 708. 偶数

#include <iostream>

using namespace std;

int main()
{
    for(int i = 1; i <= 100; i++)
    {
        if((i&1) == 0)   //位运算  。偶数和1   &位元算为0
        {
            cout<<i<<endl;
        }
    }
    return 0;
}

AcWing 709. 奇数

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        if((i&1) != 0)   //位运算  。偶数和1   &位元算为0
        {
            cout<<i<<endl;
        }
    }
    return 0;
}

AcWing 712. 正数

#include <bits/stdc++.h>
using namespace std;
int main(){
    int x = 0;
    for(int i = 1;i <= 6;i++){
        double t;
        cin >> t;
        if(t > 0) x++;
    }
    cout << x << " positive numbers";
    return 0;
}

 AcWing 714. 连续奇数的和 1

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

int main()
{
    int x,y;
    int ans=0;
    cin>>x>>y;
    if(x>y) swap(x,y);
    for(int i=x+1;i<y;i++){
        if(i%2) ans+=i;
    }
    printf("%d\n",ans);
    return 0;
}

AcWing 716. 最大数和它的位置

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
int a[111];
int b[111];
const int N = 100;
int main()
{
    for (int i = 0; i < N; i ++ ){
        cin >> a[i];
        b[i] = a[i];
    }
    sort(a,a+N);
    for (int i = 0; i < N; i ++ ){
        if(b[i] == a[N-1])
        printf("%d\n%d",a[N-1],i+1);
    }


    return 0;
}

AcWing 721. 递增序列 

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    while(cin >> n && n!=0){
    for(int i = 1; i <= n; i++){
        printf("%d ",i);
    }
    printf("\n");
    }

    return 0;
}

 AcWing 720. 连续整数相加

#include <bits/stdc++.h>
using namespace std;
int main(){
    int a, n, t = 0;
    cin >> a;
    while(cin >> n){
        if(n > 0){
            for(int i = 1;i <= n;i++){
                t += a;
                a ++;
            }
        }
    }
    cout << t;
    return 0;
}

 AcWing 724. 约数

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
int i,a[1000];
int main()
{
    int n;
    cin >> n;
    for (i = 1; i <= n; i ++ ){
        if(n % i == 0) printf("%d\n",i);
    }

    return 0;
}

 AcWing 723. PUM

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int a,b,c = 1;
    cin >> a >> b;
    for(int i = 0; i < a; i++){
        for(int j = 0; j < b - 1; j++){
            printf("%d ",c); c++;
        }
        c++;
        printf("PUM\n");
    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永夜天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值