| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1006 人关注过本帖
标题:北大ACM题1002,求大神指导,为什么运行超时或错误,求解释!!!
只看楼主 加入收藏
jj369258
Rank: 4
等 级:业余侠客
帖 子:116
专家分:226
注 册:2010-12-2
结帖率:69.57%
收藏
已结贴  问题点数:10 回复次数:5 
北大ACM题1002,求大神指导,为什么运行超时或错误,求解释!!!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int d[100002]={0};
char a[100002][100];  //输入数组
char b[100002][100];  //输入后去除'-'的数组
int main()
{
    int i,j,k,n,min;
    char c[100];   
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%s",a[i]);
        for(k=0,j=0; ;j++)
        {        
            if(a[i][j]=='A' || a[i][j]=='B' || a[i][j]=='C')
            {
                a[i][j]='2';
            }
            if(a[i][j]=='D' || a[i][j]=='E' || a[i][j]=='F')
            {
                a[i][j]='3';
            }
            if(a[i][j]=='G' || a[i][j]=='H' || a[i][j]=='I')
            {
                a[i][j]='4';
            }
            if(a[i][j]=='J' || a[i][j]=='K' || a[i][j]=='L')
            {
                a[i][j]='5';
            }
            if(a[i][j]=='M' || a[i][j]=='N' || a[i][j]=='O')
            {
                a[i][j]='6';
            }
            if(a[i][j]=='P' || a[i][j]=='R' || a[i][j]=='S')
            {
                a[i][j]='7';
            }
            if(a[i][j]=='T' || a[i][j]=='U' || a[i][j]=='V')
            {
                a[i][j]='8';
            }
            if(a[i][j]=='W' || a[i][j]=='X' || a[i][j]=='Y')
            {
                a[i][j]='9';
            }
            if(a[i][j]!='-')
            {
                b[i][k]=a[i][j];
                k++;
            }
            if(a[i][j]=='\0')
            {
                break;
            }
        }
    } //n个字符串输入
    char e[100]={0};
    int t;
    for(i=0;i<n;i++)
    {
        for(j=i;j<n-1;j++)
        {
            if(strcmp(b[i],b[j+1])==0)
            {
                d[i]++;
                strcpy(b[j+1],e);
            }
        }
    }
    int x;
    for(x=0,i=0;i<n;i++)
    {   
        min=i;
        for(j=i;j<n-1;j++)
        {
            if(d[min]!=0 && d[j+1]!=0)
            {
                if(strcmp(b[min],b[j+1])>0)
                {
                    min=j+1;   
                }
            }
        }

        strcpy(c,b[i]);
        strcpy(b[i],b[min]);
        strcpy(b[min],c);
        t=d[i];
        d[i]=d[min];
        d[min]=t;
        
        if(strcmp((b[i]),e)!=0 && d[i]!=0)
        {
            for(j=7;j>3;j--)
            {
                b[i][j]=b[i][j-1];
            }
            b[i][j]='-';
            printf("%s %d\n",b[i],d[i]+1);
            x++;
        }
        
    }
    if(x==0)
    {
        printf("No duplicates.");
    }
    return 0;
}
搜索更多相关主题的帖子: 北大 include 
2012-03-03 18:09
魏林诚
Rank: 2
等 级:论坛游民
帖 子:8
专家分:32
注 册:2012-2-29
收藏
得分:5 
用vc6.0去编译调试,把所有的主函数里面的所有声明都提到句首
2012-03-03 20:44
jj369258
Rank: 4
等 级:业余侠客
帖 子:116
专家分:226
注 册:2010-12-2
收藏
得分:0 
回复 2楼 魏林诚
大神,你能否说具体一点,把我的代码改一下!!
2012-03-04 20:59
虚心向学
Rank: 2
等 级:论坛游民
帖 子:19
专家分:12
注 册:2011-2-15
收藏
得分:5 
#include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 int d[100002]={0};
 char a[100002][100];  //输入数组
 char b[100002][100];  //输入后去除'-'的数组
 int main()
 {
     int i,j,k,n,min;
     char c[100];
      char e[100]={0};
     int t;
      int x;
    scanf("%d",&n);
     for(i=0;i<n;i++)
     {
         scanf("%s",a[i]);
         for(k=0,j=0; ;j++)
         {        
             if(a[i][j]=='A' || a[i][j]=='B' || a[i][j]=='C')
             {
                 a[i][j]='2';
             }
             if(a[i][j]=='D' || a[i][j]=='E' || a[i][j]=='F')
             {
                 a[i][j]='3';
             }
             if(a[i][j]=='G' || a[i][j]=='H' || a[i][j]=='I')
             {
                 a[i][j]='4';
             }
             if(a[i][j]=='J' || a[i][j]=='K' || a[i][j]=='L')
             {
                 a[i][j]='5';
             }
             if(a[i][j]=='M' || a[i][j]=='N' || a[i][j]=='O')
             {
                 a[i][j]='6';
             }
             if(a[i][j]=='P' || a[i][j]=='R' || a[i][j]=='S')
             {
                 a[i][j]='7';
             }
             if(a[i][j]=='T' || a[i][j]=='U' || a[i][j]=='V')
             {
                 a[i][j]='8';
             }
             if(a[i][j]=='W' || a[i][j]=='X' || a[i][j]=='Y')
             {
                 a[i][j]='9';
             }
             if(a[i][j]!='-')
             {
                 b[i][k]=a[i][j];
                 k++;
             }
             if(a[i][j]=='\0')
             {
                 break;
             }
         }
     } //n个字符串输入
   
     for(i=0;i<n;i++)
     {
         for(j=i;j<n-1;j++)
         {
             if(strcmp(b[i],b[j+1])==0)
             {
                 d[i]++;
                 strcpy(b[j+1],e);
             }
         }
     }
   
     for(x=0,i=0;i<n;i++)
     {   
        min=i;
         for(j=i;j<n-1;j++)
         {
             if(d[min]!=0 && d[j+1]!=0)
             {
                 if(strcmp(b[min],b[j+1])>0)
                 {
                     min=j+1;   
                }
             }
         }
 
        strcpy(c,b[i]);
         strcpy(b[i],b[min]);
         strcpy(b[min],c);
         t=d[i];
         d[i]=d[min];
         d[min]=t;
         
         if(strcmp((b[i]),e)!=0 && d[i]!=0)
         {
             for(j=7;j>3;j--)
             {
                 b[i][j]=b[i][j-1];
             }
             b[i][j]='-';
             printf("%s %d\n",b[i],d[i]+1);
             x++;
         }
         
     }
     if(x==0)
     {
         printf("No duplicates.");
     }
     return 0;
 }
