[SP1] TEST - Life, the Universe, and Everything.题解

本文通过一个具体的编程题目,介绍了在C++中使用递归和while循环处理连续输入的方法。通过对比两种解决方案,帮助读者理解递归调用和逗号表达式的使用场景。

这个红SP题过水,入门coders可自行食用!

老规矩,先看题目。

显然题目不难理解,就是让我们在看到424242之前,输出所有输入的数字。
这题我们讲解两种做法:一个是递归,一个是whilewhilewhile的不断输入用法!


代码

一、递归法
(注释版)
#include<bits/stdc++.h>//万能头文件
using namespace std;
int main(){//好习惯,程序从主函数读起
    int a;
    cin>>a;
    if(a!=42) cout<<a<<endl,main();//当a不等于42时,输出a,并无限次递归调用主函数
    return 0;//返回值0,程序正常退出
}
(无注释版)
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    if(a!=42) cout<<a<<endl,main();
    return 0;
}

二、whilewhilewhile输入法
(注释版)
#include<bits/stdc++.h>//万能头文件
using namespace std;
int main(){//好习惯,程序从主函数读起
    int a;
    while(cin>>a,a!=42)
    /*while(cin>>a)表示不断输入a,直到程序结束即EOF(End of File)
    其中用逗号连接两个不同表达式,类似于逻辑且&&*/
        cout<<a<<endl;
    return 0;
}
(无注释版)
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a;
    while(cin>>a,a!=42)
        cout<<a<<endl;
    return 0;
}

总结

1. 不断递归调用mainmainmain(或其他函数)能达到反复执行一内容的效果。
2. while(cin>>a)while(cin>>a)while(cin>>a): 不断输入aaa直至无可读取信息。
3. while(,)while( , )while(,): 用逗号表达式连接不同的表达式,类似于逻辑且&&。

root@ubuntu:~# apt install python3-venv -y Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: python3-pip-whl python3-setuptools-whl python3.10-venv The following NEW packages will be installed: python3-pip-whl python3-setuptools-whl python3-venv python3.10-venv 0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded. Need to get 2,474 kB of archives. After this operation, 2,890 kB of additional disk space will be used. Ign:1 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-pip-whl all 22.0.2+dfsg-1ubuntu0.4 Ign:2 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-setuptools-whl all 59.6.0-1.2ubuntu0.22.04.1 Ign:3 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3.10-venv amd64 3.10.12-1~22.04.3 Ign:4 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-venv amd64 3.10.6-1~22.04 Ign:1 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-pip-whl all 22.0.2+dfsg-1ubuntu0.4 Ign:2 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-setuptools-whl all 59.6.0-1.2ubuntu0.22.04.1 Ign:3 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3.10-venv amd64 3.10.12-1~22.04.3 Ign:4 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-venv amd64 3.10.6-1~22.04 Ign:1 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-pip-whl all 22.0.2+dfsg-1ubuntu0.4 Ign:2 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-setuptools-whl all 59.6.0-1.2ubuntu0.22.04.1 Ign:3 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3.10-venv amd64 3.10.12-1~22.04.3 0% [Working]
07-02
### 安装 python3-venv 失败的解决方 在 Ubuntu 系统中安装 `python3-venv` 时遇到下载失败的问题,通常是由于软件源配置不当或网络问题导致的。以下是一些常见的解决方1. **更换镜像源** 默认的官方镜像源可能因为网络原因导致下载速度慢或无访问。建议将系统源更换为国内的镜像站点,例如阿里云、华为云或清华源等。以华为云源为例,在 `/etc/apt/sources.list` 文件中添加以下内容: ``` deb http://mirrors.huaweicloud.com/repository/ubuntu/ jammy main restricted deb http://mirrors.huaweicloud.com/repository/ubuntu/ jammy universe deb http://mirrors.huaweicloud.com/repository/ubuntu/ jammy multiverse deb http://mirrors.huaweicloud.com/repository/ubuntu/ jammy-security main restricted deb http://mirrors.huaweicloud.com/repository/ubuntu/ jammy-security universe deb http://mirrors.huaweicloud.com/repository/ubuntu/ jammy-security multiverse ``` 更换完成后执行 `sudo apt update` 更新软件包列表 [^5]。 2. **检查 Python 版本兼容性** 不同版本的 Python 对应的 `venv` 包也不同,比如 `python3.8-venv` 是针对 Python 3.8 的虚拟环境工具。如果尝试安装的 `venv` 包与当前系统的 Python 版本不匹配,可能会导致依赖冲突。可以使用以下命令查看当前系统支持的 `venv` 版本: ```bash sudo apt-cache madison python3-venv ``` 根据输出选择合适的版本进行安装,例如 `sudo apt install python3.12-venv` [^3]。 3. **手动安装缺失的依赖项** 如果提示某些依赖包(如 `python-pip-whl` 或 `python3.8-distutils`)未满足,可以尝试单独安装这些依赖项: ```bash sudo apt install python-pip-whl python3.8-distutils ``` 4. **更新系统并升级软件包** 在安装之前确保系统和所有软件包都是最新的,运行以下命令: ```bash sudo apt update && sudo apt upgrade -y ``` 5. **使用 pip 安装 virtualenv 或 venv 模块** 如果通过 APT 安装 `python3-venv` 仍然存在问题,可以考虑直接使用 `pip` 安装 `virtualenv` 工具来替代标准的 `venv` 功能: ```bash pip3 install virtualenv ``` 创建虚拟环境的方如下: ```bash virtualenv myenv source myenv/bin/activate ``` 6. **检查 held broken packages** 如果提示存在 “held broken packages”,可以通过以下命令尝试修复: ```bash sudo apt --fix-broken install ``` 7. **清除 APT 缓存并重新下载** 有时 APT 缓存可能损坏,导致下载失败,可以尝试清除缓存后重新安装: ```bash sudo apt clean sudo apt update sudo apt install python3-venv ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值