| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2678 人关注过本帖
标题:输入一个正整数N,要求满足题目条件
取消只看楼主 加入收藏
bc121189971
Rank: 2
来 自:深圳
等 级:论坛游民
帖 子:29
专家分:36
注 册:2010-9-27
结帖率:75%
收藏
已结贴  问题点数:10 回复次数:2 
输入一个正整数N,要求满足题目条件
题目的要求是输入一个正整数N,范围在0~100000000(1亿)之间,系统再将N转换成另一个正整数Q,并输出Q的数值

而Q的数值的满足条件为:Q每一位的数值上的数字连乘结果要等于N本身,且Q要取满足该条件的最小正整数!

例如我输入N为10,则系统会生成52,25两个数字
最后输出Q为较小的正整数25!

现在贴出我所实现的代码:
程序代码:
#include<stdio.h>
void main()
{
    int n,bmp;
    int a[30],i,j,k,t=1;
    int num=0,num2=0,num3=0,num5=0,num7=0;


    for (;;i++)
    {
        num=0;num2=0;num3=0;num5=0;num7=0;         
begin:
    scanf("%d",&n);
    if ( n>1000000000 || n<=0 )                       
    {
        printf("ERROR!&Ouml;&Oslash;&ETH;&Acirc;&Ecirc;&auml;&Egrave;&euml;!\n");
        goto begin;
    }

    else if (n>0&&n<10)                           
    {
        printf("Q&micro;&Auml;&Ecirc;&yacute;&Ouml;&micro;&Icirc;&ordf;1%d!\n\n",n);
        goto exit;
    }




   
    else if (n>=10)                                    
     {

        for (;;i++)                           
        {
            if ( n%2==0 && n!=2 )
            {num2+=1;n/=2;
            }
            else if ( n%3==0 && n!=3 )
            {num3+=1;n/=3;
            }
            else if ( n%5==0 && n!=5 )
            {num5+=1;n/=5;
            }
            else if ( n%7==0 && n!=7 )
            {num7+=1;n/=7;
            }
           
            if (n==2)                               
            {num2+=1;break;
            }
            if (n==3)
            {num3+=1;break;
            }
            if (n==5)
            {num5+=1;break;
            }
            if (n==7)
            {num7+=1;break;
            }
           
            if ( n%2!=0 &&
                 n%3!=0 &&
                 n%5!=0 &&
                 n%7!=0 )                           
            {printf("&Icirc;&THORN;&frac12;&acirc;!");goto exit;
            }
        }
    }


    

    for (i=0;i<30;i++) a[i]=1;                      

    for (i=0;i<(num+num2);i++) a[i]=2;              
    num+=num2;
    for (i=num;i<(num+num3);i++) a[i]=3;           
    num+=num3;
    for (i=num;i<(num+num5);i++) a[i]=5;           
    num+=num5;
    for (i=num;i<(num+num7);i++) a[i]=7;            




    for (k=0;k<30;k++)
    {
        if ((a[k]*a[k+1])<10)
        {
            a[k+1]=a[k+1]*a[k];
            a[k]=1;
            t++;
        }
        if ( (a[k]*a[k+1])>=10 && t>0 )
        {
            for (i=0;i<29;i++)
            {
                for (j=0;j<29-i;j++)
                {
                    if (a[j]>a[j+1])
                    {
                        bmp=a[j];
                        a[j]=a[j+1];
                        a[j+1]=bmp;
                    }
                    else
                        continue;
                }
            }
        }
    }

//    for (i=0;i<30;i++) printf("%2d",a[i]);   
//    printf("\n\n");


    printf("Q&micro;&Auml;&Ecirc;&yacute;&Ouml;&micro;&Icirc;&ordf;");
    for (i=0;i<30;i++)
    {
        if (a[i]==1) continue;
        printf("%d",a[i]);
       
    }
    printf("!\n\n");
   
}


exit:
    printf("\n");
   
}

基本的N数值都可以正常输出,但是还有一些比较纠结的问题
像我输入100000000与1000000000等数值就会产生错误的数值Q

换句话说,我代码中的冒泡排序出了点错误

