[Off] cattle deft strokes written in autumn 2020 Campus Recruitment - A paper algorithm

Link: https: //www.nowcoder.com/questionTerminal/c8cfc98711a14a3fbc587ca5aabc09ee
Source: Cattle-off network

[Programming problem] calculate the cumulative sum of squares .
Heat index: 361 Time limit: C / C ++ 1 second, 2 seconds space limitations other languages: C / C ++ 32M, 64M other languages

Int type given a 32-bit integer, we define the following operation, whichever is the decimal digits of sum of squares, and repeating this operation. If the result of a particular operation is obtained after completion of 1, it returns true; otherwise continue until proven never get the result is 1, returns false
the INPUT: 19

output:true

the reason:

1^2 + 9^2=82

8^2 + 2^2 = 68

6^2 + 8^2 = 100

1^2 + 0^2 + 0^2 = 1

Link: https: //www.nowcoder.com/questionTerminal/c8cfc98711a14a3fbc587ca5aabc09ee
Source: Cattle-off network

Description Input:
Input a m (1 <= m <= 1000), indicates the number of query groups.

Subsequently m rows, each line is an 32-bit integer type int.

Description Output:
For each query, if satisfied subject description, the output "true", and vice versa outputs "false" (do not print marks)
Example 1
Input
2
. 19
. 7
outputs
to true
to true

Analytical title

Enter a number to determine how cumulative square and not 1? If after a sum of squares and a current value is not recorded, and is repeated n times after the first value, then it represents the sum of squares of the number of possible 1.

#include<iostream>
#include<vector>
#include<list>


using namespace std;

bool Find(int x, list<int> List)
{
	for (list<int>::iterator iter = List.begin(); iter != List.end(); iter++)
	{
		if (*iter == x)
		{
			return true;
		}
	}
	return false;
}
bool isOne(int num, int(*lib)[2], list<int> List)
{
	if (num == 1) return true;
	//这里最重要的就是判断什么时候是false,其实就是循环了
	//每次判断list中是否已经含有当前数字,如果有,那就不可能为1了;
	if (Find(num, List))
	{
		return false;
	}
	//并且插入num
	List.push_back(num);
	int n = 0;
	while (num > 0){
		int tmp = num % 10;
		n += lib[tmp][1];
		num /= 10;
	}
	return isOne(n, lib, List);
}

int main()
{
	//建立一个二维数组,提高后面平方的运算速度
	int (*lib)[2] = new int[10][2];
	for (int i = 0; i < 10; i++) {
		lib[i][0] = i;
		lib[i][1] = i*i;
	}
	//输入数据
	int n;
	int x;
	vector<int> arr;
	while (cin >> n)
	{
		//扩容否则无法存放输入数据
		arr.resize(n);
		for (int i = 0; i < n; i++)
		{
			cin >> x;
			arr[i] = x;
		}
		//判断输入数据
		for (int i = 0; i < n; i++)
		{
			list<int> List;
			if (isOne(arr[i], lib, List))
			{
				cout << "true" << endl;
			}
			else
			{
				cout << "false" << endl;
			}
		}
	}
	system("pause");
	return 0;
}
Published 33 original articles · won praise 102 · Views 3990

Guess you like

Origin blog.csdn.net/famur/article/details/105049968