用数组编程寻找既是完全平方数,又有两位数字相同的三位正整数,例如121.144等。要求统计满足该条件的整数个数,并从大到小打印这些整数
马上开写
#include<stdio.h>
#include<math.h>
int main(){
int num[100];
int i,j,index,a,b,c;
for(i=10,j=0;i<=31;i++)
{
index=i*i;
a = index / 100;
b = (index/ 10) % 10;
c = index % 10;
if (a == b || a == c || b == c)
{
num[j]=index;
j++;
}
}
printf("个数为:%d\n",j);
for(i=0;i<j;i++)
{
printf("%d ",num[i]);
}
return 0;
}
vector<int>vv;//存放符合条件的数
int cout = 0;//符合条件个数
for (int i = 999; i >= 100; --i)
{
int m = floor(sqrt(i) + 0.5);
if (m * m == i)//完全平方数判断
{
int a = i % 10;
int b = i / 10 % 10;
int c = i / 100 % 10;
if ((a == b)||(b == c)||(c == a))//两个相同数判断
{
vv.push_back(i);
cout++;
}
}
}
printf("符合条件个数:%d\n", cout);
printf("从大到小排序:\n");
for (int j = 0; j < vv.size(); j++)
{
printf("%d\n", vv[j]);
}
#include <stdio.h>
#include <math.h>
int main()
{
int i;
int count = 0;
for (i = 999; i >= 100; i--)
{
if (i == (int)sqrt(i) * (int)sqrt(i))
{
int a = i % 10;
int b = i /10% 10;
int c = i /100% 10;
if ((a == b) + (b == c) + (c == a)==1)
{
printf("%d\n", i);
count++;
}
}
}
printf("个数:%d", count);
return 0;
}
#include <stdio.h>
int main()
{
int arr[900];
int i, j, n = 0, a, b, c;
for (i = 100; i < 1000; i++)
{
a = i / 100;
b = (i / 10) % 10;
c = i % 10;
if (a == b || a == c || b == c)
{
j = sqrt(i);
if (j * j == i)
arr[n++] = i;
}
}
printf("%d\n", n);
for (i = n - 1; i >= 0; i--)
printf("%d ", arr[i]);
代码参考:
#include <stdio.h>
#include <math.h>
int func(int);
int func(int x)
{
int i;
int count=0;
int t1,t2,t3;
for(i=101;i<=x;i++)
{
if(sqrt(i)==(int)sqrt(i))
{
t1=i%10;
t2=i/10%10;
t3=i/100;
if(t1==t2||t2==t3||t1==t3)
count++;
}
}
return count;
}
int main()
{
int x;
scanf("%d",&x);
printf("%d",func(x));
return 0;
}