當前位置:首頁 » 編程語言 » c語言求11000所有同構數
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言求11000所有同構數

發布時間: 2022-10-24 15:02:59

❶ 找出1-1000的同構數

public class test {
public static void main(String[] args) {
//這是個很復雜很笨的辦法,不想去%10,麻煩,直接這樣寫吧,這是把數看出一個字元串來對待
for(int x=0;x<=1000;x++){
int y=x*x;
String x1=String.valueOf(x);
String y1=String.valueOf(y);
if(x1.equals(y1.substring(y1.length()-x1.length(), y1.length())))
System.out.print(x+" ");
}}}

c語言求同構數

1、演算法分析:
對指定范圍內的每一個整數a,求出其平方,判斷a是否是其平方的尾部,通常有以下方法:
通過字元串子串比較找出同構數。
為了求更高位數的同構數,可應用同構數的以下性質:一個m位同構數的尾部m-1位數也是一個同構數。道理很簡單

,a是一個m位數,a的平方數尾部的m-1位僅由a尾部的m-1位決定而與a的最高位無關。
易知一個同構數有三個:1,5,6,則二位同構數的個位數字只可能是1,5,6這三個數字。
根據這一思路,我們可應用遞推求出多位同構數串。

2.求指定區間內的同構數
程序代碼如下:
#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); /*相等就是同構數,輸出結果*/
}
}
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;
}

❸ 求1000以內的同構數

PrivateSubCommand1_Click()
DimiAsInteger'求1000以內的同構數。
Fori=1To100
Ifi=Val(Right(Str(i*i),Len(i)))ThenPrinti
'IfStr(i)=Str(Right(Str(i*i),Len(i)))ThenPrinti'這樣也行。都str轉一下再比較相等
Next
EndSub
輸出12576

❹ C語言編程求出1~1000的同構數

#include"stdio.h"

int main()
{
long i,j,k;
k=10;
for (i=1;i<=10000;i++)
{
if (i==k) k*=10;
j=i*i;
if(j%k==i) printf("%ld\t%ld\n",i,j);
}
return 0;
}

❺ c語言編程:找出1到1000的全部同構數

#include<stdio.h>
intIsTongGou(inta){//判斷是否同構
intb=a*a;
while(a!=0){
if(b%10!=a%10){
return0;
}else{
b=b/10;
a=a/10;
}
}
return1;
}
intmain(){
for(inti=1;i<1000;i++){
if(IsTongGou(i)){
printf("%d",i);
}
}
printf(" ");
}

❻ C程:求1到100內的所有同構數

for循環中沒有上限,有3種可能m<i*100,m=i*100,m>i*100,可以改變條件還有break的位置不對應該是if(n==m*m&&n==m+i*1000){printf("%d\n",m);break;}return0;前的break;去掉

❼ c語言 打出1~1000所有的同構數

#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;
}

❽ 用C++編一個1~1000的同構數

把一個數的平方計算出來,再變成字元串比較字元串的末幾位如果一樣就說明是同構數。這個方法更直觀。

#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,1000]中的同構數:\n");
for(a=1;a<=1000;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); /*相等就是同構數,輸出結果*/
}
}
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;
}