力扣刷题本地化-输入输出方法大全

力扣刷题本地化-输入输出方法大全

力扣代码在本地编译运行-数组转化成容器打印

参考代码随想录
拿我们刚讲过的这道题动态规划:使用最小花费爬楼梯 (opens new window)来做示范

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

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        vector<int> dp(cost.size());
        dp[0] = cost[0];
        dp[1] = cost[1];
        for (int i = 2; i < cost.size(); i++) {
            dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];
        }
        return min(dp[cost.size() - 1], dp[cost.size() - 2]);
    }
};

int main() {
    int a[] = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1};
    vector<int> cost(a, a + sizeof(a) / sizeof(int));
    Solution solution;
    cout << solution.minCostClimbingStairs(cost) << endl;
}

ACM模式-一维vector初始化

示例一:

输入: 4 1 1 0 0 0 1 1 0

输出: 2

#include<iostream>
#include<vector>
using namespace std;
int main() {
    int n;
    while (cin >> n) {
        vector<int> gym(n);
        vector<int> work(n);
        for (int i = 0; i < n; i++) cin >> work[i];
        for (int i = 0; i < n; i++) cin >> gym[i];
        int result = 0;

        // 处理逻辑

        cout << result << endl;
    }
    return 0;
}

二维容器的初始化

c++利用vector创建二维数组的几种办法

#include <iostream>
#include <vector>
using namespace std;
 
void test01() {
     //创建一个外层容器
	vector<vector<int>>v;
    //创建一些内层容器,并赋值
	vector<int>v1(10,1);
	vector<int>v2(10,2);
	vector<int>v3(10,3);
    //将内层小容器插入到大容器之中,类似于二维数组。
	v.push_back(v1);
	v.push_back(v2);
	v.push_back(v3);
 
}

链表的构建与输出

以移除链表元素为例

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

// 单链表结点定义
struct ListNode{
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

// 生成链表
ListNode* constructList(vector<int> nodes)
{
    ListNode* dummyHead = new ListNode();
    ListNode* cur = dummyHead;
    for (auto i : nodes)
    {
        cur->next = new ListNode(i);
        cur = cur->next;
    }
    cur = dummyHead->next;
    delete dummyHead;
    return cur;
}

// 打印链表
void printList(ListNode* head)
{
    ListNode* cur = head;
    if (cur == NULL)
    {
        cout << "[]" << endl;
    }
    while (cur != NULL)
    {
        cout << cur->val << ' ';
        cur = cur->next;
    }
}

// 使用虚假头部统一删除结点
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val)
    {
        ListNode* dummyHead = new ListNode();
        dummyHead->next = head;
        ListNode* cur = dummyHead;
        while (cur->next != NULL)
        {
            if (cur->next->val == val)
            {
                ListNode* temp = new ListNode();
                temp = cur->next;
                cur->next = cur->next->next;
                delete temp;
            }
            else
            {
                cur = cur->next;
            }
        }
        head = dummyHead->next;
        delete dummyHead;
        return head;
    }
};

int main() {

//    int nums[] = {1,2,6,3,4,5,6}; 7,7,7,7
//    int val = 6;
    int nums[] = {7,7,7,7};
    int val = 7;
    vector<int> nodes(nums, nums + sizeof(nums) / sizeof(int));
    ListNode* head = constructList(nodes);
    Solution solution;
    ListNode* result = solution.removeElements(head, val);
    printList(result);
    return 0;
}

构建二叉树并打印-力扣上如何自己构造二叉树输入用例?

参考代码随想录
以以下这道题为例

538.把二叉搜索树转换为累加树

根据数组构造二叉树的基本步骤

  1. 创建节点数组和头结点
  2. 把输入数值数组,先转化为二叉树节点数组
  3. 遍历一遍,根据规则左右孩子赋值就可以了
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

