头像

ACER01




离线:2天前


最近来访(3)
用户头像
77777777777
用户头像
四合院院士


ACER01
2天前

用了一个比较笨的总结数学规律的方法实现的,大致思路如下:
根据题目可知这是一个上下对称的图形,最中间的一行 * 的个数为输入的层数,整个图形可以理解为以输入层数为边长的正方形,空格和 * 分别占每一行的一个单位,每一行共n个单位,当输出的第x层为最中间时,的个数为n个,并且我们发现第一层为1个,第二层为3个,也就是随着层数,的个数为加2的等差数列。那么每一层两侧分别的空格数为n减去所在层数的数除以2,所在层数的数可以通过循环来建立等差数列进行计算。

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int n,num;  \\ n 为层数,num为最中间*的个数
    cin >> n;
    num = n;
    int m; \\ m为下班部分图形的代换层数
    if(n == 1) \\ 当n为1时不存在上下两个对称部分,所以单独列出
    {
        cout << "*" <<endl;
        return 0;
    }
    for(int i = 1 ; i < n; ) \\ 上半层 * 的个数来循环直到中间层
    {
        m = i; \\ 让m在最后获取到中间层上一层的*数
        for (int  j = 1; j <= (num - i) / 2; j++ ) \\ 计算每一行前半部分的空格数
        {
            cout << " ";
        }
        for (int  k = (num - i) / 2 + 1; k <= n - (num - i) /2; k++)  
        \\  从前半部分的空格数结束之后的后一个位置开始循环,直到*的最后一个位置结束
        \\ (num - i)/2为后半部分空格的数量,由n减去后半部分空格数量则为*的最后位置
        {
            cout<< "*";
        }
        for(int z = n - (num - i) / 2 + 1; z <= n; z ++)
        \\ 从*结束的后一个位置开始循环输出空格,直到空格和*的总数达到n
        {
            cout << " ";
        }
        cout << endl;
        i = i + 2;
    }
    for(int j = 0; j < n; j++) \\ 循环输出最中间层的*
    {
        cout << "*";
    }
    cout << endl;
    for(int i = m; i >= 0;) 
    \\ 因为下半部分图形是上半部分的对称,所以采取反方向递减来输出,让下半层第一层的点数从中间层上一层的点数开始递减
    {
        \\ 空格和*的计算输出方式和上半层一样,主要通过层数和*数来影响
        for (int  j = 1; j <= (num - i) / 2; j++ )
        {
            cout << " ";
        }
        for (int  k = (num - i) / 2 + 1; k <= n - (num - i) /2; k++)
        {
            cout<< "*";
        }
        for(int z = n - (num - i) / 2 + 1; z <= n; z ++)
        {
            cout << " ";
        }
        cout << endl;
        i = i - 2;  \\ 以每层减少2 * 来递减
    }

    return 0;
}

第一次写题解,写的有点儿乱,见谅。




活动打卡代码 AcWing 727. 菱形

ACER01
3天前
#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int n,num;
    cin >> n;
    num = n;
    int m;
    if(n == 1)
    {
        cout << "*" <<endl;
        return 0;
    }
    for(int i = 1 ; i < n; )
    {
        m = i;
        for (int  j = 1; j <= (num - i) / 2; j++ )
        {
            cout << " ";
        }
        for (int  k = (num - i) / 2 + 1; k <= n - (num - i) /2; k++)
        {
            cout<< "*";
        }
        for(int z = n - (num - i) / 2 + 1; z <= n; z ++)
        {
            cout << " ";
        }
        cout << endl;
        i = i + 2;
    }
    for(int j = 0; j < n; j++)
    {
        cout << "*";
    }
    cout << endl;
    for(int i = m; i >= 0;)
    {
        for (int  j = 1; j <= (num - i) / 2; j++ )
        {
            cout << " ";
        }
        for (int  k = (num - i) / 2 + 1; k <= n - (num - i) /2; k++)
        {
            cout<< "*";
        }
        for(int z = n - (num - i) / 2 + 1; z <= n; z ++)
        {
            cout << " ";
        }
        cout << endl;
        i = i - 2;
    }

    return 0;
}


活动打卡代码 AcWing 726. 质数

ACER01
3天前
#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int n;
    long long int x;
    int flag;
    long long int m;
    cin >> n;
    for (int i =0; i < n; i++)
    {
        m = 2;
        flag = 0;
        cin >> x;
        while(m * m <= x)
        {
            if (x % m == 0)
            {
                cout << x << " is not prime" << endl;
                flag = 1;
                break;
            }
            m++;
        }
        if (flag == 0)
        {
            cout << x << " is prime" << endl;
        }
    }
    return 0;
}


活动打卡代码 AcWing 725. 完全数

ACER01
3天前
#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int n;
    long long int  x;
    cin >> n;
    while(n)
    {
        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;
        }
        n--;
    }
    return 0;
}



ACER01
3天前
#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int n, m, sum;
    cin >> n >> m;
    while(n > 0 && m > 0)
    {
        sum = 0;
        if(m > n)
        {
            for(int i = n; i <= m; i++)
            {
                cout << i << " ";
                sum += i;

            }
            cout << "Sum=" << sum << endl;
        }
        else if(m < n)
        {
            for(int i = m; i <= n; i++)
            {
                cout << i << " ";
                sum += i;

            }
            cout << "Sum=" << sum << endl;
        }
        else
        {
            cout << n << " ";
            cout << "Sum=" << m << endl;
        }
        cin >> n >> m;
    }
    return 0;
}


活动打卡代码 AcWing 717. 简单斐波那契

ACER01
3天前
#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int n, pre, now;
    pre = 0;
    now = 1;
    cin >> n;
    if(n == 1)
    {
        cout << pre << endl;
    }
    else if(n == 2)
    {
        cout <<pre << " " << now << endl;
    }
    else
    {
        cout << pre << " " << now;
        for (int i = 0; i < n - 2; i++)
        {
            cout<< " " << pre + now;
            int temp = pre;
            pre = now;
            now = temp + now;
        }
        cout << endl;
    }

    return 0;
}



ACER01
3天前
#include<iostream>
#include<cstdio>

using namespace std;

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

    return 0;
}


活动打卡代码 AcWing 713. 区间 2

ACER01
3天前
#include<iostream>
#include<cstdio>

using namespace std;

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


活动打卡代码 AcWing 718. 实验

ACER01
3天前
#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int n, a;
    char t;
    int c = 0;
    int r = 0;
    int f = 0;
    double percent;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a >> t;
        if( t == 'C')
        {
            c += a;
        }
        else if (t == 'R')
        {
            r += a;
        }
        else if (t == 'F')
        {
            f += a;
        }
    }
    int sum = c + r + f;
    cout << "Total: " << sum << " animals " << endl;
    cout << "Total coneys: " << c << endl;
    cout << "Total rats: " << r << endl;
    cout << "Total frogs: " << f << endl;
    percent = c *1.0 / sum;
    printf("Percentage of coneys: %0.2lf %\n",percent * 100);
    percent = r *1.0 / sum;
    printf("Percentage of rats: %0.2lf %\n", percent * 100);
    percent = f *1.0 / sum;
    printf("Percentage of frogs: %0.2lf %\n", percent * 100);
    return 0;
}


活动打卡代码 AcWing 711. 乘法表

ACER01
3天前
#include<iostream>
#include<cstdio>

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;
}