2012-03-04 22:33
jj369258
Rank: 4
等 级:业余侠客
帖 子:116
专家分:226
注 册:2010-12-2
收藏
得分:0 
谢谢了!!
2012-03-14 08:24
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:0 
程序代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
#include <cmath>
using namespace std;
const int mapping[26] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6,
    7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};
const long int fact[9] = {1, 10, 100, 1000, 10000, 100000, 1000000, 
    10000000, 100000000};
int duplicate[10000000];
int main()
{
    long int i, j, n, k, S;
    string str;
    cin >> n;
    for(j = 0; j < n; j++){
        cin >> str;
        k = 0;
        //cout << str << " -> ";
        for(i = 0, S = 0; i < str.length(); i++){
            if(isdigit(str[i])){
                k += fact[6 - S++] * (str[i] - '0');
            }else if(isalpha(str[i])){
                k += fact[6 - S++] * (mapping[str[i] - 'A']);
            }
        }
        //cout  << k << endl;
        duplicate[k]++;
    }
    k = 0;
    for(i = 0; i < 10000000; i++){
        if(duplicate[i] > 1){
            printf("%03d-%04d %d\n", i / 10000, i % 10000, duplicate[i]);
            k = 1;
        }
    }
    if(!k) cout << "No duplicates." << endl;
    return 0;
}

                                         
===========深入<----------------->浅出============
2012-03-14 09:10
快速回复:北大ACM题1002,求大神指导,为什么运行超时或错误,求解释!!!
数据加载中...
 
   



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

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