111 条题解
-
2
猫粮寸断 LV 10 @ 6 年前
-
12 年前@
-
14 年前@
-
17 年前@
so water
-
17 年前@
-
18 年前@
简单的递归,不过效率不高
#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;
int n,now;
bool r=0;
void print(int k,int r2=0)
{
if(!k)return;
int step=-1,p=k;
while(k!=0)
{
step++;
k/=2;
}
k=p;
if(r2)cout<<"+";
if(step==1)
{
cout<<"2";
print(k-2,1);
return;
}
if(step==0)
{
cout<<"2(0)";
return;
}
if(step>1)
{
cout<<"2(";
print(step);
cout<<")";
}
if(k-pow(2,step)!=0)print(k-pow(2,step),1);
}
int main()
{
cin>>n;
print(n);
return 0;
} -
03 年前@
-
05 年前@
#include<iostream>
#include<algorithm>
using namespace std;void print2(int n){
if(n==1){
cout<<"2(0)";
return;
}
if(n==2){
cout<<'2';
return;
}
int i=0;//指数
int s=2;//用于寻找最接近n的2的幂次方
while(s<=n){
s<<=1;
i++;
} //s=2^(i+1)>n
s>>=1; //s=2^i<=n
if(n==s){//2^i=n
cout<<"2(";
print2(i);
cout<<')';
}else{ //2^i<n
print2(s);
cout<<'+';
print2(n-s);
}
}int main(){
int n;
cin>>n;
print2(n);
return 0;
} -
07 年前@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
void dfs(int n)
{
int a[20];
memset(a,0,sizeof(a));
int flag = 0;
int i = 0,j;
while(n>0)
{
a[i++] = n%2;
n/=2;
}
for(i=15;i>=0&&a[i]==0;--i);
for(j=i;j>=0;--j)
if(a[j]==1)
{
if(j>1){
if(flag)
cout<<"+";
cout<<"2(";
dfs(j);
cout<<")";
flag = 1;
}
else if(j==1){
if(flag)
cout<<"+";
cout<<"2";
flag = 1;
}
else{
if(flag)
cout<<"+";
cout<<"2(0)";
}
}
}
int main()
{
int n;
cin>>n;
dfs(n);
return 0;
} -
07 年前@
分治
#include<cstdio>int a[15];
int work(int l,int r,int p)
{
if (l>r) return l-1;
int mid=(l+r)/2;
if (a[mid]==p) return mid;
if (a[mid]<p) return work(mid+1,r,p);
else if (a[mid]>p) return work(l,mid-1,p);
}
void done(int n)
{
int k=work(0,14,n);
if (k>=0 && k<=2)
{
if (k==1) printf("2");
else printf("2(%d)",k);
}
else
{
printf("2(");
done(k);
printf(")");
}
if (n!=a[k])
{
printf("+");
done(n-a[k]);
}
}
int main()
{
int n;
scanf("%d",&n);
a[0]=1;
for (int i=1;i<15;i++)
a[i]=a[i-1]*2;
done(n);
return 0;
} -
07 年前@
Var
n,cf,num:longint;
k:char;Procedure dfs(x:longint);
Var
num,cf:longint;
Begin
if x<3 then
begin
write(x);
exit;
end;
while x>0 do
begin
num:=1;
cf:=0;
while num*2<=x do
begin
num:=num*2;
inc(cf);
end;
if cf=1 then
begin
if k=')' then
write('+2');
if k='(' then
begin
write('2');
k:=')';
end;
end
else
begin
if k=')' then
write('+2(')
else
write('2(');
k:='(';
dfs(cf);
write(')');
k:=')';
end;
x:=x-num;
end;
End;Begin
readln(n); k:='(';
while n>0 do
begin
num:=1;
cf:=0;
while num*2<=n do
begin
num:=num*2;
inc(cf);
end;
if cf=1 then
begin
if k=')' then
write('+2');
if k='(' then
begin
write('2');
k:=')';
end;
end
else
begin
if k=')' then
write('+2(')
else
write('2(');
k:='(';
dfs(cf);
write(')');
k:=')';
end;
n:=n-num;
end;
readln;
End.
日常一水题 -
07 年前@
不知道有没有人这样写过。。。
渣渣小水比,大神们见笑了 -
08 年前@
##这个题由于条件的限制,只需要到2^14即可,然后打个表,比较简单##
上代码
#include<iostream>
#include<algorithm>using namespace std;
int i,j,m,n;
int h[15]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384};
string c[15]={"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 main()
{
freopen("m.in","r",stdin);
freopen("m.out","w",stdout);
int kg=1;
cin>>n;
while(n)
{
for(i=0;i<=14;i++)
if(n<h[i])
break;
if(i)
i--;
if(kg)
{kg=0;}
else
cout<<"+"<<c[i];
n-=h[i];
}
}评测状态 Accepted
题目 P1597 2的幂次方
递交时间 2017-02-18 19:01:32
代码语言 C++
评测机 ShadowShore
消耗时间 0 ms
消耗内存 736 KiB
评测时间 2017-02-18 19:01:33
评测结果
编译成功测试数据 #0: Accepted, time = 0 ms, mem = 732 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 736 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 732 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 732 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 732 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 736 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 732 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 736 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 732 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 732 KiB, score = 10
Accepted, time = 0 ms, mem = 736 KiB, score = 100
为了让同学们更好的了解这个题,我修改了一些东西,导致你复制粘贴大法失效。。。比如输出。。。 -
08 年前@
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
using namespace std;int n;
void ask(int n){
if(n==2){
putchar('2');
return;
}
if(n<=0){
putchar('0');
return;
}
vector<int>v;
int tot=0;
while(n){
if(n&1)v.push_back(tot);
tot++;
n>>=1;
}
for(int i=v.size()-1;i>=0;i--){
putchar('2');
if(v[i]!=1){
putchar('(');
ask(v[i]);
putchar(')');
}
if(i>0)putchar('+');
}
}int main(){
scanf("%d",&n);
ask(n);
} -
08 年前@
为何我递归最后一组数据超时?……
```pascal
program yange;
var
i,j,n:longint;
S:array[0..14] of longint;
procedure once(x:longint);
var
i,j,a,b,y:longint;
begin
repeat
i:=0;
repeat
i:=i+1;
until x<=S[i];
if x<S[i]
then j:=i-1
else j:=i;
a:=S[j];
y:=j;
write('2');
if j>2 then
begin
write('(');
once(y);
write(')');
end
else if j=2
then write('(2)')
else if j=0
then write('(0)');
b:=x-a;
x:=x-a;
if b<>0
then write('+');
until b=0;
end;
begin
readln(n);
for i:=0 to 14 do
S[i]:=1;
for i:=0 to 14 do
for j:=1 to i do
S[i]:=2*S[i];
once(n);
end. -
09 年前@
#include<cstdio>
using namespace std;
void ok(int n)//2(2(2)
{
int x=1,y=0,s=0,h=n;
while(s!=n)
{
x=1,y=0;
while(x<=h)
{
y++;
x=x*2;
}
y--;x=x/2;
if(y==1) printf("2");
else if(y==0) printf("2(0)");
else if(y==2) printf("2(2)");
else {
printf("2(");
ok(y);
printf(")");
}
s=s+x;h=h-x;
if(s!=n) printf("+");
}
}
int main()
{
int n;
scanf("%d",&n);
ok(n);
return 0;
} -
09 年前@
var
n:string;
function ch(i1:string):string;
var
o1,t:string;
n,c,i:longint;
begin
c:=1;
val(i1,n);
i:=0;
o1:='';
while true do
begin
if c>n then
break;
c:=c*2;
inc(i);
end;
while c>0 do
begin
if (c<=n)and(c<>2) then
begin
n:=n-c;
if (i<>2)and(i<>0) then
begin
str(i,t);
o1:=o1+'2('+ch(t)+')+';
end
else
if i=2 then
o1:=o1+'2(2)+'
else
o1:=o1+'2(0)+';
end
else
if (c=2)and(c<=n) then
begin
o1:=o1+'2+';
n:=n-c;
end
else
begin
c:=c div 2;
dec(i);
end;
end;
exit(copy(o1,1,length(o1)-1));
end;
begin
readln(n);
n:=ch(n);
writeln(n);
end. -
09 年前@
#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;
} -
09 年前@
program exercise(input,output);
var n:longint;
procedure print(x:longint);
var i,j:longint;
begin
if x=0 then
begin
write(0);
exit;
end;
while x>0 do
begin
write(2);
i:=x;
j:=0;
while i>1 do
begin
i:=i div 2;
inc(j);
end;
if j<>1 then
begin
write('(');
print(j);
write(')');
end;
x:=x-1 shl j;
if x>0 then
write('+');
end;
end;
begin
readln(n);
print(n);
writeln;
end. -
09 年前@
30行秒杀
#include <stdio.h>
void print(int n){
int i, k;
for(k=0; k<=15 && (n&(1<<k))==0; k++);
for(i=15; i>k; i--){
if(n&(1<<i)){
putchar('2');
if(i != 1){
putchar('(');
print(i);
putchar(')');
}
putchar('+');
}
}
putchar('2');
if(k != 1){
putchar('(');
if(k == 0)
putchar('0');
else
print(k);
putchar(')');
}
}
int main(){
int num;
scanf("%d", &num);
print(num);
return 0;
}