ACM模式各种输入整理(C++)

这篇具有很好参考价值的文章主要介绍了ACM模式各种输入整理(C++)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.前言

本文整理ACM模式的各种输入形式。

2. ACM模式的输入种类

2.1 整形数组输入

2.1.1 在终端的一行中输入固定数目的整型数字,并存到数组中,中间以空格分隔

示例:

  • 3
  • 1 2 3

方法1

#include <iostream>
#include <vector>
using namespace std;


int main(){
    int n;
    cin >> n;
    vector<int> nums(n);
    for (int i = 0; i < n; ++i){
        cin >> nums[i];
    }
    //测试:打印数组
    cout<<"test c++ input:"<<endl;
    for(auto i : nums)
        cout << i << " ";
    cout << endl;
}

方法2 

#include <iostream>
#include <vector>
using namespace std;


int main(){
    int n;
    cin >> n;
    vector<int> nums;
    nums.resize(n);
    for (int i = 0; i < n; ++i){
        cin >> nums[i];
    }
    //测试:打印数组
    cout<<"test c++ input:"<<endl;
    for(auto i : nums)
        cout << i << " ";
    cout << endl;
}

方法3

#include <iostream>
#include <vector>
using namespace std;


int main(){
    int n;
    cin >> n;
    vector<int> nums;
    for (int i = 0; i < n; ++i){
        int val;
        cin >> val;
        nums.push_back(val);
    }
    //测试:打印数组
    cout<<"test c++ input:"<<endl;
    for(auto i : nums)
        cout << i << " ";
    cout << endl;
}

 正确性测试:

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
3
1 2 3
test c++ input:
1 2 3

2.1.2 在终端的一行中输入非固定数目的整型数字,并存到数组中,中间以空格(或者其他单字符,./)分隔

方法1

#include <iostream>
#include <vector>
using namespace std;


int main(){
    vector<int> nums;
    int num;
    while(cin>>num){
        nums.push_back(num);
        if(getchar() == '\n')
            break;
    }
    //测试:打印数组
    cout<<"test c++ input:"<<endl;
    for(auto i : nums)
        cout << i << " ";
    cout << endl;
}

方法2

#include <iostream>
#include <vector>
using namespace std;


int main(){
    //代码通过cin.get()从缓存中读取一个字节,这样就扩充了cin只能用空格和TAB两个作为分隔符。
    vector<int> nums;
    int num;
    while(cin>>num){
        nums.push_back(num);
        if(cin.get() == '\n')
            break;
    }
    //测试:打印数组
    cout<<"test c++ input:"<<endl;
    for(auto i : nums)
        cout << i << " ";
    cout << endl;
}

正确性测试:

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
1 2 3 4
test c++ input:
1 2 3 4

2.1.3 在终端的一行中输入固定数目的整型数字,并存到数组中,中间以(其他单字符,./)分隔

示例:

  • 3
  • 1,2 ,3
#include <iostream>
#include <vector>
using namespace std;


int main(){
    int m;
    cin >>  m;
    char sep;
    vector<int> nums(m);

    for (int i = 0; i < m - 1; ++i){
        cin >> nums[i] >> sep;
    }
    cin >> nums[m - 1];
    //测试:打印数组
    cout<<"test c++ input:"<<endl;
    for(auto i : nums)
        cout << i << " ";
    cout << endl;
}

正确性测试:

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
3
1,2,3
test c++ input:
1 2 3

2.2 字符串输入

2.2.1 给定一行字符串,每个字符串用空格间隔,一个样例为一行

示例:

  • daa ma yello

方法

#include <iostream>
#include <vector>
using namespace std;


int main(){
    string str;
    vector<string> strs;
    while (cin >> str) {
        strs.push_back(str);
        if (getchar() == '\n') {  //控制测试样例
            for (auto& str : strs) {
                cout << "current character:"<< str << " ";
            }
            cout << endl;
            strs.clear();
        }
    }
    return 0;
}

