海康面试代码题

面试过程超级顺利,项目也感兴趣,简历从上到下问了一遍,就在我以为功德圆满之际,面试官突然说:当场你写个代码吧!!写个代码吧...代码吧...,我的内心是....#$%^&*@#$%...,最近几个月每天都是python的算法和网络,突然让写C的,大脑突然就调取内存失败,没想到超级简单的题目,整数逆序输出,连华为笔试第一道题的难度都没有。面试官看我迟迟不下笔,说有什么问题吗?我面不改色:“没有没有~……”,然并卵,心里一万只草泥马奔过,包含哪些库来着?定义哪个类型?Wait !!C里面怎么读入数据来着???那黑暗三分钟感觉自己突然宛若一个智障,觉得老脸都要搁那了,emmmm~~~,还是得复习复习一雪前耻,bgm: 都是我给自己自由过了火,坚持裸面的后果~~~

#include<iostream>
using namespace std;

int main()
{
	int i;
	int res[10];

	cout << "please enter a intger number:\n" << endl;

	while (cin >> i)
	{
		int n = 0;
		if (i == 0)
			cout << i << endl;
		else
		{
			while (i)
			{
				res[n] = i % 10;
				n++;
				i = i / 10;
			}

			for (int j = 0; j < n; j++)
			{
				cout << res[j];
			}
			cout << endl;
		}
	}
	return 0;
}

或者

#include<iostream>
using namespace std;

int main()
{
	int i;
	int res[10] = { 0 };

	cout << "please enter a intger number:\n" << endl;

	while (cin >> i)
	{
		int n = 0;
		while (i)
		{
			res[n] = i % 10;
			n++;
			i = i / 10;
		}
		cout << res[0];
		for (int j = 1; j < n; j++)
		{
			cout << res[j];
		}
		cout << endl;
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章