| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1257 人关注过本帖
标题:为什么这个程序用C++在OJ上提交显示编译错误,用C提交就过了?
只看楼主 加入收藏
青蝶
Rank: 2
等 级:论坛游民
帖 子:160
专家分:51
注 册:2018-2-4
结帖率:92%
收藏
已结贴  问题点数:20 回复次数:2 
为什么这个程序用C++在OJ上提交显示编译错误,用C提交就过了?
题目描述
任何一个正整数都可以用 2 的幂次方表示。例如
137=27+23+20137=2^7+2^3+2^0
同时约定方次用括号来表示,即
a^b可表示为 a(b) 。
由此可知,137 可表示为:
2(7)+2(3)+2(0)
进一步:
7=2(2)+2+2(0)(2^1用2表示),并且3=2+2(0)。
所以最后 137 可表示为:
2(2(2)+2+2(0))+2(2+2(0))+2(0)

输入输出格式
输入格式:
一个正整数 n(n≤20000) 。
输出格式:
符合约定的 n 的 0,2 表示(在表示中不能有空格)

输入输出样例
输入样例#1:
1315
输出样例#1:
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)

代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;

int index=0,a[1000][100];

int calculate(int x){
    int i,j=0,s=0,t,k;
    index++;
    while(s!=x){
        t=1;
        k=0;
        while(s+t<=x){
            t=t*2;
            k++;
        }
        s+=t/2;
        a[index][j++]=k-1;
    }
    return index;
}

void output2(int number){
    int i=0,k;
    printf("2(");
    while(a[number][i]!=-1){
        if(i>0) printf("+");
        if(a[number][i]!=0 && a[number][i]!=1 && a[number][i]!=2){
            k=calculate(a[number][i]);
            output2(k);
        }
        else{
            if(a[number][i]==1) printf("2");
            else printf("2(%d)",a[number][i]);
        }
        i++;
    }
    printf(")");
}
   

void output1(int number){
    int i=0,k;
    while(a[number][i]!=-1){
        if(i>0) printf("+");
        if(a[number][i]!=0 && a[number][i]!=1 && a[number][i]!=2){
            k=calculate(a[number][i]);
            output2(k);
        }
        else{
            if(a[number][i]==1) printf("2");
            else printf("2(%d)",a[number][i]);
        }
        i++;
    }
}
   
   
int main(void){
    int n,k,i,j;
    scanf("%d",&n);
    for(i=0;i<1000;i++){
        for(j=0;j<100;j++){
            a[i][j]=-1;
        }
    }
    k=calculate(n);
    output1(k);
    printf("\n");
    return 0;
}

用C++提交显示编译错误:
编译信息
编译失败
/tmp/tmpI0bRlB.cpp:10:5: error: 'int index' redeclared as different kind of symbol
int index=0,a[1000][100];

^
In file included from /tmp/runtime/include/c++/4.9.4/cstring:42:0,
from /tmp/tmpI0bRlB.cpp:4:
/tmp/runtime/include/string.h:467:20: note: previous declaration 'const char* index(const char*, int)'
extern const char *index (const char *__s, int __c)
^
/tmp/tmpI0bRlB.cpp: In function 'int calculate(int)':
/tmp/tmpI0bRlB.cpp:14:7: error: no post-increment operator for type
index++;

^
/tmp/tmpI0bRlB.cpp:23:10: error: invalid types 'int [1000][100][<unresolved overloaded function type>]' for array subscript
a[index][j++]=k-1;

^
/tmp/tmpI0bRlB.cpp:25:9: error: cannot resolve overloaded function 'index' based on conversion to type 'int'
return index;

^

搜索更多相关主题的帖子: include int index number printf 
2018-07-31 18:34
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:10 
图片附件: 游客没有浏览图片的权限,请 登录注册
2018-07-31 22:59
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
首先,还是老话,不贴链接可耻 http://acm.xidian.

回正题
error: 'int index' redeclared as different kind of symbol
int index=0,a[1000][100];

^
In file included from /tmp/runtime/include/c++/4.9.4/cstring:42:0,
就是说 index 重复定义了,上一个定义在 /tmp/runtime/include/c++/4.9.4/cstring 的第 42 行
找个英汉翻译软件吧

吐槽一下,gcc 4.9.4 好古老呀,现在都已经是 gcc 8.2 了。

这道题很简单,如果只是为了过关,那没什么可思考的。但要想速度最快……
考虑 20000 最高位才 2^14,那只有先将1到14的表示法存起来查表就行了。
所以第一步
程序代码:
    std::string buf[15] = { "0" };

    for( size_t i=1; i!=std::size(buf); ++i )
    {
        for( unsigned j=15; j!=0; --j )
        {
            if( (i&(1u<<(j-1))) != 0 )
            {
                if( j == 2 )
                    buf[i] += "+2";
                else
                    buf[i] += "+2(" + buf[j-1] + ")";
            }
        }
        buf[i].erase( buf[i].begin() );
    }

    for( size_t i=1; i!=std::size(buf); ++i )
        std::cout << i << " : " << buf[i] << '\n';
获得
1 : 2(0)
2 : 2
3 : 2+2(0)
4 : 2(2)
5 : 2(2)+2(0)
6 : 2(2)+2
7 : 2(2)+2+2(0)
8 : 2(2+2(0))
9 : 2(2+2(0))+2(0)
10 : 2(2+2(0))+2
11 : 2(2+2(0))+2+2(0)
12 : 2(2+2(0))+2(2)
13 : 2(2+2(0))+2(2)+2(0)
14 : 2(2+2(0))+2(2)+2

现在就有最终代码了
程序代码:
#include <iostream>
#include <string>

void foo( unsigned n )
{
    static const std::string buf[15] = { "0", "2(0)", "2", "2+2(0)", "2(2)", "2(2)+2(0)"
                                       , "2(2)+2", "2(2)+2+2(0)", "2(2+2(0))", "2(2+2(0))+2(0)", "2(2+2(0))+2"
                                       , "2(2+2(0))+2+2(0)", "2(2+2(0))+2(2)", "2(2+2(0))+2(2)+2(0)", "2(2+2(0))+2(2)+2" };

    std::string tmp;
    for( unsigned j=15; j!=0; --j )
    {
        if( (n&(1u<<(j-1))) != 0 )
        {
            if( j == 2 )
                tmp += "+2";
            else
                tmp += "+2(" + buf[j-1] + ")";
        }
    }
    puts( tmp.c_str()+1 );
}

int main( void )
{
    unsigned n;
    scanf( "%u", &n );
    foo( n );
}


2018-08-01 10:27
快速回复:为什么这个程序用C++在OJ上提交显示编译错误,用C提交就过了?
数据加载中...
 
   



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

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