麻烦高手解惑!
搜索更多相关主题的帖子: 整数 条件 输入 
2010-10-25 13:40
bc121189971
Rank: 2
来 自:深圳
等 级:论坛游民
帖 子:29
专家分:36
注 册:2010-9-27
收藏
得分:0 
#include<stdio.h>
void main()
{
    int n,bmp;
    int a[30],i,j,k,t=1;
    int num=0,num2=0,num3=0,num5=0,num7=0;


    for (;;i++)
    {
        num=0;num2=0;num3=0;num5=0;num7=0;           //&Ograve;ò×&Oacute;&Ccedil;&aring;&Aacute;&atilde;
begin:
    scanf("%d",&n);
    if ( n>1000000000 || n<=0 )                      //&acute;í&Icirc;ó&Ecirc;&auml;&Egrave;&euml;&Aring;&ETH;&para;&Iuml;
    {
        printf("ERROR!&Ouml;&Oslash;&ETH;&Acirc;&Ecirc;&auml;&Egrave;&euml;!\n");
        goto begin;
    }

    else if (n>0&&n<10)                              //&Ograve;&raquo;&Icirc;&raquo;&Ecirc;&yacute;&Aring;&ETH;&para;&Iuml;
    {
        printf("Q&micro;&Auml;&Ecirc;&yacute;&Ouml;&micro;&Icirc;&ordf;1%d!\n\n",n);
        goto exit;
    }



/*&Ograve;ò×&Oacute;&sup2;&para;&raquo;&ntilde;&sup2;&iquest;·&Ouml;*/
   
    else if (n>=10)                                     //&Otilde;&yacute;&Egrave;·&Ecirc;&auml;&Egrave;&euml;&Aring;&ETH;&para;&Iuml;
     {
//&sup2;&para;&raquo;&ntilde;&Ograve;ò×&Oacute;
        for (;;i++)                           
        {
            if ( n%2==0 && n!=2 )
            {num2+=1;n/=2;
            }
            else if ( n%3==0 && n!=3 )
            {num3+=1;n/=3;
            }
            else if ( n%5==0 && n!=5 )
            {num5+=1;n/=5;
            }
            else if ( n%7==0 && n!=7 )
            {num7+=1;n/=7;
            }
            
            if (n==2)                                //&sup2;&para;&raquo;&ntilde;&Ouml;&Otilde;&Ouml;&micro;
            {num2+=1;break;
            }
            if (n==3)
            {num3+=1;break;
            }
            if (n==5)
            {num5+=1;break;
            }
            if (n==7)
            {num7+=1;break;
            }
            
            if ( n%2!=0 &&
                 n%3!=0 &&
                 n%5!=0 &&
                 n%7!=0 )                            //&Iacute;&Euml;&sup3;&ouml;&Aring;&ETH;&para;&Iuml;
            {printf("&Icirc;&THORN;&frac12;&acirc;!");goto exit;
            }
        }
    }
//    printf("2=%d  3=%d  5=%d  7=%d\n",num2,num3,num5,num7);  &Ntilde;é&Ouml;¤&Ograve;ò×&Oacute;&sup2;&para;&raquo;&ntilde;&Ecirc;&Ccedil;·&ntilde;&sup3;&Eacute;&Aacute;&cent;!


   
/*&Ograve;ò×&Oacute;&ETH;&acute;&Egrave;&euml;&sup2;&iquest;·&Ouml;*/

    for (i=0;i<30;i++) a[i]=1;                       //&Ecirc;&yacute;×é&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;

    for (i=0;i<(num+num2);i++) a[i]=2;               //&ETH;&acute;&Egrave;&euml;&Ograve;ò×&Oacute;2
    num+=num2;
    for (i=num;i<(num+num3);i++) a[i]=3;             //&ETH;&acute;&Egrave;&euml;&Ograve;ò×&Oacute;3
    num+=num3;
    for (i=num;i<(num+num5);i++) a[i]=5;             //&ETH;&acute;&Egrave;&euml;&Ograve;ò×&Oacute;5
    num+=num5;
    for (i=num;i<(num+num7);i++) a[i]=7;             //&ETH;&acute;&Egrave;&euml;&Ograve;ò×&Oacute;7


   
//    for (i=0;i<30;i++) printf("%2d",a[i]);           // &Ntilde;é&Ouml;¤&Ograve;ò×&Oacute;&Ecirc;&Ccedil;·&ntilde;&Otilde;&yacute;&Egrave;·&ETH;&acute;&Egrave;&euml;
//    printf("\n\n");


//************************&Ograve;&Ocirc;&Eacute;&Iuml;&acute;ú&Acirc;&euml;&iquest;é&Ocirc;&Euml;&ETH;&ETH;&micro;÷&Ecirc;&Ocirc;&Iacute;ê±&Iuml;!************************//
   


/*&Ograve;ò×&Oacute;&ordm;&Iuml;&sup2;&cent;&sup2;&iquest;·&Ouml;*/

//&Ograve;ò×&Oacute;&ordm;&Iuml;&sup2;&cent;&iquest;é&micro;÷&Ecirc;&Ocirc;&Ouml;&ETH;......
    for (k=0;k<30;k++)
    {
        if ((a[k]*a[k+1])<10)
        {
            a[k+1]=a[k+1]*a[k];
            a[k]=1;
            t++;
        }
        if ( (a[k]*a[k+1])>=10 && t>0 )
        {
            for (i=0;i<29;i++)
            {
                for (j=0;j<29-i;j++)
                {
                    if (a[j]>a[j+1])
                    {
                        bmp=a[j];
                        a[j]=a[j+1];
                        a[j+1]=bmp;
                    }
                    else
                        continue;
                }
            }
        }
    }

//    for (i=0;i<30;i++) printf("%2d",a[i]);   
//    printf("\n\n");


//&Aacute;&ETH;&frac34;&Ugrave;&Ograve;ò×&Oacute;&sup2;&cent;&Eacute;ú&sup3;&Eacute;&Ecirc;&yacute;&Ouml;&micro;Q
    printf("Q&micro;&Auml;&Ecirc;&yacute;&Ouml;&micro;&Icirc;&ordf;");
    for (i=0;i<30;i++)
    {
        if (a[i]==1) continue;
        printf("%d",a[i]);
        
    }
    printf("!\n\n");
   
}


exit:
    printf("\n");
   
}




 貌似论坛的代码不支持中文~     重新发一次~

