abcd ab cd 2c语言,整数趣题(求具有abcd = (ab + cd)^2性质的四位数)
/****************************************
* File Name : integer.c
* Creat Data : 2015.1.24
* Author : ZY
*****************************************/
/*整數(shù)趣題*/
/*求具有abcd = (ab + cd)^2性質(zhì)的四位數(shù)*/
/*3025這個數(shù)具有一種獨特的性質(zhì):將它平分成兩段
,即30和25,使之相加后求平方和恰好等于3025本身
求具有這樣性質(zhì)的全部四位數(shù)。*/
/*方法一*/
#include
#include
int main()
{
int i,j,m,n;
printf("There are following numbers with 4 digits satisfied condition:\n");
for( i = 1;i < 10;i++ )
{
for( j = 0;j < 10;j++ )
{
for( m = 0;m < 10;m++ )
{
for( n = 0;n < 10;n++ )
{
if((i*1000 + j*100 + m*10 + n )== pow((i*10 + j + m*10 + n),2))
{
printf("%10d",i*1000 + j*100 + m*10 + n);
}
}
}
}
}
printf("\n");
return 0;
}
/*方法二*/
#include
#include
int main()
{
int a[4],i,j,k;
printf("There are following numbers with 4 digits satisfied condition:\n");
for( i = 1000 ;i < 10000;i++ )
{
for( j = 0,k = 10000;k >= 10;j++ )
{
a[j] = (i%k)/(k/10);
k /= 10;
}
if((a[0]*1000 + a[1]*100 + a[2]*10 + a[3]) == pow((a[0]*10+a[1]+a[2]*10+a[3]),2))
{
printf("%10d",a[0]*1000 + a[1]*100 + a[2]*10 + a[3]);
}
}
printf("\n");
return 0;
}
/*方法三*/
#include
int main()
{
int n,a,b;
printf("There are following numbers with 4 digits satisfied condition:\n");
for( n = 1000;n < 10000;n++ )
{
a = n/100;//截取N的前兩位存于a
b = n%100;//截取N的后兩位存于b
if((a + b)*(a + b) == n)
{
printf("%10d",n);
}
}
printf("\n");
return 0;
}
總結(jié)
以上是生活随笔為你收集整理的abcd ab cd 2c语言,整数趣题(求具有abcd = (ab + cd)^2性质的四位数)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于网络爬虫的负面信息搜集系统
- 下一篇: Python ISBN号概述,校验位计算