天梯赛L1-001 ~ 010

文章详细列举了多个编程练习题目,包括HelloWorld、打印沙漏、个位数统计等,涉及C++语言,旨在提升编程技能,如蓝桥杯和天梯赛的准备。文章还分享了针对这些题目的解题思路和代码实现。

👂 White Lie - Jhameel - 单曲 - 网易云音乐

👂 丁丁猫儿 - 施鑫文月 - 单曲 - 网易云音乐

今年蓝桥 / 天梯都陪跑,希望明年,蓝桥杯省一(C++A组60分),天梯赛国三(180分)

目录

🌼一,L1-001 Hello World

🌼二,L1-002 打印沙漏

🌼三,L1-003 个位数统计

🌼四,L1-004 计算摄氏温度

🌼五,L1-005 考试座位号

🌼六,L1-006 连续因子  

🌼七,L1-007 念数字

🌼八,L1-008 求整数段和

🌼九,L1-009 N个数求和

🌼十,L1-010 比较大小

🥁总结


🌼一,L1-001 Hello World

题目详情 - L1-001 Hello World (pintia.cn)

满分5

#include<iostream>
using namespace std;
int main()
{
    cout<<"Hello World!";
    return 0;
}

🌼二,L1-002 打印沙漏

题目详情 - L1-002 打印沙漏 (pintia.cn)

满分20

 

满分20

1,先找规律,显然,也就是正数前缀和的基础上,+= i / 2,i是当前的奇数,比如7

就是28 + 7 / 2 = 28 + 3 = 31

2,有个,代码第15行,if(n >= a[i] && n < a[i + 2]),表示给定的数字最大满足 i 个*一行

外面for循环中,已经表示i += 2的奇数了,第一次没对应写成了a[i + 1]

debug二十分钟才找到问题

#include<iostream>
using namespace std;
int a[50]; //保存i颗*对应的字符数
int main()
{
    int n;
    char z;
    cin>>n>>z;
    for(int i = 1; i <= 50; i++)
        a[i] = i + a[i - 1];
    for(int i = 1; i <= 50; i +=2)
        a[i] += i / 2;
    int m;
    for(int i = 1; i <= 50; i += 2)
        if(n >= a[i] && n < a[i + 2]) { //这里是a[i + 2]而不是a[i + 1]
            m = i; //得到单行最大符号数
            break;
        }

    int mm = m, space = 0; //space当前空格数
    //倒三角
    while(mm > 0) {
        for(int i = 0; i < space; ++i)
            cout<<" "; //打印空格
        for(int i = 0; i < mm; ++i)
            cout<<z; //打印字符
        cout<<endl;
        space++;
        mm -= 2;
    }
    mm = 3;
    space = (m - mm) / 2;
    //正三角不算第一行
    while(mm <= m) {
        for(int i = 0; i < space; ++i)
            cout<<" ";
        for(int i = 0; i < mm; ++i)
            cout<<z;
        cout<<endl;
        space--;
        mm += 2;
    }
    cout<<n - a[m]; 

    return 0;
}

🌼三,L1-003 个位数统计

题目详情 - L1-003 个位数统计 (pintia.cn)

满分15

考察字符串转ASCII

#include<iostream>
using namespace std;
string s;
int a[20];

int main()
{
    cin>>s;
    for(int i = 0; i < s.size(); ++i)
        a[s[i] - '0']++;
    for(int i = 0; i <= 9; ++i)
        if(a[i] != 0) {
            cout<<i<<":"<<a[i]<<endl;
        }
    return 0;
}

🌼四,L1-004 计算摄氏温度

题目详情 - L1-004 计算摄氏温度 (pintia.cn)

满分5 

输出整数部分,int 满足

注意按要求输出

#include<iostream>
using namespace std;

int main()
{
    int f;
    cin>>f;
    cout<<"Celsius = "<<5 * (f - 32) / 9;
    return 0;
}

🌼五,L1-005 考试座位号

题目详情 - L1-005 考试座位号 (pintia.cn)

满分15 

三个变量联系起来,需要结构体

关于根据试机号查询其他两个量,一开始我想用二分,后来发现直接排序就能输出了

