面试笔试整理:1、笔试常见输入输出(待补充)

常用的输入输出

整理笔试时候常用的输入输出,可以使用

#include <bits/stdc++.h>

来一次性输入所有的C++头文件包括:

\#include <iostream>
\#include <cstdio>
\#include <fstream>
\#include <algorithm>
\#include <cmath>
\#include <deque>
\#include <vector>
\#include <queue>
\#include <string>
\#include <cstring>
\#include <map>
\#include <stack>
\#include <set>
等等……

1、c++篇

(1)输入一个数字再加上空格间隔的数组:

int num;
cin>>num;
int *arr = new int[len+1]
for(int i=0; i<=num; i++){
    cin>>arr[i]; 
}

(2)需要多次输入形式重复的数据:

while(cin>>k)

(3)对于不存在空格的字符数组:

char str[30];
cin>>str;

(4)输入带有空格的字符数组:

char str[30];
cin.getline(str,len);//读入整行数据,它使用回车键输入的换行符来确定输入结尾。
//cin.get(str,len);//这个还会保留换行符

(5)输入字符串string

string str;
getline(cin,str);

(6)输入多个整数

int a,b,c,d;
cin>>a>>b>>c>>d;

(7)具体应用:数组从小到大排列并去除重复项

  • sort,将这组数字从小到大排列;
  • unique,将相邻且重复的数放到vector的尾部,然后返回指向第一个重复元素的迭代器(需要注意的是,被放在尾部的数据有时会产生变化,所以不能继续使用了,需要废弃掉);
  • erase,擦除重复的数据
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    vector<int> v;
    cout << "please input the number of vector's element" << endl;
    int number;
    cin >> number;
    for (int i = 0; i < number; i++)
    {
        int temp;
        cin >> temp;
        v.push_back(temp);   //在vector尾部加入一个数据
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    for (int i = 0; i < v.size(); i++)
    {
        cout << v[i] << " ";
    }
    return 0;
}

(8)输出不定量的行的字符串

string str;
while(cin>>str)

2、python篇

(1)输入多个整数

a,b,c,d = map(int, input().split())

或者写成:

str_in= input()
num = [int(n), for n instr_in.split()]

(2)输入多行数据

N = int(input())
inputlist = []
area = 0
for i in range(N):
    lines = input()
    inputlist.append(lines.split())

(3)不定行的数据

import sys
for line in sys.stdin:
    .....
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值