- 2的幂次方
- 2015-10-13 22:49:55 @
c++AC不了,pascal却可以啊?!!!!大神救救我
#include <iostream>
#include <stdio.h>
using namespace std;
char str[16][150]={
{"2(0)"},
{"2"},
{"2(2)"},
{"2(2+2(0))"},
{"2(2(2))"},
{"2(2(2)+2(0))"},
{"2(2(2)+2)"},
{"2(2(2)+2+2(0))"},
{"2(2(2+2(0)))"},
{"2(2(2+2(0))+2(0))"},
{"2(2(2+2(0))+2)"},
{"2(2(2+2(0))+2+2(0))"},
{"2(2(2+2(0))+2(2)"},
{"2(2(2+2(0))+2(2)+2(0))"},
{"2(2(2+2(0))+2(2)+2)"},
{"2(2(2+2(0))+2(2)+2+2(0))"}
};
int num[17]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
int main()
{
int a;
int now=15;
scanf("%d",&a);
for(int j=1;;j++)
{
if(!a)break;
if(j!=1)printf("+");
for(now=15;now>=0;now--)
if(num[now]<=a)
break;
a-=num[now];
printf("%s",str[now]);
}
printf("\n");
return 0;
}
program p1914;
const a:array[0..14] of integer=(1,
2,
4,
8,
16,
32,
64,
128,
256,
512,
1024,
2048,
4096,
8192,
16384);
b:array[0..14] of string=
('2(0)',
'2',
'2(2)',
'2(2+2(0))',
'2(2(2))',
'2(2(2)+2(0))',
'2(2(2)+2)',
'2(2(2)+2+2(0))',
'2(2(2+2(0)))',
'2(2(2+2(0))+2(0))',
'2(2(2+2(0))+2)',
'2(2(2+2(0))+2+2(0))',
'2(2(2+2(0))+2(2))',
'2(2(2+2(0))+2(2)+2(0))',
'2(2(2+2(0))+2(2)+2)');
var i,j,n:integer;
begin
readln(n);
for i:=14 downto 0 do
if n>=a[i] then
begin
n:=n-a[i];
write(b[i]);
break;
end;
dec(i);
for j:=i downto 0 do
if n>=a[j] then
begin
n:=n-a[j];
write('+',b[j]);
end;
writeln;
end.
2 条评论
-
TenderRun LV 10 @ 2015-10-15 22:15:39
###坑啊
-
2015-10-15 22:15:07@
#include <iostream>
#include <stdio.h>
using namespace std;
char str[16][150]={
"2(0)",
"2",
"2(2)",
"2(2+2(0))",
"2(2(2))",
"2(2(2)+2(0))",
"2(2(2)+2)",
"2(2(2)+2+2(0))",
"2(2(2+2(0)))",
"2(2(2+2(0))+2(0))",
"2(2(2+2(0))+2)",
"2(2(2+2(0))+2+2(0))",
"2(2(2+2(0))+2(2))",
"2(2(2+2(0))+2(2)+2(0))",
"2(2(2+2(0))+2(2)+2)"
};
int num[17]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
int main()
{
int a,i=14,j;
scanf("%d",&a);
for(i;i>=0;i--)
{
if(a>=num[i])
{
a-=num[i];
printf("%s",str[i]);
break;
}
}
i--;
for(int j=i;j>=0;j--)
{
if(a>=num[j])
{
a-=num[j];
printf("+%s",str[j]);
}
}
printf("\n");
return 0;
}
###怀疑是评测机系统的问题,把数组的大括号去掉就对了
- 1