to be or not to be
2010-10-25 13:42
bc121189971
Rank: 2
来 自:深圳
等 级:论坛游民
帖 子:29
专家分:36
注 册:2010-9-27
收藏
得分:0 
要上课了

==下午再发一次编码正常的...
程序代码:
#include<stdio.h>
void main()
{
    int n,bmp;
    int a[30],i,j,k,t=1;
    int num=0,num2=0,num3=0,num5=0,num7=0;


    for (;;i++)
    {
        num=0;num2=0;num3=0;num5=0;num7=0;           //因子清零
begin:
    scanf("%d",&n);
    if ( n>1000000000 || n<=0 )                      //错误输入判断
    {
        printf("ERROR!重新输入!\n");
        goto begin;
    }

    else if (n>0&&n<10)                              //一位数判断
    {
        printf("Q的数值为1%d!\n\n",n);
        goto exit;
    }


/*因子捕获部分*/
    else if (n>=10)                                 //正确输入判断
     {
//捕获因子
        for (;;i++)                           
        {
            if ( n%2==0 && n!=2 )
            {num2+=1;n/=2;
            }
            else if ( n%3==0 && n!=3 )
            {num3+=1;n/=3;
            }
            else if ( n%5==0 && n!=5 )
            {num5+=1;n/=5;
            }
            else if ( n%7==0 && n!=7 )
            {num7+=1;n/=7;
            }
           
            if (n==2)                                //捕获终值
            {num2+=1;break;
            }
            if (n==3)
            {num3+=1;break;
            }
            if (n==5)
            {num5+=1;break;
            }
            if (n==7)
            {num7+=1;break;
            }
           
            if ( n%2!=0 &&
                 n%3!=0 &&
                 n%5!=0 &&
                 n%7!=0 )                            //退出判断
            {printf("?T?a!");goto exit;
            }
        }
    }
//    printf("2=%d  3=%d  5=%d  7=%d\n",num2,num3,num5,num7);  //验证因子捕获是否成立!


   
/*因子写入部分*/

    for (i=0;i<30;i++) a[i]=1;                       //数组初始化

    for (i=0;i<(num+num2);i++) a[i]=2;               //写入因子2
    num+=num2;
    for (i=num;i<(num+num3);i++) a[i]=3;             //写入因子3
    num+=num3;
    for (i=num;i<(num+num5);i++) a[i]=5;             //写入因子5
    num+=num5;
    for (i=num;i<(num+num7);i++) a[i]=7;             //写入因子7


   
//    for (i=0;i<30;i++) printf("%2d",a[i]);         // 验证因子是否正确写入
//    printf("\n\n");



/*因子合并部分*/
    for (k=0;k<30;k++)
    {
        if ((a[k]*a[k+1])<10)
        {
            a[k+1]=a[k+1]*a[k];
            a[k]=1;
            t++;
        }
        if ( (a[k]*a[k+1])>=10 && t>0 )
        {
            for (i=0;i<29;i++)
            {
                for (j=0;j<29-i;j++)
                {
                    if (a[j]>a[j+1])
                    {
                        bmp=a[j];
                        a[j]=a[j+1];
                        a[j+1]=bmp;
                    }
                    else
                        continue;
                }
            }
        }
    }

//    for (i=0;i<30;i++) printf("%2d",a[i]);   
//    printf("\n\n");


//列举因子并生成数值Q
    printf("Q的数值为");
    for (i=0;i<30;i++)
    {
        if (a[i]==1) continue;
        printf("%d",a[i]);
       
    }
    printf("!\n\n");
   
}


exit:
    printf("\n");
   
}







[ 本帖最后由 bc121189971 于 2010-10-25 16:09 编辑 ]

to be or not to be
2010-10-25 13:44
快速回复:输入一个正整数N,要求满足题目条件
数据加载中...
 
   



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

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