| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 496 人关注过本帖
标题:pku acm里面的第1002题,不知道我的程序为什么能不过?
只看楼主 加入收藏
袁德凯
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2011-4-8
结帖率:57.14%
收藏
已结贴  问题点数:20 回复次数:4 
pku acm里面的第1002题,不知道我的程序为什么能不过?
// 1002.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <math.h>
#include <iostream>
using namespace std;

typedef struct HM
{
    int num,number;
    HM  *r,*l;

} hm;

int tans(char);
void inselt(int,hm *&);
void ouselt(hm*,int&);
hm* creathm(int);

int main ()
{
    int count,a[7],b;
    int m=0;
    hm *s;
    hm *gen=new hm;
    gen->num =0;
    gen->r =0;
    gen->l =0;
    gen->number=0;

    char c[50];
    cin>>count;
    for (int i=0;i<count;i++)
    {
        cin>>c;
        int aa=0;
    for(int j=0;c[j]!=NULL;j++)
    {
        if(tans(c[j])!=10)
        {
            a[aa]=tans(c[j]);
            aa++;
        }
        
    }
        b=0;
        for(int k=0;k<7;k++)
        {
            int df=pow(10.0,(6-k));
            b=b+a[k]*df;
        }

        if (gen->number ==0)
        {
            gen->number =b;
            gen->num ++;
        }
        else
        {
            s=gen;
            inselt(b,s);
        }
    }
    hm* g=gen;
    ouselt(g,m);
    if (m==0)
        cout<<"No duplicates."<<endl;
        return 0;
}

int tans(char d)
{
    if(d-'0'>=0 && d-'9'<=0)
        return d-'0';
    switch(d)
    {
    case 'A':
    case 'B':
    case 'C': return 2;
        break;

    case 'D':
    case 'E':
    case 'F': return 3;
        break;

    case 'G':
    case 'H':
    case 'I': return 4;
        break;

    case 'J':
    case 'K':
    case 'L': return 5;
        break;

    case 'M':
    case 'N':
    case 'O': return 6;
        break;

    case 'P':
    case 'R':
    case 'S': return 7;
        break;

    case 'T':
    case 'U':
    case 'V': return 8;
        break;

    case 'W':
    case 'X':
    case 'Y': return 9;
        break;

    default : return 10;
    }
}

hm* creathm(int a)
{
    hm *b=new hm;
    b->number=a;
    b->l =0;
    b->r =0;
    b->num =1;
    return b;
}




void inselt(int t,hm *&s)
{
    while(s!=0)
    {
        if( t< s->number)
        {
            if(s->l==0)
            {
                s->l=creathm(t);
                s=0;

            }
            else
                s=s->l;
        }


        else if ( t> s->number)
        {
            if(s->r==0)
            {
                s->r=creathm(t);
                s=0;
            }
            else
                s=s->r;
        }
        else
        {
            s->num++;
            s=0;
        }
    }
}


