华为OJ:查找同构数的数量

来源:互联网 发布:淘宝卖家最便宜的快递 编辑:程序博客网 时间:2024/05/13 21:57

描述: 

  • 找出1至n之间同构数的个数。同构数是这样一组数:它出现在平方数的右边。例如:5是25右边的数,25是625右边的数,5和25都是同构数。

详细描述:

  • 接口说明

原型:

  • intSearchSameConstructNum(int n);

输入参数:

    int n:查找1至n之间的全部同构数

返回值:

        int:1至n之间同构数的个数

 



#include <stdio.h>#include <stdlib.h>#include <string.h>#include "OJ.h"/*功能:找出1至n之间同构数的个数输入:int n:查找1至n之间的全部同构数返回:int:1至n之间同构数的个数*/int SearchSameConstructNum(int n){/*在这里实现功能*/int i = 0, j = 0,k = 0, lenq = 0, lenqq = 0;int count = 0;int q = 0, qq = 0;char strq[10] = { 0 }, strqq[30] = { 0 };for (i = 1; i <= n; i++){/*判断i是否是同构数*/q = i;qq = i * i;memset(strq, 0, 10);memset(strqq, 0, 30);/*把整数转换为字符串*/sprintf_s(strq, "%d", q);sprintf_s(strqq, "%d", qq);/*求出长度*/lenq = strlen(strq);lenqq = strlen(strqq);for (j = lenq - 1, k = lenqq - 1; j >= 0; j--, k--){if (strq[j] != strqq[k]){break;}}if (j == -1){count++;}}return count;}


0 0
原创粉丝点击