如何修改单数字2的幂检测C语言程序以支持批量数字检测?
Got it, let's adjust your code so you can check multiple numbers easily! Here are a few practical approaches depending on how you want to input the values:
Approach 1: Loop to accept input until you choose to stop
This lets you manually enter numbers one by one, and exit when you input a specific termination value (like -1):
#include <stdio.h> //function prototype for checking power of two int checkPowerofTwo(int n); int main() { int num; printf("Enter numbers to test (type -1 to quit):\n"); // Infinite loop until user enters -1 while (1) { printf("Enter a number: "); scanf("%d", &num); if (num == -1) { printf("Closing program...\n"); break; } if (checkPowerofTwo(num) == 1) printf("%d is a power of 2\n", num); else printf("%d is not a power of 2\n", num); } return 0; } //function body int checkPowerofTwo(int x) { if (x == 0) return 0; while( x != 1) { if(x % 2 != 0) return 0; x /= 2; } return 1; }
Approach 2: Batch-check a predefined array of numbers
If you have a fixed set of numbers to test, store them in an array and loop through it:
#include <stdio.h> //function prototype for checking power of two int checkPowerofTwo(int n); int main() { // Define your list of numbers here int numbers[] = {2, 4, 7, 8, 16, 17, 0, 32, 64}; int arraySize = sizeof(numbers) / sizeof(numbers[0]); // Calculate how many elements are in the array printf("Testing batch of numbers:\n"); for (int i = 0; i < arraySize; i++) { int currentNum = numbers[i]; if (checkPowerofTwo(currentNum) == 1) printf("%d is a power of 2\n", currentNum); else printf("%d is not a power of 2\n", currentNum); } return 0; } //function body int checkPowerofTwo(int x) { if (x == 0) return 0; while( x != 1) { if(x % 2 != 0) return 0; x /= 2; } return 1; }
Approach 3: Pass numbers via command line arguments
Run the program with numbers directly (e.g., ./powerChecker 2 8 10 16) using command line inputs:
#include <stdio.h> #include <stdlib.h> // Needed for atoi() to convert strings to integers //function prototype for checking power of two int checkPowerofTwo(int n); int main(int argc, char *argv[]) { // Make sure the user provided at least one number if (argc < 2) { printf("Usage: %s <number1> <number2> ...\n", argv[0]); return 1; } printf("Testing command line inputs:\n"); // Loop through each argument (skip argv[0], which is the program name) for (int i = 1; i < argc; i++) { int num = atoi(argv[i]); if (checkPowerofTwo(num) == 1) printf("%d is a power of 2\n", num); else printf("%d is not a power of 2\n", num); } return 0; } //function body int checkPowerofTwo(int x) { if (x == 0) return 0; while( x != 1) { if(x % 2 != 0) return 0; x /= 2; } return 1; }
Quick Optimization Tip
Your checkPowerofTwo function can be simplified (and made faster) using bitwise operations. A number is a power of 2 if it's non-zero and x & (x-1) equals 0 (this works because powers of 2 have exactly one '1' bit in binary):
int checkPowerofTwo(int x) { return (x != 0) && ((x & (x - 1)) == 0); }
内容的提问来源于stack exchange,提问作者Someone