void ouselt(hm *s,int &m)
{
    if(s!=0)
    {
        ouselt(s->l,m);
        if(s->num >1)
        {
            m++;
            char rf[10];
            _itoa( s->number, rf, 10);
            char sh[10];
            for (int ii=0;ii<3;ii++)
                sh[ii]=rf[ii];
            for (int jj=3;jj<8;jj++)
                sh[jj+1]=rf[jj];
            sh[3]='-';
            cout<<sh<<" "<<s->num <<endl;
        }
        ouselt(s->r,m);
    }
   
}
搜索更多相关主题的帖子: 应用程序 include number 控制台 count 
2011-09-12 18:11
袁德凯
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2011-4-8
收藏
得分:0 
这是一段VBs代码,方便调试程序用。不过我的程序表面应该没有问题。可能是那些细节的东西没有注意到。希望有谁可以帮我指出并更正过来。
set t=wscript.createobject("wscript.shell")
set f=wscript.createobject("scripting.filesystemobject")
wscript.sleep 5000
t.sendkeys "12"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
t.sendkeys "4873279"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
t.sendkeys "ITS-EASY"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
t.sendkeys "888-4567"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
t.sendkeys "3-10-10-10"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
t.sendkeys "888-GLOP"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
t.sendkeys "TUT-GLOP"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
t.sendkeys "967-11-11"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
t.sendkeys "310-GINO"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
t.sendkeys "F101010"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
t.sendkeys "888-1200"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
t.sendkeys "-4-8-7-3-2-7-9-"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
t.sendkeys "487-3279"
wscript.sleep 500
t.sendkeys "{enter}"
wscript.sleep 500
2011-09-12 18:27
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <malloc.h>
typedef struct
{
    int * number;
    int count;
    int max;
}PhoneBook;
void HeapSort(int *a, int length)
{
    int i, p, k, k1, k2, tmp;
    for (i = 1; i < length; i++)
    {
        p = i;
        k = (i - 1) >> 1;
        while (k >= 0)
        {
            if (a[k] < a[p])
            {
                tmp = a[k];
                a[k] = a[p];
                a[p] = tmp;
            }
            p = k;
            k = (p - 1) >> 1;
        }
    }
    for (i = length - 1; i > 0; i--)
    {
        tmp = a[0];
        a[0] = a[i];
        a[i] = tmp;
        p = 0;
        k1 = (p << 1) + 1;
        k2 = k1 + 1;
        while (k1 < i)
        {
            k = (k2 < i && a[k2] > a[k1]) ? k2 : k1;
            if (a[k] < a[p]) break;
            tmp = a[p];
            a[p] = a[k];
            a[k] = tmp;
            p = k;
            k1 = (p << 1) + 1;
            k2 = k1 + 1;
        }
    }
}
int StringToNumber(char * s)
{
    int num = 0;
    int i=0;
    while(s[i] != '\0')
    {
        switch(s[i])
        {
            case '0': num *= 10; break;
            case '1': num *= 10; num += 1; break;
            case 'A':
            case 'B':
            case 'C':
            case '2': num *= 10; num += 2; break;
            case 'D':
            case 'E':
            case 'F':
            case '3': num *= 10; num += 3; break;
            case 'G':
            case 'H':
            case 'I':
            case '4': num *= 10; num += 4; break;
            case 'J':
            case 'K':
            case 'L':
            case '5': num *= 10; num += 5; break;
            case 'M':
            case 'N':
            case 'O':
            case '6': num *= 10; num += 6; break;
            case 'P':
            case 'R':
            case 'S':
            case '7': num *= 10; num += 7; break;
            case 'T':
            case 'U':
            case 'V':
            case '8': num *= 10; num += 8; break;
            case 'W':
            case 'X':
            case 'Y':
            case '9': num *= 10; num += 9; break;
        }
        i++;
    }
    return num;
}
PhoneBook * CreatePhoneBook(int size)
{
    PhoneBook * p;
    p = malloc(sizeof(PhoneBook));
    p->number = malloc(sizeof(int) * size);
    p->count = 0;
    p->max = size;
    return p;
}
void DeletePhoneBook(PhoneBook *p)
{
    free(p->number);
    free(p);
}
void InsertPhone(PhoneBook *p, int num)
{
    if(p->count < p->max) p->number[p->count++] = num;
}
void SortPhone(PhoneBook *p)
{
    HeapSort(p->number, p->count);
}
void OutputMessage(PhoneBook *p)
{
    int num, dups, i, count, *ns, isDup = 0;
    SortPhone(p);
    ns = p->number;
    count = p->count;
    num = ns[0];
    dups = 1;
    for(i = 1; i < count; i++)
    {
        if(ns[i] == num)
        {
            dups++;
            isDup = 1;
        }
        else if(dups > 1)
        {
            printf("%03d-%04d %d\n", num / 10000, num % 10000, dups);
            num = ns[i];
            dups = 1;
        }
        else
        {
            num = ns[i];
            dups = 1;
        }
    }
    if(dups > 1) printf("%03d-%04d %d\n", num / 10000, num % 10000, dups);
    if(isDup == 0) printf("No duplicates.\n");
}
int main()
{
    int size, i;
    PhoneBook *p;
    char str[64];
    scanf("%d", &size);
    if(size <= 0 || size > 100000) return 0;
    p = CreatePhoneBook(size);
    for(i = 0; i < size; i++)
    {
        scanf("%s", str);
        InsertPhone(p, StringToNumber(str));
    }
    OutputMessage(p);
    DeletePhoneBook(p);
}

重剑无锋,大巧不工
2011-09-12 23:10
袁德凯
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2011-4-8
收藏
得分:0 
回复 3楼 beyondyf
谢谢你的回答。看你写了那么大一段代码,知道你花了不少心思。不过,我不是问这个题目怎么做,而是我的做法为什么不行。
能告诉我,我的问题出在哪吗?
2011-09-13 16:07
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:20 
倒没花什么心思,这是我以前做过的,顺手复制一份给你而已。我也不想花时间看这么长的代码。

重剑无锋,大巧不工
2011-09-14 21:25
快速回复:pku acm里面的第1002题,不知道我的程序为什么能不过?
数据加载中...
 
   



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

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