[Huawei machine trial exercise] 58. Search for the number of homogeneous numbers.
Question
Description:
Find the number of homogeneous numbers between 1 and n. The homogeneous number is a set of numbers: it appears on the right side of the number of operators. For example, 5 is the number on the Right of 25, 25 is the number on the Right of 625, and 5 and 25 are homogeneous numbers.
Detailed description:
Interface Description
Prototype:
intSearchSameConstructNum(int n);
Input parameters:
Int n: finds all homogeneous numbers between 1 and n.
Return Value:
Int: Number of homogeneous numbers between 1 and n
Exercise phase:
Elementary
Code
/* ------------------------------------- * Date: 2015-07-05 * Author: SJF0115 * question: Number of homogeneous numbers * Source: Huawei machine trial exercise question author */# include <iostream> # include "OJ. h "# include <vector> # include <cmath> # include <algorithm> using namespace std;/* function: Find the number of homogeneous numbers between 1 and n. Input: int n: returns the number of all homogeneous numbers between 1 and n: int: 1 to n x/int SearchSameConstructNum (int n) {int square, tmp; int count = 0; for (int I = 1; I <= n; ++ I) {squ Are = I * I; tmp = I; while (tmp) {if (square % 10! = Tmp % 10) {break;} // if square/= 10; tmp/= 10;} // while if (tmp = 0) {++ count ;} // if} // for return count ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.