// 根据数组构造二叉树
TreeNode* construct_binary_tree(const vector<int>& vec) {
    vector<TreeNode*> vecTree (vec.size(), NULL);
    TreeNode* root = NULL;
    for (int i = 0; i < vec.size(); i++) {
        TreeNode* node = NULL;
        if (vec[i] != -1) node = new TreeNode(vec[i]);
        vecTree[i] = node;
        if (i == 0) root = node;
    }
    for (int i = 0; i * 2 + 2 < vec.size(); i++) {
        if (vecTree[i] != NULL) {
            vecTree[i]->left = vecTree[i * 2 + 1];
            vecTree[i]->right = vecTree[i * 2 + 2];
        }
    }
    return root;
}

// 层序打印打印二叉树
void print_binary_tree(TreeNode* root) {
    queue<TreeNode*> que;
    if (root != NULL) que.push(root);
    vector<vector<int>> result;
    while (!que.empty()) {
        int size = que.size();
        vector<int> vec;
        for (int i = 0; i < size; i++) {
            TreeNode* node = que.front();
            que.pop();
            if (node != NULL) {
                vec.push_back(node->val);
                que.push(node->left);
                que.push(node->right);
            }
            // 这里的处理逻辑是为了把null节点打印出来,用-1 表示null
            else vec.push_back(-1);
        }
        result.push_back(vec);
    }
    for (int i = 0; i < result.size(); i++) {
        for (int j = 0; j < result[i].size(); j++) {
            cout << result[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    // 注意本代码没有考虑输入异常数据的情况
    // 用 -1 来表示null
    vector<int> vec = {4,1,6,0,2,5,7,-1,-1,-1,3,-1,-1,-1,8};
    TreeNode* root = construct_binary_tree(vec);
    print_binary_tree(root);
}

整数的输入求和-牛客网

计算a+b(1)-不停输入两个值,并求和打印

题目

链接:https://ac.nowcoder.com/acm/contest/5657/A
来源:牛客网

输入描述:

输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。

输出描述:

输出a+b的结果

输入

1 5
10 20

输出

6
30
答案
#include <iostream>
using namespace std;

int main()
{    
    int a , b;
    while (cin >> a >> b)
    {
        cout << a + b << endl;
    }
    return 0;
}

计算a+b (2)-按照次数输入两个值,求和并打印

题目

链接:https://ac.nowcoder.com/acm/contest/5657/B
来源:牛客网

输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 1000)

输入
2
1 5
10 20
输出
6
30

答案
#include <iostream>
using namespace std;
int main()
{
    int a, b, count;
    cin >> count;
    while (count -- )
    {
        cin >> a >> b;
        cout << a + b << endl;
    }
    return 0;
}

计算a+b (3)-输入两个值,求和,遇到(0,0)停止

题目

链接:https://ac.nowcoder.com/acm/contest/5657/C
来源:牛客网

输入描述:

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入

输入
1 5
10 20
0 0
输出
6
30

答案
#include <iostream>
using namespace std;
int main()
{
    int a, b;
    while (cin >> a >> b )
    {
        if (a == 0 && b == 0)
        {
            break;
        }
        cout << a + b << endl;
    }
    return 0;
}

计算a+b (4)-输入一组自定义数量的数据,求和,遇到0停止

题目

链接:https://ac.nowcoder.com/acm/contest/5657/D
来源:牛客网

输入描述:

输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。

输出描述:
每组数据输出求和的结果

示例1
输入
4 1 2 3 4
5 1 2 3 4 5
0
输出
10
15

答案
#include <iostream>
using namespace std;
int main()
{
    int sum;
    int count;
    while (cin >> count)
    {    
        if (count == 0) break;
        while(count -- )
        {
            int a;
            cin >> a;
            sum += a;
        }
        cout << sum << endl;
        sum = 0;
    }
    return 0;
}

计算a+b (5)-按照次数,输入多组自定义数量的数据,求和

题目

链接:https://ac.nowcoder.com/acm/contest/5657/E
来源:牛客网

输入描述:

输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

输出描述:
每组数据输出求和的结果

示例1
输入
2
4 1 2 3 4
5 1 2 3 4 5

输出
10
15

答案
#include <iostream>
using namespace std;
int main()
{
    int sum;
    int row;
    cin >> row;
    while (row -- )
    {    
       // if (count == 0) break;
        int column;
        cin >> column;
        while(column -- )
        {
            int a;
            cin >> a;
            sum += a;
        }
        cout << sum << endl;
        sum = 0;
    }
    return 0;
}

计算a+b (6)-不停地输入一组自定义数量的数据,求和

题目

和第四道基本一样

链接:https://ac.nowcoder.com/acm/contest/5657/F
来源:牛客网

输入描述:
输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果

示例1
输入
4 1 2 3 4
5 1 2 3 4 5
输出
10
15

答案
#include <iostream>
using namespace std;
int main()
{
    int sum;
    int count;
    while (cin >> count)
    {    
        // if (count == 0) break;
        while(count -- )
        {
            int a;
            cin >> a;
            sum += a;
        }
        cout << sum << endl;
        sum = 0;
    }
    return 0;
}

计算a+b (7)(困难)-输入一组的数据,遇到回车就求和打印,循环这个过程

题目

链接:https://ac.nowcoder.com/acm/contest/5657/G
来源:牛客网

输入描述:
输入数据有多组, 每行表示一组输入数据。

每行不定有n个整数,空格隔开。(1 <= n <= 100)。
输出描述:
每组数据输出求和的结果

示例1
输入
1 2 3
4 5
0 0 0 0 0
输出
6
9
0

答案
#include <iostream>
using namespace std;
int main()
{
    int sum;
    int num;
    while (cin >> num)
    {    
        sum = 0;
        while(true)
        {
            sum += num;
            if (cin.get() == '\n') // 重点
            {
               break;
            }
            cin >> num;
        }
        cout << sum << endl;
    }
    return 0;
}

字符串的输入排序-输入一组自定义数量的字符串,排序打印

字符串排序1

题目

链接:https://ac.nowcoder.com/acm/contest/5657/H
来源:牛客网

输入描述:
输入有两行,第一行n

第二行是n个字符串,字符串之间用空格隔开
输出描述:
输出一行排序后的字符串,空格隔开,无结尾空格

示例1
输入
5
c d a bb e
输出
a bb c d e

答案
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    vector<string> vec(n);
    for (int i = 0; i < n; i ++ )
    {    
        cin >> vec[i];
    }
    sort(vec.begin(), vec.end()); // 排序
    string res = "";
    for (int i = 0; i < n; i ++ )
    {    
        res += vec[i];
        res += " ";
    }
    res.pop_back(); // 去掉空格
    cout << res << endl; 
    return 0;
}

字符串排序2-只要输入一组字符串,就排序打印

题目

链接:https://ac.nowcoder.com/acm/contest/5657/I
来源:牛客网

输入描述:
多个测试用例,每个测试用例一行。

每行通过空格隔开,有n个字符,n<100
输出描述:
对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开

示例1
输入
a c bb
f dddd
输出
a bb c
dddd f

答案
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    string temp;
    while (cin >> temp)
    {
        vector<string> s;
        while (true)
        {
            s.push_back(temp);
            if(cin.get()=='\n') break;
            cin >> temp;
        }
        sort(s.begin(), s.end());
        for (auto i : s)
        {
            cout << i << " ";
        }
        cout << endl;
    }
    return 0;
}

字符串排序3(困难)-只要输入一组字符串(用规定字符隔开),就排序打印

题目

链接:https://ac.nowcoder.com/acm/contest/5657/J
来源:牛客网

输入描述:
多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100
输出描述:
对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格

示例1
输入
a,c,bb
f,dddd
nowcoder
输出
a,bb,c
dddd,f
nowcoder

答案
#include <math.h>

#include <algorithm>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  string s;
  while (getline(cin, s)) { // 读取整行
    stringstream ss(s); // 转化为string
    vector<string> vec;
    string tmp;
    while (getline(ss, tmp, ',')) { //getline的第三个参数是终止字符,到当前字符终止
      vec.push_back(tmp);
    }
    sort(vec.begin(), vec.end());
    string res = "";
    for (auto sss : vec) res += sss + ',';
    res.pop_back();
    cout << res << endl;
  }
  return 0;
}

字符串分割神器–stringstream+getline

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值