ACM模式处理输入输出大全(多组数据、数组、链表、二叉树链接整理)

1、一维数组输入:

关键代码:

int main() {
	int n;
    cin >> n;
    vector<int> nums(n);
    for(int i = 0; i < n; i++) {
	   cin >> nums[i];
   }
}

2、旋转矩阵,手写输入二维矩阵

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

void rotate(vector<vector<int>>& matrix) {
    int m = matrix.size();
    int n = matrix[0].size();

    for (int i = 0; i < m / 2; i++) {
        for (int j = 0; j < n; j++) {
            swap(matrix[i][j], matrix[m - i - 1][j]);
        }
     }

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < i; j++) {
            swap(matrix[i][j], matrix[j][i]);
        }
    }
}

int main() {
    int m, n, temp;
    cin >> m;
    cin >> n;
    vector<vector<int>> matrix(m, vector<int>(n));
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cin >> temp;
            matrix[i][j] = temp;
        }
    }
    rotate(matrix);
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cout << matrix[i][j] << " ";
        }
         cout << endl;
     }
     
     system("PAUSE");
     return 0;
}

在这里插入图片描述

3、合并两个链表,手写输入链表。

第一种写法,自建链表

///#include <bits/stdc++.h> //包含所有c++头文件
//#include <iostream>
//#include <vector>
//using namespace std;
//struct ListNode {
//    int val;
//    ListNode* next;
//    ListNode(int x) : val(x), next(NULL) {}
//};
//ListNode* createList(vector<int>& nums) { //链表转换为数组
//    if (nums.size() == 0)
//        return NULL;
//    ListNode* head = new ListNode(nums[0]);
//    ListNode* curNode = head;
//    for (int i = 1; i < nums.size(); ++i) {
//        curNode->next = new ListNode(nums[i]);
//        curNode = curNode->next;
//    }
//    return head;
//}
//ListNode* MergeTwoListNode(ListNode* l1, ListNode* l2) { //合并两个链表
//    if (!l1)
//        return l2;
//    if (!l2)
//        return l1;
//    if (l1->val < l2->val) {
//        l1->next = MergeTwoListNode(l1->next, l2);
//        return l1;
//    }
//    else {
//        l2->next = MergeTwoListNode(l1, l2->next);
//        return l2;
//    }
//}
//int main()
//{
 //    vector<int> nums1, nums2;
//    int num1, num2;
//    while (cin >> num1)
//    {
//        nums1.push_back(num1);
//        if (cin.get() == '\n')
//            break;
//    }
//    while (cin >> num2)
//    {
//        nums2.push_back(num2);
//        if (cin.get() == '\n')
//            break;
//    }
//    ListNode* head1 = createList(nums1);
//    ListNode* head2 = createList(nums2);
//    ListNode* res = MergeTwoListNode(head1, head2);
//    ListNode* p = res;
//    while (p)
//    {
//        cout << p->val << " ";
//        p = p->next;
//    }
//    return 0;
//}

//方法2,直接放入数组中:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
	vector<int> nums1, nums2, nums;
 	int num1, num2;
	 while (cin >> num1)
	{
   		nums1.push_back(num1);
    	nums.push_back(num1);
    	if (cin.get() == '\n')
        	break;
	}
	while (cin >> num2)
	{
    	nums2.push_back(num2);
    	nums.push_back(num2);
    	if (cin.get() == '\n')
        	break;
 	}
	sort(nums.begin(), nums.end());
	for (int i = 0;i < nums.size();i++)
    	cout << nums[i] << " ";
	return 0;
}

在这里插入图片描述

4、层序遍历,手写输入输出二叉树

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

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

void treePrint(TreeNode* root) {
	if (!root) return;
	queue<TreeNode*> que;
	que.push(root);
	while (!que.empty()) {
    	int size = que.size();
    	for (int i = 0; i < size; i++) {
        	TreeNode* node = que.front();
        	que.pop();
        	if (!node) cout << "-1 ";
        	else {
            	cout << node->val << " ";
            	que.push(node->left);
            	que.push(node->right);
       		 }
   		}
    	cout << endl;
	}
}

//需要补齐-1当前节点的左右节点
TreeNode* build(const vector<int>& nums) {
	vector<TreeNode*> tree;
	for (auto x : nums) {
    	if (x == -1) tree.push_back(nullptr);
    	else tree.push_back(new TreeNode(x));
	}
	int idx = 1;
	for (int i = 0; i < tree.size(); i++) {
   		if (tree[i] == nullptr) continue;
   	    if (idx >= tree.size()) break;
    	tree[i]->left = tree[idx];
    	tree[i]->right = tree[idx + 1];
    	idx += 2;
	}
   return tree[0];
}

int main() {
	int n;
	cin >> n;
	vector<int> nums(n);
	for (int i = 0; i < n; i++) {
		cin >> nums[i];
	}
	TreeNode* root = build(nums);
	treePrint(root);
	system("pause");
	return 0;
}

在这里插入图片描述

(ps:缩进有的可能没注意,还有内存泄漏需要注意,见谅!)

参考链接(非常推荐):
多组数据/字符串输入:https://www.cnblogs.com/jiangxinnju/p/8506089.html
输入描述的例子:https://blog.csdn.net/qq_39295220/article/details/116785551
输入链表:https://blog.csdn.net/PETERPARKERRR/article/details/122806763
输入二叉树:
https://blog.csdn.net/weixin_49220519/article/details/121721096
https://www.csdn.net/tags/MtTaMg2sNjc3MjMyLWJsb2cO0O0O.html

内容来源于参考链接和整理,侵权联系删。
欢迎关注!

  • 5
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

执着且专注

予人玫瑰,手有余香

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

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

打赏作者

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

抵扣说明:

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

余额充值