题目1:将非负十进制整数n转换成b进制。(其中b=2~16)
题目2:任何一个正整数都可以用2的幂次方表示。例如:
137=27+23+2^0
同时约定幂次方用括号来表示,即ab 可表示为a(b)。
由此可知,137可表示为:
2(7)+2(3)+2(0)
进一步:7= 22+2+20 (21用2表示)
3=2+2^0
所以最后137可表示为:
2(2(2)+2+2(0))+2(2+2(0))+2(0)
又如:
1315=2^10 +2^8 +2^5 +2+2^0
所以1315最后可表示为:
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
输入:正整数(n≤20000)
输出:符合约定的n的0,2表示(在表示中不能有空格)
输入格式 Input Format
一个正整数
输出格式 Output Format
符合约定的n的0,2表示(在表示中不能有空格)
样例输入 Sample Input
73
样例输出 Sample Output
2(2(2)+2)+2(2+2(0))+2(0)
#include <stdio.h>
void transFormation(int n ,int b)
{
char t;
if(n==0){
return ;
}
else{
transFormation(n/b,b);
if(n%b<10){
printf("%d",n%b);
}
else{
t='A'+n%b-10;
printf("%c",t);
}
}
}
int main (void)
{
int n ,b;
printf("输入n的值:");
scanf("%d",&n);
printf("输入b的值:");
scanf("%d",&b);
transFormation(n,b);
printf("\n");
return 0;
}
#include <stdio.h>
void fun (int x,int flag)
{
int n=0,t=1;
if(flag==1&&x){
printf("+");
}
if(x==0){
return ;
}
if(x==1){
printf("2(0)");
return;
}
if(x==2){
printf("2");
return ;
}
printf("2");
if(x>=4){
printf("(");
}
while(t<=x){
t=t*2;
n++;
}
if(n-1>=2){
fun(n-1,0);
}
if(x>=4){
printf(")");
}
fun(x-t/2,1);
}
int main (void)
{
int x;
scanf("%d",&x);
fun (x,0);
printf("\n");
return 0;
}