C语言求教输入两个正整数m和n(m≥100,n<1000),输出m和n之间的满足如下条件的三位正整数

输入两个正整数m和n(m≥100,n<1000),输出m和n之间的满足如下条件的三位正整数:它是某整数的平方,它的三位数码有两位是相同的。(如100是10的平方,它有两个0,225是15的平方,它有两个2),每行输出4个,字符宽度为5个左对齐。

如输入:(下划线部分表示输入)

Input m: 100

Input n: 500

则输出

100 121 144 225

400 441 484

/*

m n = 100 1000

100 121 144 225 400 441 484 676 900

Press any key to continue

*/

#include <stdio.h>
#include <math.h>

int hasRepNum(int n) {
int a,b,c;
if(n < 100 || n > 1000) return 0;
a = n % 10;
n /= 10;
b = n % 10;
n /= 10;
c = n % 10;
if(a == b || b == c || c == a) return 1;
return 0;
}

int main() {
int i,m,n,t;
printf("m n = ");
scanf("%d%d",&m,&n);
for(i = m; i <= n; ++i) {
t = (int)sqrt(i);
if(t * t == i && hasRepNum(i))
printf("%d ",i);
}
printf("\n");
return 0;
}

追问

基本对了,就是差一点{每行输出4个,字符宽度为5个左对齐。}这个怎么弄

追答

/*

m n = 100 1000


  100  121  144  225

  400  441  484  676

  900

Press any key to continue

*/

#include <stdio.h>
#include <math.h>
 
int hasRepNum(int n) {
    int a,b,c;
    if(n < 100 || n > 1000) return 0;
    a = n % 10;
    n /= 10;
    b = n % 10;
    n /= 10;
    c = n % 10;
    if(a == b || b == c || c == a) return 1;
    return 0;
}
 
int main() {
    int i,m,n,t,cnt = 0;
    printf("m n = ");
    scanf("%d%d",&m,&n);
    for(i = m; i <= n; ++i) {
        t = (int)sqrt(i);
        if(t * t == i && hasRepNum(i)) {
if(cnt && !(cnt % 4)) printf("\n");
            printf("%5d",i);
++cnt;
}
    }
    printf("\n");
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-05-31
/*
Please input two numbers(m n):100 1000
100 121 144 225
400 441 484 676
900
*/
#include"stdio.h"
#include"math.h"
void IsNeedNumber(int m,int n)
{
int i,j=0;
for(i=m;i<n;i++)
{
if(((int)sqrt(i)*(int)sqrt(i))==i) /*判断是否是某个数的平方*/
{
if((i%10==i/10%10)||(i%10==i/100%10)||(i/10%10==i/100%10)) /*判断是否有两位相同*/
{
printf("%d ",i);
j++;
if(j%4==0)
printf("\n");
}
}
}
}
void main()
{
int m,n;
printf("Please input two numbers(m n):");
scanf("%d %d",&m,&n);
IsNeedNumber(m,n);
getch();
}