当前位置:网站首页>第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组
第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组
2022-07-03 19:18:00 【股_四】
//4位卡普雷卡数
int main()
{
int a, b, c, x, y;
printf(" 4位卡普雷卡数有:");
c = (int)sqrt(1000);
for (b = c + 1; b <= 99; b++) // 枚举2位数b
{
a = b * b;
x = a / 100; // a 分为前后两个2公
y = a % 100; // 分段和条件检验
if( y >= 10 && b == (x + y))
printf("%d, ", a);
}
return 0;
}
结果:
//搜索偶数位卡普雷卡数
int main()
{
double a, b, m, w, x, y;
int k, n; long c, d;
printf("请输入偶数n(n≤14) :");
scanf("%d", &n);
if (n % 2 > 0)
{
printf(" 请输入偶数!\n");
return 0;
}
printf(" % d位卡普雷卡数有: \n", n);
for (m = 1, k = 2;k <= n; k++)m *= 10;
for (w = 1, k = 1 ; k <= n / 2; k++)w *= 10;
c = (long)pow(m, 0.5);
d = (long)pow(10 * m - 1, 0.5); // 求出枚举上循环的起点与终点
for (b = c + 1; b <= d; b++)
{
a = b * b;
x = floor(a / w);
y = fmod(a, w);//n位平方数a分为前后数x、y
if (b == x + y && y >= w / 10) //分段和条件检验
printf(" %.0f= (%.0f+%.0f)^2 \n",a,x,y);
}
return 0;
}
结果:
//搜索n位2段和平方数
int main()
{
double a, b, m, w, x, y;
long c, d; int k, n, s = 0;
printf(" 请输入正整数n (3<n<16:");
scanf(" %d", &n);
for (m = 1, k = 2; k <= n; k++) m *= 10;
c = (long)pow(m, 0.5);
d = (long)pow(10 * m - 1, 0.5);
for (b = c + 1; b <= d; b++)
{
a = b * b;
w = 1;
for (k = 1; k <= n - 1; k++)
{
w *= 10;
x = floor(a / w);
y = fmod(a, w);
if (b == x + y && y > 0)
{
s++;
printf(" % .0f = ( % .0f + % .0f) ^ 2\n", a, x, y);
}
}
}
if (s > 0)
printf("共%d个%d 位2段和平方数 \n", s, n);
else printf("没有 % d位2段和平方数。 \n", n) ;
}
结果:
//m位不含0的巧妙平方数
int main()
{
int k, m, n, i, x, b[10];
long t, t1, t2, y, d, f;
printf(" 把 1,2,..,9这9个数字选m个, ");
printf(" 组合成没有重复数字的平方数。\n");
printf(" 请输入平方数的位数m:");
scanf("%d", &m);
n = 0;
t = 1;
for (y = 1; y <= m - 1; y++)
t = t * 10;
t1 = (long)sqrt(t);
t2 = (long)sqrt(10 * t);
for (y = t1 + 1; y <= t2; y++)
{
f = y * y;
d = f;
for (i = 1; i <= 9; i++)
b[i] = 0;
for (x = 0, i = 1; i <= m; i++)
{
k = f % 10;
f = f / 10;
if (k == 0 || (++b[k] > 1))
{
x = 1;
break;
}
}
if (x == 0)
{
n++;
printf(" %1d=%d^2", d, y);
if (n % 3 == 0) printf("\n");
}
}
printf(" \n 共%d个\n", n);
return 0;
}
结果:
int main()
{
int k, m, n, t, f[10];
long a, b, c, d, w;
printf(" 数字2、3、5、6、7、8、9的全排列中的平方数:\n");
n = 0;
b = (long)sqrt(2356789);
c = (long)sqrt(9876532);
for (a = b; a <= c; a++)
{
d = a * a; w = d; //确保d为平方数
for (k = 0; k <= 9; k++) f[k] = 0;
while (w > 0)
{
m = w % 10;
f[m]++;
w = w / 10;
}
for (t = 0, k = 1; k <= 9; k++)
if (f[k] > 1) t = 1; //测试三个平方数是否有重复数字
if (t == 0 && f[0] + f[1] + f[4] == 0) //测试平方数中没有数字0、1、4
{
n++;
printf(" %2d:", n);
printf(" %1d=%1d 2 \n", d, a);
}
}
printf(" 共可组成以上%d个", n);
return 0;
}
结果:
//求指定区间内的勾股数组
int main()
{
int a, b, n;
long x, y, z, d;
printf(" 请输入区间[a,6]的上下限a,b: ");
scanf("%d, %d", &a, &b);
printf(" 区间[%d,%d]中的勾股数组有:\n",a,b);
n=0;
for (x=a;x<=b-2;x++)
for (y = x + 1; y <= b - 1; y++)
{
d = x * x + y * y;
z = sqrt(d); //2为x,的平方和开平方
if (z > b) break;
if (z * z == d) //满足勾股数条件时输出
{
n++;
printf(" %ld^2+%ld^2=%1d^2 \n", x, y, z);
}
}
printf(" 共%d 组勾股数", n);
return 0;
}
结果:
//,求指定区间内的倒立勾股数组
int main()
{
int a, b, n;
long x, y, z;
printf(" 请输入区间[a,6]的上下限a,b: ");
scanf("%d,%d", &a, &b);
printf(" 区间[%d,%d]中的勾股数组有:\n",a,b);
n=0;
for(z = a; z <= b -2; z++)
for(y = z + 1;y <=b - 1; y++)
for(x = y + 1;x <= b;x++)
if(z*z*( x * x + y * y) == x*x*y*y)
{
n++;
printf(" 1/%ld^2+1/%ld^2=1/%1d^2 \n", x, y, z);
}
printf(" 共%d 组倒立勾股数", n);
return 0;
}
结果:
边栏推荐
- Succession of flutter
- Luogu-p1107 [bjwc2008] Lei Tao's kitten
- Thinking about festivals
- Scrapy爬虫框架
- 【光学】基于matlab涡旋光产生【含Matlab源码 1927期】
- Understanding of database architecture
- Summary of learning materials and notes of Zhang Fei's actual combat electronics 1-31
- FBI warning: some people use AI to disguise themselves as others for remote interview
- Flume learning notes
- math_泰勒公式
猜你喜欢
Basic principle of LSM tree
235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】
记录在模拟器中运行flutter时报的错
PyTorch中在反向传播前为什么要手动将梯度清零?
[wallpaper] (commercially available) 70 wallpaper HD free
The necessity of lean production and management in sheet metal industry
Driveseg: dynamic driving scene segmentation data set
Free hand account sharing in September - [cream Nebula]
利用可视化结果,点击出现对应的句子
【疾病识别】基于matlab GUI机器视觉肺癌检测系统【含Matlab源码 1922期】
随机推荐
SSM整合-前后台协议联调(列表功能、添加功能、添加功能状态处理、修改功能、删除功能)
How does if ($variable) work? [repeat] - how exactly does if ($variable) work? [duplicate]
Ego planner code parsing Bspline_ Optimizer section (2)
我們做了一個智能零售結算平臺
Scrape crawler framework
Thesis study - 7 Very Deep Convolutional Networks for Large-Scale Image Recognition (3/3)
Basic principle of LSM tree
The more you talk, the more your stupidity will be exposed.
How to build an efficient information warehouse
High concurrency Architecture - read write separation
东数西算拉动千亿产业,敢啃“硬骨头”的存储厂商才更有机会
This Chinese numpy quick look-up table is too easy!
Nous avons fait une plateforme intelligente de règlement de détail
【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁
The most valuable thing
application
I didn't cancel
[academic related] how to find the innovation of top papers? Chinese universities won the CVPR Best Student Thesis Award for the first time
The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking
EGO Planner代碼解析bspline_optimizer部分(1)