#include<iostream>
#include<algorithm> //sort()
using namespace std;
struct node
{
    long long num, id1, id2;
}a[1010];
bool cmp(node x, node y)
{
    return x.id1 < y.id1;
}
int main()
{
    int n, q, t;
    cin>>n;
    for(int i = 1; i <= n; ++i)
        cin>>a[i].num>>a[i].id1>>a[i].id2;
    sort(a + 1, a + n + 1, cmp); //按试机号排序
    cin>>q;
    for(int i = 0; i < q; ++i) {
        cin>>t;
        cout<<a[t].num<<" "<<a[t].id2<<endl;
        }
    return 0;
}

🌼六,L1-006 连续因子  

满分20

题目详情 - L1-006 连续因子 (pintia.cn)

做了挺久,2小时是有了,磕磕绊绊从2分 --> 5分 --> 19分 --> 20分

1,输出的是最小连续因子数,所以,比如输入9999,你不能输出1 9999

而要输出1 3,考虑到这里,需要初始化len = 0, num = 0

len为连续因子长度,num为连续因子第一个数

2,注意题目,1不算因子

3,开头写个判断素数的函数

4,代码第29行,保证乘积不超过n

5,代码第31行很关键,保证连续因子乘积依然是n的因子,且长度更大,即可更新

6,要会求因子,遍历2~sqrt(n),如果 n % i == 0表示i是n的因子,不需要遍历后面是因为,题目求的是连续的,平方根往后的因子肯定是断开的,毕竟sqrt(n) * sqrt(n)已经 == n了

#include<iostream>
#include<cmath> //sqrt()
using namespace std;

//素数的因子只有1和本身
bool prime(long long x)
{
    for(int i = 2; i <= sqrt(x); ++i)
        if(x % i == 0)
            return false; //不是素数
    return true;
}

int main()
{
    //pro连续乘积, len连续子序列长度, num连续子序列第一个数
    long long n, pro, len, num;
    cin>>n;
    //判断素数
    if(prime(n))
        cout<<1<<endl<<n;
    //不是素数
    else {
        //初始化num为0, len为0, 因为要输出"最小"的连续因子序列
        len = 0, num = 0;
        //遍历2~sqrt(n)
        for(int i = 2; i <= sqrt(n); ++i) {
            pro = 1;
            for(int k = i; pro * k <= n; ++k) { //保证乘积不超过n
                pro *= k;
                if(n % pro == 0 && k - i + 1 > len) { //关键
                    len = k - i + 1;
                    num = i;
                }
            }
        }
        //输出答案
        cout<<len<<endl;
        for(int i = num; i <= num + len - 1; ++i) {
            cout<<i;
            if(i != num + len - 1)
                cout<<'*';
        }
    }
    return 0;
}

🌼七,L1-007 念数字

题目详情 - L1-007 念数字 (pintia.cn)

满分10

注意几个点

1,switch, case, break的用法,能简化程序

2,注意,行末无空格

3,字符转ASCII

AC  代码

#include<iostream>
using namespace std;
int main()
{
    string s;
    int fir = 0;
    cin>>s;
    if(s[0] == '-') {
        cout<<"fu";
        if(s.size() > 1) cout<<" "; //行末无空格
        fir = 1;
    }
    for(int i = fir; i < s.size(); ++i) {
        switch(s[i] - '0') { //字符转ASCII
            case 0: cout<<"ling"; break;
            case 1: cout<<"yi"; break;
            case 2: cout<<"er"; break; //注意break
            case 3: cout<<"san"; break;
            case 4: cout<<"si"; break;
            case 5: cout<<"wu"; break;
            case 6: cout<<"liu"; break;
            case 7: cout<<"qi"; break;
            case 8: cout<<"ba"; break;
            case 9: cout<<"jiu"; break;
        }
        if(i != s.size() - 1) cout<<" "; //行末无空格
    }
    return 0;
}
-
fu

0
ling

-82654981
fu ba er liu wu si jiu ba yi

🌼八,L1-008 求整数段和

题目详情 - L1-008 求整数段和 (pintia.cn)

满分10

1,考察占位函数setw()的使用,头文件#include<iomanip>

2,每5个数换行一次,多测试几次,防止粗心

AC  代码