正确性测试:

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
aaa bbb
current character:aaa current character:bbb

2.2.2 给定一行字符串,每个字符串用逗号间隔,一个样例为一行

方法:使用getline 读取一整行字符串到字符串input中,然后使用字符串流stringstream,读取单个数字或者字符。每个字符中间用','间隔

#include <iostream>
#include <vector>
#include <sstream>
#include<algorithm>

using namespace std;


int main(){
    string input;
    while (getline(cin, input)) {  //读取一行
        vector<string> strs;
        string str;
        stringstream ss(input);
        while(getline(ss, str,',')){
            strs.push_back(str);
        }
        sort(strs.begin(), strs.end());
        for (auto& str : strs) {
            cout << str << " ";
        }
        cout << endl;
    }
    return 0;
}

正确性测试:

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
d,v,b,c,f
b c d f v

2.2.3 给定一行字符串,每个字符串用空格间隔,一个样例为一行

#include <iostream>
#include <vector>
#include <sstream>

using namespace std;


int main(){
    string input;
    while (getline(cin, input)) {  //读取一行
        stringstream data(input);  //使用字符串流
        int num = 0, sum = 0;
        while (data >> num) {
            sum += num;
        }
        cout << sum << endl;
    }
    return 0;
}

正确性测试:

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
1 2 3 
6

2.3 字符串输入到数组

2.3.1 输入n行字符串,逗号分开,存到数组里

示例(假定每行输入5个数)

3

1,2,3,3,2

4,5,6,1,3

7,8,9,3,4

#include<bits/stdc++.h>

using namespace std;


int main()
{
    int n;
    cin >> n;
    vector<vector<int>> vec(n,vector<int>(5));
    for (int i = 0; i < n;++i){//注意这里一定要有i控制,用while容易一直输入导致错误
        string s;
        cin >> s;
        replace(s.begin(), s.end(), ',', ' ');
        istringstream input(s);
        string tmp;
        for (int j = 0; j < 5;++j){//内层循环也很重要
            input >> tmp;
            vec[i][j] = stoi(tmp);
        }
    }

    for (int i = 0; i < vec.size(); i++)
    {
        for (int j = 0; j < vec[0].size(); j++)
        {
            cout << "postion "<< i << j << " output is:"<< vec[i][j] << endl;
        }
    }
    return 0;
}

正确性测试

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
2
1,2,3,4,5,
6,7,8,9,0
postion 00 output is:1
postion 01 output is:2
postion 02 output is:3
postion 03 output is:4
postion 04 output is:5
postion 10 output is:6
postion 11 output is:7
postion 12 output is:8
postion 13 output is:9
postion 14 output is:0

Process finished with exit code 0

3. 最后

相对于leetcode模式,ACM模式需要自己写输入输出,但是常见输入输出也是几类。熟悉后,就不会在这方面浪费宝贵的时间了。文章来源地址https://www.toymoban.com/news/detail-432109.html

