C++ 基础速通【数组】Ac-Wing

数组替换

在这里插入图片描述

#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    for(int i = 0; i < 10; i ++ ){
        int x;  cin >> x;
        printf("X[%d] = %d\n", i, x <= 0 ? 1 : x);
    }
    return 0;
}

数组中的行

在这里插入图片描述

#include <iostream>

using namespace std;

int main()
{
    int l;
    char op;
    cin >> l >> op;
    double s=0;
    for(int i=0;i<12;i++)
    {
        for(int j=0;j<12;j++)
        {
            double a;
            cin >> a;
            if(i==l) s+=a;
        }
    }

    printf("%.1lf",op=='S' ? s : s/12);
}

数组的右上半部分

在这里插入图片描述

#include <iostream>
using namespace std;
double m, s;//m用来读入,s用来计算和
char op;
int main()
{
    cin >> op;
    for (int i = 0; i < 12; i ++)
        for (int j = 0; j < 12; j ++)
        {
            cin >> m;//重复读入m
            if (i < j)
                s += m;
        }
    if (op == 'S')
        printf("%.1lf", s);
    else 
        printf("%.1lf", s / 66);
    return 0;
}

数组的左上半部分

在这里插入图片描述

#include <iostream>
using namespace std;
double m, s;//m用来读入,s用来计算和
char op;
int main()
{
    cin >> op;
    for (int i = 0; i < 12; i ++)
        for (int j = 0; j < 12; j ++)
        {
            cin >> m;//重复读入m
            if (i+j<11)
                s += m;
        }
    if (op == 'S')
        printf("%.1lf", s);
    else 
        printf("%.1lf", s / 66);
    return 0;
}

数组的上方区域

在这里插入图片描述

#include <iostream>
using namespace std;
double m, s;//m用来读入,s用来计算和
char op;
int main()
{
    cin >> op;
    for (int i = 0; i < 12; i ++)
        for (int j = 0; j < 12; j ++)
        {
            cin >> m;//重复读入m
            if (i<j&&i+j<11)
                s += m;
        }
    if (op == 'S')
        printf("%.1lf", s);
    else 
        printf("%.1lf", s / 30);
    return 0;
}

数组的左方区域

在这里插入图片描述

#include <iostream>
using namespace std;
double m, s;//m用来读入,s用来计算和
char op;
int main()
{
    cin >> op;
    for (int i = 0; i < 12; i ++)
        for (int j = 0; j < 12; j ++)
        {
            cin >> m;//重复读入m
            if (i>j&&i+j<11)
                s += m;
        }
    if (op == 'S')
        printf("%.1lf", s);
    else 
        printf("%.1lf", s / 30);
    return 0;
}

平方矩阵 I

在这里插入图片描述
在这里插入图片描述

  • 作者:小张同学
    链接:https://www.acwing.com/solution/content/9554/
    来源:AcWing
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

平方矩阵 II

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;
const int N = 110;
int a[N][N];

int main()
{
    int n;
    cin >> n;
    while (n)
    {
        for (int i = 0; i < n; i++)
        {
            a[i][0] = i + 1;//第一行
            a[0][i] = i + 1;//第一列
        }
        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < n; j++)
            {
                a[i][j] = a[i - 1][j - 1];//其他位置,a[i][j] = a[i - 1][j - 1]
            }
        }
        for (int i = 0; i < n; i++)//输出矩阵
        {
            for (int j = 0; j < n; j++)
            {
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
        cin >> n;
    }
}

作者:Hasity
链接:https://www.acwing.com/solution/content/31030/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

平方矩阵 III

在这里插入图片描述

#include <iostream>

using namespace std;

int main()
{
    int n;
    while(cin >> n,n)
    {
        for(int i = 0; i < n; i ++)
        {
            for(int j = 0; j < n; j ++)
                cout << (1 << i) * (1 << j) << ' ';//两个乘数 后者控制基数 1 ~ 2^(n-1) ,前者控制倍数
            cout << endl;
        }
        cout << endl;
    }
    return 0;
}

作者:一只草莓
链接:https://www.acwing.com/solution/content/7673/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

蛇形矩阵

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

const int N = 110;
int a[N][N];

int main()
{
    int r,c;
    cin >> r >> c;

    int left = 0, right = c - 1;
    int top = 0, bottom = r - 1;
    int k = 1;
    while(left <= right || top <= bottom)
    {
        for(int i = left; i <= right && top <= bottom; i++)//构造最上面一行
        {
            a[top][i] = k++;
        }
        top++;
        for(int i = top; i <= bottom && left <= right; i++)//构造最右侧一列
        {
            a[i][right] = k++;
        }
        right--;
        for(int i = right; i >= left && top <= bottom; i--)//构造最下面一行
        {
            a[bottom][i] = k++;
        }
        bottom--;
        for(int i = bottom; i >= top && left <= right; i--)//构造最左侧一列
        {
            a[i][left] = k++;
        }
        left++;
    }
    for(int i = 0; i < r; i++)
    {
        for(int j = 0; j < c; j++) cout<< a[i][j] << " ";
        cout << endl;
    }
    return 0;
}


作者:Hasity
链接:https://www.acwing.com/solution/content/28991/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

宇宙超级无敌霸王龙捏

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

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

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

打赏作者

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

抵扣说明:

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

余额充值