| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2366 人关注过本帖
标题:新手,刚写了段代码,结果和预想的不一样,求大神指点
只看楼主 加入收藏
t_waiwai
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-10-25
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
新手,刚写了段代码,结果和预想的不一样,求大神指点
//    标题:拼接平方数

//    小明发现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
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:10 
1001本身不是平方数哇
2016-10-25 21:04
ak1ca
Rank: 2
等 级:论坛游民
帖 子:3
专家分:20
注 册:2008-7-3
收藏
得分:10 
对的,1001,4004,9009,9900等等这些数字自己就不是平方数。建议取哪一位数字用方法实现。

public static boolean check1(int a){
        int sqrt=(int) Math.sqrt(a);
        if(a == sqrt*sqrt){
            return true;
        }else{
            return false;
        }
    }
   
    public static boolean check2(int a){
        
        int temp1=0,temp2=0,i=0;
        int[] t=new int[10];
        
        do{//循环取出各个数据位
             t[i]=a%10;
             a/=10;
             i++;
        }while(a>0);
        
        for(int j=i-1;j>0;j--){
            temp1+=t[j];
            if(!check1(temp1)){ //找到第一个平方位
                temp1*=10;
                continue;
            }
            for(int k=j-1;k>=0;k--){ //取出剩余数据
                temp2+=t[k];
                if(k==0){
                    continue;
                }
                temp2*=10;
            }
            if(temp2==0){ //过滤第二位是0 00 000 情况
                continue;
            }
            if(check1(temp2)){ //找到第二个平方位
                return true;
            }
            temp2=0;
            temp1*=10;
        }
        
        return false;
    }
   
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        int a,b;
        System.out.println("输入第一个数:");
        a=sc.nextInt();
        System.out.println("输入第二个数:");
        b=sc.nextInt();
        
        System.out.println("a="+a+","+"b="+b);
            
        for(int i=a;i<b;i++){
            
            if(i<10){
                continue;
            }
            if(!check1(i)){
                continue;
            }
            if(check2(i)){
                System.out.println(i);
            }
        }
    }
2016-10-26 11:27
yytc18194
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-7-16
收藏
得分:0 
为啥举例里面1004和1009也没有哇,这个是你代码输出的结果吗?感觉100是10的平方,然后4是3的平方,9也同理啊,为什么我自己按照代码算感觉这两个应该算啊。我这边vs2015死活装不上,没法运行代码
2016-11-11 10:32
快速回复:新手,刚写了段代码,结果和预想的不一样,求大神指点
数据加载中...
 
   



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

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