Home  >  Article  >  Backend Development  >  Use C language to find isomorphic numbers from 1 to 1000

Use C language to find isomorphic numbers from 1 to 1000

王林
王林Original
2020-02-05 10:32:5810931browse

Use C language to find isomorphic numbers from 1 to 1000

Please see examples for details:

Free video tutorial recommendation: c language video tutorial

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *right(char *ms,int len); /*截取一个字符串尾部长为len个字符的子串*/
void main()
{
long a;
int len;
char as[10];
char ms[20];
printf("求[1,10000]中的同构数:\n");
for(a=1;a<=10000;a++)
{
ultoa(a,as,10); /*数a转换为字符串,存入as*/
len=strlen(as);
ultoa(a*a,ms,10);
if(strcmp(as,right(ms,len))==0) /*比较字符串as与ms长为len的子串是否相等*/
printf("%s 其平方为 %s\n",as,ms); /*相等就是同构数,输出结果*/
}
system("pause");
}
char *right(char *ms,int len) /*截取字符串尾部长为len子串的函数*/
{
int i,j;
for(i=0;i<(signed)strlen(ms);ms++);
for(j=0;j<len;j++,ms--);
return ms;
}

Related article tutorial sharing: c language tutorial

The above is the detailed content of Use C language to find isomorphic numbers from 1 to 1000. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn