异形洗脑女漫画:有没有比这道题的最佳答案更好的答案?

来源:百度文库 编辑:高考问答 时间:2024/04/29 23:54:57
http://zhidao.baidu.com/question/8544856.html
有没有按照该楼主的原有要求(除了“调用函数来进行判断”以外)的更精湛的答案?
 
谢绝没建设性的答案!

 
 
 
一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
找出所有满足如下条件的三位数:它既是完全平方数;又有两位数字相同。
一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一

因为每一位数都相同的完全平方数并不存在,所以在检测每一个三位数的完全平方数时,
只需判断该平方数有无重复的数字。 这么一来,程序可以很简短:

#include<iostream>
void main( ) {
    for( int i = 10, square = i*i; square < 1000; i++, square = i*i )
        for( char count[ 10 ] = ""; square; square /= 10 )
            if( ++count[ square % 10 ] > 1 ) std::cout << i*i << '\n';
}

严格来说,代码里的 char count[ 10 ] = ""; 应该写成 int count[ 10 ] = { 0 }; ,
但后者不符合 for 循环的初始表达式的语法,所以若坚持使用后者会增加一行代码。:)
 
 
 

void main()
{
for(int i = 10;i<=31;i++)
{
int bai,ge,shi,fang;
fang=i*i;
bai=fang/100;
shi = fang/10%10;
ge = fang%10;
if((bai==shi)||(bai==ge)||(shi==ge))
cout<<fang<<endl;
}
}

专家真是一针见血呀!

150分……汗…………

//其实,这么小的数据规模,真的要运用于大程序中的话,不如预先完成运算,也就是:

#include <iostream.h>

void main(){
int need[65535],da[9]={100,121,144,225,400,441,484,676,900},i;
for (i=0;i<9;i++)need[i]=da[9];
//for (i=0;i<9;i++)cout<<da[i]<<"\t";
}

既然楼主说用函数调用,我就写个带函数调用的。本程序在Visaual C++.net 2003下调试成功。

#include <iostream>
bool aa(const int& var)
{//判断一个三位数中是否有两位数字相同
int ent, decade, hundred;

ent = var % 10;//求个数
decade = var /10 % 10;//求十位
hundred = var /100;//求百位

if ( ((ent==decade)||(decade==hundred)||(ent==hundred)) && !((ent==decade)&&(decade==hundred)) )
{//如果只有两位数字相同(三位数字都相同的不包括在内)则返回true
return true;
}
else//否则不符合,返回false
return false;
}

int main()
{
int pow;

std::cout << "Now, show the equatation...." << std::endl;
for (int i=10; true; ++i)
{
pow = i * i;
if (pow>999)
break;

if ( aa(pow) )
std::cout << pow << " = " << i << " * " << i << std::endl;
}

return 0;
}

结果:
Now, show the equatation....
100 = 10 * 10
121 = 11 * 11
144 = 12 * 12
225 = 15 * 15
400 = 20 * 20
441 = 21 * 21
484 = 22 * 22
676 = 26 * 26
900 = 30 * 30
Press any key to continue