到了这里,关于ACM模式各种输入整理(C++)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Java】ACM模式下输入输出汇总(Java)

    (1)输入描述: (2)输出描述: (3)示例 输入: 输出: 代码: (1)输入描述: (2)输出描述: (3)示例 输入: 输出: 代码: (1)输入描述: (2)输出描述: (3)示例 输入: 输出: 代码: (1)输入描述: (2)输出描述: (3)示例 输入: 输出: 代码: (1)输入描述

    2024年02月12日
    浏览(27)
  • 【物联网】详解STM32的GPIO八种输入输出模式,GPIO各种输入输出的区别、初始化的步骤详解,看这文章就行了(超详细)_stm32输出模式

    复用开漏输出(AF Open-Drain Output):复用开漏输出模式允许将GPIO引脚用作特定外设功能。在该模式下,引脚只能输出低电平,要输出高电平需要通过外部上拉电阻或其他方式。 浮空输入(Floating Input):浮空输入模式是一种高阻抗输入模式。在该模式下,引脚不连接到外部电路,处

    2024年04月27日
    浏览(30)
  • 详解STM32的GPIO八种输入输出模式,GPIO各种输入输出的区别、初始化的步骤详解,看这文章就行了(超详细)

    在STM32微控制器中,常见的输入输出(GPIO)模式有八种,分别是推挽输出、开漏输出、复用推挽输出、复用开漏输出、浮空输入、上拉输入、下拉输入和模拟输入。下面我将为你解释每种模式的特点和区别,并提供相应的示例代码。 推挽输出(Push-Pull Output):推挽输出模式是最常

    2024年02月15日
    浏览(45)
  • 【物联网】详解STM32的GPIO八种输入输出模式,GPIO各种输入输出的区别、初始化的步骤详解,看这文章就行了(超详细)

    在STM32微控制器中,常见的输入输出(GPIO)模式有八种,分别是推挽输出、开漏输出、复用推挽输出、复用开漏输出、浮空输入、上拉输入、下拉输入和模拟输入。下面我将为你解释每种模式的特点和区别,并提供相应的示例代码。 推挽输出(Push-Pull Output):推挽输出模式是最常

    2024年02月14日
    浏览(40)
  • C++ 单例模式的各种坑及最佳实践

    单例模式是设计模式中最简单、常见的一种。其主要目的是确保整个进程中,只有一个类的实例,并且提供一个统一的访问接口。常用于 Logger 类、通信接口类、线程池等。 限制用户直接访问类的构造函数,提供一个统一的 public 接口获取单例对象。 这里有一个“先有鸡还是

    2024年02月08日
    浏览(28)
  • 本文整理了Debian 11在国内的几个软件源。

      1.使用说明  一般情况下,将/etc/apt/sources.list文件中Debian默认的软件仓库地址和安全更新仓库地址修改为国内的镜像地址即可,比如将deb.debian.org和security.debian.org改为mirrors.xxx.com,并使用https访问,可使用如下命令: 修改之后再运行apt update更新索引。 2.国内常见镜像站点

    2024年02月07日
    浏览(24)
  • C++设计模式创建型之工厂模式整理

    一、工厂模式分类         工厂模式属于创建型模式,一般可以细分为简单工厂模式、工厂模式和抽象工厂模式。每种都有不同的特色和应用场景。 二、工厂模式详情 1、简单工厂模式 1)概述         简单工厂模式相对来说,在四人组写的《设计模式------可复用面向对

    2024年02月14日
    浏览(26)
  • 【ACM竞赛入门】001.一文读懂常见的ACM题型输入输出格式

    本文通过各种类型的A+B题目来帮助大家快速了解ACM题目中常见的输入输出格式,帮助大家快速上手 时间限制: 1s 内存限制: 64MB 题目描述 Your task is to Calculate a + b. Too easy?! Of course! I specially designed the problem for acm beginners. You must have found that some problems have the same titles with this one,

    2024年02月07日
    浏览(29)
  • 华为OD机试ACM输入输出

    华为OD机考是基于牛客平台进行的,且必须采用ACM模式 ACM模式: 机试系统调用你的代码时,传入的都是字符串, 题目的输入描述 会说明字符串的组成规则,你需要根据这些规则从输入字符串中解析出需要的数据。 当算法程序计算出结果后,也不能直接返回给机试系统,因为

    2024年02月06日
    浏览(48)
  • 华为OD机试(2022&2023)ACM输入输出

    华为OD机考是基于牛客平台进行的,且必须采用ACM模式 ACM模式: 机试系统调用你的代码时,传入的都是字符串, 题目的输入描述 会说明字符串的组成规则,你需要根据这些规则从输入字符串中解析出需要的数据。 当算法程序计算出结果后,也不能直接返回给机试系统,因为

    2023年04月27日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包