C 打印到输出:整数为2的幂和

C 打印到输出:整数为2的幂和,c,twos-complement,C,Twos Complement,我参加了一次考试,从那以后我一直在努力。 您有一个整数数组(例如13、6、21、4),我需要生成如下输出: 13 = 2^3 + 2^2 + 2^0 6 = 2^2 + 2^1 21 = 2^4 + 2^2 + 2^0 4 = 2^2 这是我到目前为止得到的 #include <stdio.h> #define MAX 100 int main() { int niz[MAX], nizb, n, i, ones, k; while(1) { p

我参加了一次考试,从那以后我一直在努力。 您有一个整数数组(例如13、6、21、4),我需要生成如下输出:

13 = 2^3 + 2^2 + 2^0
6 = 2^2 + 2^1
21 = 2^4 + 2^2 + 2^0
4 = 2^2
这是我到目前为止得到的

#include <stdio.h>
#define MAX 100

int main() {
    int niz[MAX], nizb, n, i, ones, k;

    while(1) {
        printf("Array length: ");
        scanf("%d", &n);

        if (n<=0 || n>MAX) break;

        printf("Array elements: ");
        for(i=0;i<n;i++){
            scanf("%d", &niz[i]);
            if (niz[i] <=0) {
                printf("Error! Wrong value. Enter new one: ");
                scanf("%d", &niz[i]);
            }
        }

        for(i=0;i<n;i++) {
            nizb = niz[i];
            ones = 0;

            for(k=0; k < 16; k++) {
                //What should i do here?
            }

        }

    }
}
#包括
#定义最大值100
int main(){
int niz[MAX],nizb,n,i,one,k;
而(1){
printf(“数组长度:”);
scanf(“%d”和“&n”);
如果(nMAX)中断;
printf(“数组元素:”);

对于(i=0;i,您可以使用
sizeof
运算符和
CHAR\u位
计算要使用的位数:

int bitsPerInt = sizeof(int) * CHAR_BIT;
字符位
limits.h
中定义

达到该限制后,可以使用逐位
&
运算符提取每个位:

for (k = bitsPerInt - 1; k >= 0; k--)
{
    if (nizb & (1U << k))
        // output
    else
        // don't
}
用于(k=bitsprint-1;k>=0;k--)
{

如果(nizb&(1U您可以使用
sizeof
运算符和
CHAR\u位
计算要使用的位数:

int bitsPerInt = sizeof(int) * CHAR_BIT;
字符位
limits.h
中定义

达到该限制后,可以使用逐位
&
运算符提取每个位:

for (k = bitsPerInt - 1; k >= 0; k--)
{
    if (nizb & (1U << k))
        // output
    else
        // don't
}
用于(k=bitsprint-1;k>=0;k--)
{

如果(nizb&(1U这是完全的猜测,因为我的数学不是很好,但我想我会这样做:

 int potency = 0, base = 1;
 while(base < NumberInQuestion) {
     base *= 2;
     ++potency;
 }
for(i=0; i < n; ++i) {
    int Number = niz[i];
    while(Number > 0) {
        int potency = 0, base = 1;
        do {               //Executes at least once, making a number of '1' possible.
            base *= 2;
            ++potency;
        } while(base < Number);
        Number -= base/2; //Reverts the last step you made, making the 'base' smaller than 'Number'.
        printf("2^%d", potency);
    }
}
冲洗并重复,直到数字降至0,最迟应为2^0

对于您的用例,代码可能看起来有点像这样:

 int potency = 0, base = 1;
 while(base < NumberInQuestion) {
     base *= 2;
     ++potency;
 }
for(i=0; i < n; ++i) {
    int Number = niz[i];
    while(Number > 0) {
        int potency = 0, base = 1;
        do {               //Executes at least once, making a number of '1' possible.
            base *= 2;
            ++potency;
        } while(base < Number);
        Number -= base/2; //Reverts the last step you made, making the 'base' smaller than 'Number'.
        printf("2^%d", potency);
    }
}
(i=0;i{ 整数=niz[i]; 而(数量>0){ int效价=0,碱基=1; do{//至少执行一次,使“1”的数目成为可能。 基数*=2; ++效力; }while(基<数); Number-=base/2;//还原所做的最后一步,使“base”小于“Number”。 printf(“2^%d”,效力); } }
有一个可能的替代方案,它可以让你对事情有一个更完整的了解,并且可以节省你的迭代次数

for(i=0; i < n; ++i) {
    int Number = niz[i];
    int potency = 0, base = 1;
    do {               //Executes at least once, making a number of '1' possible.
        base *= 2;
        ++potency;
    } while(base < Number);

    base /= 2;                      //Reverses the last iteration.

    //At this point, we know the maximum potency, which still fits into the number.
    //In regards of base 2, we know the Most Significant Bit.
    while(base > 0) {
        Number -= base;             //Removes the MSD (Most significant digit)
        printf("2^%d", potency);    //Prints your '1'.
        while(base > Number) {      //Executes at least once.
            base /= 2;              //Goes back one potency. (Ends at '0' latest.)
            --potency;              //For each potency (except for first), it's a '0'.
        }
    }
}
(i=0;i{ 整数=niz[i]; int效价=0,碱基=1; do{//至少执行一次,使“1”的数目成为可能。 基数*=2; ++效力; }while(基<数); base/=2;//反转上一次迭代。 //在这一点上,我们知道最大效力,它仍然符合这个数字。 //关于基数2,我们知道最重要的位。 而(基数>0){ Number-=base;//删除MSD(最高有效位) printf(“2^%d”,效力);//打印您的“1”。 而(base>Number){//至少执行一次。 base/=2;//返回一个效价。(最迟在“0”结束。) --效价;//对于每个效价(第一个除外),它都是一个“0”。 } } }
这完全是猜测,因为我的数学不是很好,但我想我会这样做:

 int potency = 0, base = 1;
 while(base < NumberInQuestion) {
     base *= 2;
     ++potency;
 }
for(i=0; i < n; ++i) {
    int Number = niz[i];
    while(Number > 0) {
        int potency = 0, base = 1;
        do {               //Executes at least once, making a number of '1' possible.
            base *= 2;
            ++potency;
        } while(base < Number);
        Number -= base/2; //Reverts the last step you made, making the 'base' smaller than 'Number'.
        printf("2^%d", potency);
    }
}
冲洗并重复,直到数字降至0,最迟应为2^0

对于您的用例,代码可能看起来有点像这样:

 int potency = 0, base = 1;
 while(base < NumberInQuestion) {
     base *= 2;
     ++potency;
 }
for(i=0; i < n; ++i) {
    int Number = niz[i];
    while(Number > 0) {
        int potency = 0, base = 1;
        do {               //Executes at least once, making a number of '1' possible.
            base *= 2;
            ++potency;
        } while(base < Number);
        Number -= base/2; //Reverts the last step you made, making the 'base' smaller than 'Number'.
        printf("2^%d", potency);
    }
}
(i=0;i{ 整数=niz[i]; 而(数量>0){ int效价=0,碱基=1; do{//至少执行一次,使“1”的数目成为可能。 基数*=2; ++效力; }while(基<数); Number-=base/2;//还原所做的最后一步,使“base”小于“Number”。 printf(“2^%d”,效力); } }
有一个可能的替代方案,它可以让你对事情有一个更完整的了解,并且可以节省你的迭代次数

for(i=0; i < n; ++i) {
    int Number = niz[i];
    int potency = 0, base = 1;
    do {               //Executes at least once, making a number of '1' possible.
        base *= 2;
        ++potency;
    } while(base < Number);

    base /= 2;                      //Reverses the last iteration.

    //At this point, we know the maximum potency, which still fits into the number.
    //In regards of base 2, we know the Most Significant Bit.
    while(base > 0) {
        Number -= base;             //Removes the MSD (Most significant digit)
        printf("2^%d", potency);    //Prints your '1'.
        while(base > Number) {      //Executes at least once.
            base /= 2;              //Goes back one potency. (Ends at '0' latest.)
            --potency;              //For each potency (except for first), it's a '0'.
        }
    }
}
(i=0;i{ 整数=niz[i]; int效价=0,碱基=1; do{//至少执行一次,使“1”的数目成为可能。 基数*=2; ++效力; }while(基<数); base/=2;//反转上一次迭代。 //在这一点上,我们知道最大效力,它仍然符合这个数字。 //关于基数2,我们知道最重要的位。 而(基数>0){ Number-=base;//删除MSD(最高有效位) printf(“2^%d”,效力);//打印您的“1”。 而(base>Number){//至少执行一次。 base/=2;//返回一个效价。(最迟在“0”结束。) --效价;//对于每个效价(第一个除外),它都是一个“0”。 } } }
不确定这与两个补码(这是一种表示负数的特殊方式)有什么关系。显然,你要做的是将一个整数表示为2的幂和。下面是我的方法,它不一定比其他答案更好或更差

void powersum(int n)
{ int powers[sizeof(int) << 3];
  int i;
  char *sep = "";

  printf("%d = ", n);

  powers[0] = 0;

  for (i = 0; n; n >>= 1, ++i)
    powers[i] = n & 1;

  while (--i >= 0)
  { if (powers[i])
    { printf("%s2^%d", sep, i);
      sep = " + ";
    }
  }

  printf("\n");
}
void powersum(int n)
{int幂[sizeof(int)>=1,++i)
幂[i]=n&1;
而(--i>=0)
{if(幂[i])
{printf(“%s2^%d”,九月一日);
sep=“+”;
}
}
printf(“\n”);
}
编辑:这是另一个版本,它不使用堆栈分配的数组,但作为一种折衷,它必须多次循环(每一位循环一次,而不是在找到最高的1位之前循环):

void powersum2(int n)
{int i=(sizeof(int)=1;
--一,;
}
printf(“\n”);
}

不确定这与两个补码(这是一种表示负数的特殊方式)有什么关系。显然,你要做的是将一个整数表示为2的幂和。下面是我的方法,它不一定比其他答案更好或更差

void powersum(int n)
{ int powers[sizeof(int) << 3];
  int i;
  char *sep = "";

  printf("%d = ", n);

  powers[0] = 0;

  for (i = 0; n; n >>= 1, ++i)
    powers[i] = n & 1;

  while (--i >= 0)
  { if (powers[i])
    { printf("%s2^%d", sep, i);
      sep = " + ";
    }
  }

  printf("\n");
}
void powersum(int n)
{int幂[sizeof(int)>=1,++i)
幂[i]=n&1;
而(--i>=0)
{if(幂[i])
{printf(“%s2^%d”,九月一日);
sep=“+”;
}
}
printf(“\n”);
}
编辑:这是另一个版本,它不使用堆栈分配的数组,而是作为一种折衷