| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 416 人关注过本帖
标题:帮忙看看这个程序什么问题
只看楼主 加入收藏
njzhangyuhao
Rank: 2
等 级:论坛游民
帖 子:197
专家分:35
注 册:2010-11-20
结帖率:100%
收藏
 问题点数:0 回复次数:4 
帮忙看看这个程序什么问题
VC6.0环境 取十进制数所有奇数数字,用它们构成一个最小数  编译通过 输出有问题
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
long fun(long s)
{
    long d[15],n,i,j,g,temp;
    for(n=0;s!=0&&n<15;)
    {
        if(s%10%2)
            d[n++]=s%10;
        s/=10;
    }
    for(i=0;i<n-1;i++)
        for(j=i;j<n;j++)
         if(d[i]>d[j])
            {
                temp=d[i];
                d[i]=d[j];
                d[j]=temp;
            }
    for(j=0;j<n;j++)
    {
        i=d[n-j-1]*pow(10,j);
        g+=i;
    }
    return g;
}
void main()
{
    long s,d;
    FILE *fp;
    scanf("%ld",&s);
    d=fun(s);
    if((fp=fopen("myf2.out","w"))==NULL)
    {
        printf("can not open the file\n");
        exit(0);
    }
    fprintf(fp,"%ld\n",d);
    printf("%ld\n",d);
    fprintf(fp,"My exam number is : 1231231234\n");
    fclose(fp);
}
搜索更多相关主题的帖子: 十进制 
2011-03-27 23:32
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<stdlib.h>

struct Store
{
    int n;
    Store* next;

};



Store* GetNumber(long num);
Store* GetOddNumber(Store*);
int* Sort(Store*,int*);
int main(int argc,char* argv[])
{
   
    long number = 0;

    printf("Input a number:");

    scanf("%ld",&number);
   
    int n = 0;

    Store* head =GetOddNumber(GetNumber(number));
    int* array = Sort(head,&n);

    for(int i = 0;i < n;i++)
    {
        printf("%d",array[i]);
    }

    printf("\n");


    return 0;
}


///获取各位数字
Store* GetNumber(long num)
{
    if(num == 0)    return NULL;

    Store* const head = (Store*)malloc(sizeof(Store));
   
    if(head == NULL)
    {
        printf("mallocate for memory failed!\n");
        exit(1);

    }

    Store* p = head;

    while(true)
    {
        p->n = num % 10;
       
        num /= 10;

        if(num == 0)   
        {
            p->next = NULL;
            break;
        }

        p->next = (Store*)malloc(sizeof(Store));

        if(p->next == NULL)
        {
            printf("mallocate for memory failed!\n");
            exit(1);
        }

        p = p->next;
               
    }

    return head;
   
}



///获取奇数
Store* GetOddNumber(Store* pt)
{


    if(pt == NULL)    return NULL;

    int count = 0;

    Store* oldHead = pt;
   
    Store* const head = (Store*)malloc(sizeof(Store));
   
    if(head == NULL)
    {
        printf("mallocate for memory failed!\n");
        exit(1);
    }
   
    count++;

    Store* p = head;

    while(pt)
    {
        if((pt->n) % 2 == 1)
        {
            p->n = pt->n;
            p->next = (Store*)malloc(sizeof(Store));
            if(p->next == NULL)
            {
                printf("mallocate for memory failed!\n");
                exit(1);
            }

            count++;
            p = p->next;
            p->n = 0;
                       
        }

        pt = pt->next;
    }

    p->next = NULL;

    p = head;

    for(int i = 1;i < count - 1;i++)
    {
        p = p->next;
    }

    if(i == 1)
    {
        return NULL;
    }

    free(p->next);
    p->next = NULL;
   
    while(oldHead)
    {
        pt = oldHead;
        oldHead = oldHead->next;
        free(pt);
        pt = NULL;

    }

    return head;

}


//排序
int* Sort(Store* head,int* p)
{
    if(head == NULL)    return NULL;

    Store* tmp = head;

    int* array = NULL;

    int count = 0;

    while(tmp)
    {
        count++;
        tmp = tmp->next;
    }

    *p = count;

    array = (int* )malloc(count * sizeof(int));

    if(array == NULL)
    {
        printf("mallocate for memory failed!\n");
        exit(1);       
    }

    tmp = head;
    for(int i = 0;i < count;i++)
    {
        array[i] = tmp->n;
        tmp = tmp->next;
    }

    ///排序
    bool flag = true;
    for(i = 0;i < count - 1 && flag;i++)
    {
        flag = false;
        for(int j = 0;j < count - 1;j++)
        {
            if(array[j] > array[j+1])
            {
                flag = true;
                int temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
            }
        }

    }

    return array;
}

南国利剑
2011-03-28 01:44
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:0 
以上代码在vc6.0下成功运行。

南国利剑
2011-03-28 01:45
SGT_JM
Rank: 2
等 级:论坛游民
帖 子:14
专家分:10
注 册:2011-3-21
收藏
得分:0 
楼上代码流程清晰,很不错!
2011-03-28 13:20
njzhangyuhao
Rank: 2
等 级:论坛游民
帖 子:197
专家分:35
注 册:2010-11-20
收藏
得分:0 
我的错在哪呢?
2011-03-28 17:08
快速回复:帮忙看看这个程序什么问题
数据加载中...
 
   



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

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