检查数字是2的幂,如果输入是数字 [英] Check if number is a power of two, and if the input is number

查看:96
本文介绍了检查数字是2的幂,如果输入是数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查数组中的数字是否为2的幂,

i写下了下面的代码,但它不起作用它跳过了如果数字是2的幂而且打印最后一句。

如果有人可以帮我检查输入是否是数字而不是任何其他的charctar。

谢谢你!



我的尝试:



i want to check if the the numbers in array are power of 2,
i wrote the folowing code but it doesn't work it skips the part that chiks if the number is power of two and prints the last sentence.
also if someone can help me in how to check if the input is number and not any other charctar.
thank you!

What I have tried:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int x;
    int i;
    int k;
    int count=0;
    int a;
    int sum=0;
    printf("Enter size of input:\n");
    scanf("%d",&x);
    int *numbers=malloc(sizeof(int)*x);
    if (x<0){
      printf("Invalid size\n");
    }
    else {
       printf("Enter numbers:\n");
       for(i=0;i<x;++i){
         scanf("%d",&numbers[i]);
       }
    }
    for(k=0;k<x;++k)
    {
        count=0;
        a=numbers[k];
        while (((numbers[k] % 2) == 0) && numbers[k] > 1){ /* While x is even and > 1 */
             numbers[k]/= 2;
             ++count;
        }
        if (numbers[k]==0){
             printf("The number %d is a power of 2:%d=2^%d\n",a,a,count);
             sum+=a;
        }
    }
    printf("Total exponent num is %d\n",sum);
  return 0;
}

推荐答案

Quote:

我试过调试不起作用



这句话暗示你不知道如何使用调试器。调试器只显示你的代码在做什么,你必须检查变量并检查一切是否符合你的期望。

为了快速启动,你可以在youtube上找到tutos。



此行位置错误,因为无论 x 的值是什么,它都会尝试分配内存。在内存分配之前,你需要确保 x 为正。


This quote suggest that you don't know how to use the debugger. the debugger only show you what your code is doing, you have to inspect variables and check if everything is consistent with your expectations.
For a fast starting, you can find tutos on youtube.

This line is in wrong place because it will try to allocate memory whatever is the value of x. You need to make sure that x is positive before memory allocation.

int *numbers=malloc(sizeof(int)*x);





当你不明白你的代码在做什么或为什么它做的时候,答案是 debugger

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。



When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


你知道,C编程语言不会阻止你定义函数......

尝试

You know, the C programming language does not prevent you defining functions...
Try
#include <stdio.h>
#include <stdlib.h>


int powoftwo(int x)
{
  int count;

  for ( count = 0; x && ((x & 1) == 0); ++count )
    x >>= 1;

  if ( x == 1)
    return count;

  return -1;
}


int main()
{
    int x;
    int i;
    int k;
    printf("Enter size of input:\n");
    scanf("%d",&x);
    int *numbers= NULL;
    if (x<0){
      printf("Invalid size\n");
      return -1;
    }
    else
    {
      numbers = malloc(sizeof(int)*x);
      if ( ! numbers)
      {
        printf("memory allocation failure\n");
      }

       printf("Enter numbers:\n");
       for(i=0;i<x;++i){
         scanf("%d",&numbers[i]);
       }
    }
    for(k=0;k<x;++k)
    {
      int exp = powoftwo(numbers[k]);
      if ( exp > 0)
        printf("The number %d is a power of 2:%d=2^%d\n", numbers[k],numbers[k],exp);

    }
    free(numbers);
    return 0;
}


这篇关于检查数字是2的幂,如果输入是数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