C++笔试奇怪输入输出总结

7 篇文章 0 订阅

输入输出常用函数

cin
cin.getline()
cin.get()

举例

1.一维数组输入,第一行输入一个数字代表数组长度,第二行开始输入数组元素,中间以空格分隔

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include "stdio.h"
#include<stdlib.h>
using namespace std;



int main(void)
{
	int num;
	cin >> num;
	vector<int> nums;
	int temp;
	for (int i = 0; i < num; i++)
	{
		cin >> temp;
		nums.push_back(temp);
	}
	for (auto num : nums)
	{
		cout << num << " ";
	}
	cout << endl;
	system("pause");
	return 0;

}

输出:
6
1 2 3 4 5 6
1 2 3 4 5 6
请按任意键继续. . .

2.一维数组输入,第一行输入一个数字代表数组长度,第二行开始输入数组元素,中间以逗号分隔
下面这个代码处理以空格输入也是可以的

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include "stdio.h"
#include<stdlib.h>
using namespace std;



int main(void)
{
	int num;
	cin >> num;
	vector<int> nums;
	int temp;
	while (cin >> temp)
	{
		nums.push_back(temp);
		if (cin.get() == '\n') break;
}
	for (auto num : nums)
	{
		cout << num << " ";
	}
	cout << endl;
	system("pause");
	return 0;

}

输出:
6
1 2 3 4 5 6
1 2 3 4 5 6
请按任意键继续. . .

3.第一行输入一个长度k,第二行输入k个节点转发能力数组,以空格为间隔,每个转发能力数组元素之间以逗号分隔,如:12,12 11,11 1,1
输入示例:
3
1,2 3,4 5,6

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include "stdio.h"
#include<stdlib.h>
using namespace std;



int main(void)
{
	int k;
	cin >> k;
	int temp;

	vector<vector<int>> arr(k, vector<int>(2));
	for (int i = 0; i < k; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			cin >> temp;
			arr[i][j] = temp;
			if (cin.get() == ' ') continue;
		}
	}
	for (int i = 0; i < k; i++)
	{
		for (int m = 0; m < 2; m++)
		{
			cout << arr[i][m] << " ";
		}
	}
	cout << endl;
	system("pause");
	return 0;

}

4.输入流为字符串,字符串为单词与逗号组合,如:hello,world,hello,csdn

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

int main()
{
 string s;
 while (cin >> s)//输入一串用逗号分隔的字符串
 {
  vector<string> vs;
  string tmp;
  stringstream st;//创建string流对象
  st.clear();//清空流错误标志位,以便下一次使用,但是没用不会清空使用的内存空间
  st.str("");//清空string流对象,清空内存,占用大小置0

  st.str(s);//用s作为st流中的内容

  while (getline(st, tmp, ','))//从st流中按逗号为分隔符读取数据存储tmp中,直到流结束
   vs.push_back(tmp);

  for (auto x : vs)
   cout << x <<" ";
  cout << endl;
 }

 return 0;
}

5.输入:m=3,n=4,将3和4提取到一个数组里

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include<iomanip>
#include<cstring>
#include <string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

int main()
{

	string s;
	cin >> s;
	vector<int> arr;
	s += ',';
	int pos1 = s.find('=');//找到第一个等号的位置
	int pos2 = s.find(',');//找到第一个逗号的位置

	while (pos1!=string::npos)
	{
		arr.push_back(stoi(s.substr(pos1 + 1, pos2 - pos1 - 1)));
		pos1 = s.find('=', pos2 + 1);//位置更新,下同
		pos2 = s.find(',', pos2 + 1);
	}


	for (auto x : arr)
	{
		cout << x<<" ";
	}
	cout << endl;


	return 0;
}

输出结果:
m=3,n=4
3 4

F:\cppcode\first\leetcode\leetcode\Debug\leetcode.exe (进程 10188)已退出,返回代码为: 0。
若要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口…

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值