#include<iostream>
#include<iomanip> //setw()
using namespace std;
int main()
{
    int a, b, sum = 0;
    cin>>a>>b;
    for(int i = a; i <= b; ++i)
        sum += i;
    for(int i = a; i <= b; ++i) {
        cout<<setw(5)<<i; //占5个位
        if((i - a + 1) % 5 == 0) //每5个数换行一次
            cout<<endl;
    }
    if((b - a + 1) % 5 != 0) cout<<endl; //上面少一次换行的话
    cout<<"Sum = "<<sum;
    return 0;
}
-7 22
   -7   -6   -5   -4   -3
   -2   -1    0    1    2
    3    4    5    6    7
    8    9   10   11   12
   13   14   15   16   17
   18   19   20   21   22
Sum = 225

-5 11
   -5   -4   -3   -2   -1
    0    1    2    3    4
    5    6    7    8    9
   10   11
Sum = 51

🌼九,L1-009 N个数求和

前10题通过率最低的一题,16.28%,也做了最久....也许是2.5个小时.....

满分20

题目详情 - L1-009 N个数求和 (pintia.cn)

 

思路

1,求最大公约数(写个gcd函数)

欧几里得算法(辗转相除法),公式:gcd(a,b) = gcd(b,a mod b)

2,求所有分母最小公倍数,两两计算即可,也只用遍历一遍

3,一开始一直在纠结负数怎么处理,其实不需要处理,整型是包括负数的,scanf()读入

注意

1,长整型范围,用long long

2,对输出的结果分类讨论,存在0,负数,假分数,真分数等情况

AC  代码

#include<iostream>
#include<cstdio> //scanf()
using namespace std;

#define LL long long
long long a[110], b[110]; //保存分子 / 分母

LL gcd(LL x, LL y) //求最大公约数
{
    LL temp;
    while(x % y) {
        temp = x % y;
        x = y;
        y = temp;
    }
    return y;
}

int main()
{
    int n;
    cin>>n;
    //输入
    for(int i = 0; i < n; ++i)
        scanf("%lld/%lld", &a[i], &b[i]); //不用cin, 考虑到中间的/
    //求所有分母最小公倍数
    LL M = b[0];
    for(int i = 1; i < n; ++i)
        M =  M * b[i] / gcd(M, b[i]);
    //求分子的和
    for(int i = 0; i < n; ++i) {
        a[i] *= (M / b[i]); //通分后的分子
        if(i != 0)
            a[i] += a[i - 1]; //前缀和得到分子的和
    }
    //分类讨论输出答案
    LL GYS = gcd(a[n - 1], M); //最大公约数
    LL zi = a[n - 1] / GYS, mu = M / GYS; //化简分子分母
    //结果=0
    if(zi == 0)
        cout<<0;
    //结果<0
    else if(zi > 0) {
        if(zi % mu == 0) cout<<zi / mu;
        else if(zi < mu) cout<<(zi % mu)<<"/"<<mu;
        else cout<<zi / mu<<" "<<(zi % mu)<<"/"<<mu;
    }
    //结果>0
    else {
        if(zi % mu == 0) cout<<zi / mu;
        else if(zi < mu) cout<<(zi % mu)<<"/"<<mu;
        else cout<<zi / mu<<" "<<(zi % mu)<<"/"<<mu;
    }

    return 0;
}

🌼十,L1-010 比较大小

满分10

题目详情 - L1-010 比较大小 (pintia.cn)

AC  代码

#include<iostream>
using namespace std;
int main()
{
    int a, b, c, Min, Max, Middle;
    cin>>a>>b>>c;
    Min = min(min(a, b), c); //最小值
    Max = max(max(a, b), c); //最大值
    Middle = a + b + c - Min - Max; //中间值
    cout<<Min<<"->"<<Middle<<"->"<<Max;
    return 0;
}

🥁总结

复盘一下欠缺的地方

1,scnaf()和cin的使用不够灵活,拘泥于其中一种

      2,一些基础函数或者简单算法不熟练,比如setw()占位,gcd()辗转相除法求公约数,

            prime()判断素数,

3,模拟的题,容易心急跳步,导致出错,或者分类讨论不够全面

      4,读题不够准确,常常误解题意或输出无关的值

5,数组越界,整型范围,复杂度,找规律(老生常谈了)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

千帐灯无此声

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

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

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

打赏作者

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

抵扣说明:

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

余额充值