C++ 笔试 对于处理输入的一些常用操作

1.对于普通的 输入n 然后输入n个数的情况

int n;
	cin >> n;
	vector<int>data;
	for (int i = 0;i < n;i++) {
		cin >> data[i];
	}
  1. 对于输入N组数据 每组数据的个数不确定的情况
    应该这样写
int rows = 0;
	cin >> rows;

	vector<vector<int>> data(rows);

	int temp = 0;
	for (int i = 0; i < rows; i++)
	{
		while (std::cin >> temp)
		{
			data[i].push_back(temp);
			if (getchar() == '\n')
				break;
		}
	}

或者 还有一种就是
scanf函数的话
输入 整数 %d 字符串 %s 字符 %c

while(1){
scanf("%d",&t);
if(getchar()=='\n')
	break;
}

注意的是:

3.对于需要固定长度的这种 我们scanf或者 cin都很容易

 int x, y; scanf("%d%d", &x, &y);

对于同时两个的 不会在意这个输入格式

例如 字节跳动2019春招研发部分编程题汇总
小明是一名算法工程师,同时也是一名铲屎官。某天,他突发奇想,想从猫咪的视频里挖掘一些猫咪的运动信息。为了提取运动信息,他需要从视频的每一帧提取“猫咪特征”。一个猫咪特征是一个两维的vector。如果x_1=x_2 and y_1=y_2,那么这俩是同一个特征。
因此,如果喵咪特征连续一致,可以认为喵咪在运动。也就是说,如果特征在持续帧里出现,那么它将构成特征运动。比如,特征在第2/3/4/7/8帧出现,那么该特征将形成两个特征运动2-3-4 和7-8。
现在,给定每一帧的特征,特征的数量可能不一样。小明期望能找到最长的特征运动。

#include
using namespace std;
unordered_map<string, int> last;
unordered_map<string, int> cur;
 
string make_str(int x, int y) {
    return to_string((long long)x) + to_string((long long)y);
}
 
int N, M;
int main() {
    scanf("%d", &N);
    while(N--) {
        scanf("%d", &M);
        int res = 1;
        while(M--) {
            int cnt; scanf("%d", &cnt);
            cur.clear();
            while(cnt--) {
                int x, y; scanf("%d%d", &x, &y);
                if(last.count(make_str(x, y))) {
                    cur[make_str(x, y)] += (last[make_str(x, y)] + 1);
                    res = max(res, cur[make_str(x, y)]);
                } else {
                    cur[make_str(x, y)]++;
                }
            }
            last = cur;
        }
        last.clear();
        printf("%d\n", res);
    }
}
/*
1
8
2 1 1 2 2
2 1 1 1 4
2 1 1 2 2
2 2 2 1 4
0
0
1 1 1
1 1 1
res: 3
*/

你可能感兴趣的:(企业真题笔记)