标题:新手,刚写了段代码,结果和预想的不一样,求大神指点
取消只看楼主
t_waiwai
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-10-25
结帖率:0
已结贴  问题点数:20 回复次数:1 
新手,刚写了段代码,结果和预想的不一样,求大神指点
//    标题:拼接平方数

//    小明发现49很有趣,首先,它是个平方数。它可以拆分为4和9,拆分出来的部分也是平方数。169也有这个性质,我们权且称它们为:拼接平方数。

//    100可拆分1 00,这有点勉强,我们规定,0 00 000 等都不算平方数。

//    小明想:还有哪些数字是这样的呢?

//    你的任务出现了:找到某个区间的所有拼接平方数。

//【输入格式】
//两个正整数  a b (a<b<10^6)

//【输出格式】
//若干行,每行一个正整数。表示所有的区间[a,b]中的拼接平方数

//例如:
//输入:
//1 200

//程序应该输出:
//49
//169

//再例如:
//输入:
//169 10000

//程序应该输出:
//169
//361
//1225
//1444
//1681
//3249
//4225
//4900
//9025
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("输入");
            int a = Convert.ToInt16(Console.ReadLine());
            int b = Convert.ToInt16(Console.ReadLine());
            int i = 0;
          //  Console.WriteLine ( Math.Sqrt(a)%1 );
            
            for ( i = a; i <= b; i++)
            {
                if (i < 10)
                {
                    continue;               
                }
                if ((Math.Sqrt(i) % 1) != 0)
                {
                    continue;
                }               

                if (i <100)   //2位数只要拆成2个数字
                {
                    int a1 = i % 10;                //个位数   
                    int a2 = ((i % 100) - a1) / 10;          //十位数   
                    //Console.WriteLine(a1);
                    //Console.WriteLine(a2);
                    if ((Math.Sqrt(a1) % 1 == 0) && (Math.Sqrt(a2) % 1 == 0))
                    {
                        Console.WriteLine(i);
                    }
                }
                else if (i < 1000) //3位数要考虑个位1个,十位百位一组,个位十位1组,百位1组
                {
                    int a1 = i % 10;
                    int a2 = ((i % 100) - a1)/10;
                    int a3 = ((i % 1000 )- a2*10-a1)/100;
                    if(a1==0 && a2==0)  //将结尾是00的数去掉
                    {
                        continue;
                    }
                    if (((Math.Sqrt(a1) % 1 == 0) && (Math.Sqrt(a2 + a3 * 10) % 1 == 0)) || ((Math.Sqrt(a3) % 1 == 0) && (Math.Sqrt(a1 + a2 * 10) % 1 == 0)) )
                    {
                        Console.WriteLine(i);
                    }
                }
                else if (i < 10000)  //4位数要考虑(1)1位数1组,十百千为1组 (2)个十位数1组,百千位1组 (3)个十百位1组,千位1组
                {
                    int a1 = i % 10;
                    int a2 = ((i % 100) - a1) / 10;
                    int a3 = ((i % 1000) - a2 * 10 - a1) / 100;
                    int a4 = (i % 10000 - a3 * 100 - a2 * 10 - a1) / 1000;
                    //去掉后3位为0的情况
                    if (a1 == 0 && a2 == 0 && a3 == 0)//x000
                    {
                        continue;
                    }
                     if ( a3==0 && a2==0 && a1 !=0 )//x00x的情况  为什么没有1001,4004,9009??
                    {
                       // Console.WriteLine("x00x");
                        if (Math.Sqrt(a1) % 1 == 0 && Math.Sqrt(a4 * 100) % 1 == 0)//x00  x
                        {
                            Console.WriteLine(i);
                        }
                    }
                     if ( a3 == 0 && a2!=0 && a1==0)//x0x0
                    {
                      //  Console.WriteLine("x0x0");
                        if ( (Math.Sqrt(a4 * 10) % 1 == 0 && Math.Sqrt(a2 * 10) % 1 == 0) )//x0 x0
                        {
                            Console.WriteLine(i);
                        }
                    }
                     if ( a3 == 0 && a2!=0 && a1!=0    )//x0xx
                    {
                       // Console.WriteLine("x0xx");
                        if ( (Math.Sqrt(a4 * 100 + a2) % 1 == 0 && Math.Sqrt(a1) % 1 == 0) || (Math.Sqrt(a4*10)%1==0 &&Math.Sqrt(a2*10+a1)%1==0) )//x0x  x,   x0 xx
                        {
                            Console.WriteLine(i);
                        }
                    }
                     if ( a3 != 0 && a2==0 && a1==0 )//xx00
                    {
                       // Console.WriteLine("xx00");
                        if (Math.Sqrt(a4)%1==0 && Math.Sqrt(a3*100)%1==0)      //x  x00
                        {
                            Console.WriteLine(i);
                        }
                    }
                     if(a3!=0 && a2==0 && a1!=0)  //xx0x
                    {
                      //  Console.WriteLine("xx0x");
                        if ((Math.Sqrt(a4) % 1 == 0 && Math.Sqrt(a3 * 100 + a1) % 1 == 0) || (Math.Sqrt(a4 * 100 + a3 * 10) % 1 == 0 && Math.Sqrt(a1) % 1 == 0))//x x0x ,xx0 x
                        {
                            Console.WriteLine(i);
                        }
                    }
                     if (a3 != 0 && a2 != 0 && a1 != 0)//xxx0
                    {
                      //  Console.WriteLine("xxx0");
                        if ((Math.Sqrt(a4) % 1 == 0 && Math.Sqrt(a3 * 100 + a2 * 10) % 1 == 0) || (Math.Sqrt(a4 * 10 + a3) % 1 == 0 && Math.Sqrt(a2 * 10) % 1 == 0))  //x xx0, xx x0
                        {
                            Console.WriteLine(i);
                        }
                    }
                     if(a3!=0 && a2!=0 && a1!=0)//xxxx
                    {
                       // Console.WriteLine("xxxx");
                        if( (Math.Sqrt(a4)%1==0 && Math.Sqrt(a3*100+a2*10+a1)%1==0) ||  (Math.Sqrt(a4*10+a3)%1==0 && Math.Sqrt(a2*10+a1)%1==0) || (Math.Sqrt(a4*100+a3*10+a2)%1==0 && Math.Sqrt(a1)%1==0) )//x xxx,   xx  xx,    xxx x
                        {
                            Console.WriteLine(i);
                        }
                    }
                    
                }              
                  
            }

        }
    }
搜索更多相关主题的帖子: 正整数 拼接 
2016-10-25 14:47
t_waiwai
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-10-25
得分:0 
结果中没有我想象中的1001,4004,9009,9900等等
2016-10-25 14:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-470242-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.158989 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved