这是一个庞大项目的最后一部分。我的项目的问题是用 C 语言编写可以从十进制转换为二进制的程序。我做了所有的事情,但是,我只坚持了一部分。分成两个数组后 - 一个数组包含 1 的位置,另一个数组包含 0 的位置。例如数字 19。我的数组有:
array1 = {4,1,0}//分别是pow(2,4)、pow(2,1)和pow(1,0)
。显然,这些数字应该替换为数字 1。
同样,array2 = {3,2}//分别是 pow(2,3) 和 pow(2,2)
表示数字应替换为数字 0。
我的问题是:有没有办法将这两个数组组合并排序为一个新数组。最终,我们需要比较新数组的值,寻找重复的值来替换0和1。
示例:让我们看一下数字 19;
array1 = {4,1,0};
array2 = {3,2};
newarray = {4,3,2,1,0};
expectedoutput = {1 0 0 1 1};
下面是我从十进制转换为二进制的代码,但由于上面 Unresolved 问题,它没有完成。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main(){
int number,number1,y,i,total=0,z,a,ya,a1,m,n,count1=0,count2=0;
int array1[10];
int array2[10];
float x,xa;
printf("Enter the number you want to convert from decimal integer to binary: \n");
scanf(" %d",&number1);
number = number1;
x = log2f(number1);
y = floor(x);
while(y!=0){
for (m=0;m<=100;m++){
x = log2f(number1);
y = floor(x);
number1 = number1 - pow(2,y);
//array1 = (int * )malloc(y * sizeof(int));
array1[m] = y;
count1 += 1;
if (number1==0){
break;
}
}
}
x = log2f(number);
y = floor(x);
for (i=0;i<=y;i++){
z = pow(2,i);
total += z;
}
a = total - number;
a1=a;
xa = log2f(a);
ya = floor(xa);
while(ya!=0){
for (n=0;n<=100;n++){
xa = log2f(a);
ya = floor(xa);
a = a - pow(2,ya);
array2[n] = ya;
count2 += 1;
if (a==0){
ya = 0;
break;
}
}
}
最佳答案
假设 array1
和array2
按降序排序,这是创建二进制数组 expectedoutput
的一种方法是通过连续比较 array1 和 array2 的第一个元素。前两个元素中较大的一个将是发送到 expectedoutput
的值。作为 1
或0
。然后将较大的第一个元素从其包含数组中删除。这是一个例子:
/*
expectedoutput = {}
array1 = {4, 1, 0}
array2 = {3, 2}
procedure: --------------------------------------------------------------------
(any element from array1 is added to expectedoutput as 1, and from array2 as 0)
{4, 1, 0} ------- ------- {3, 2}
^ | ^
4 > 3 --> array1 = {1, 0} , expectedoutput = {1}
|
{1, 0} ------- ------- {3, 2}
^ | ^
1 < 3 --> array2 = {2}, expectedoutput = {1, 0}
|
{1, 0} ------- ------- {2}
^ | ^
1 < 2 --> array2 = {}, expectedoutput = {1, 0, 0}
|
{1, 0} ------- ------- {}
|
----------> array2 is empty, so add 1 to expectedoutput
until array1 becomes empty
Now: array1 = {}, array2 = {},
expectedoutput = {1, 0, 0, 1, 1}
*/
由于我们处理的是 1 和 0,因此最好将空数组设置为一些无效值,例如 -1
。您可以使用memset
初始声明为int array1[10]; memset(array1, -1, sizeof(array1));
这样稍后array1 = {4, 1, 0, -1, -1, -1, -1, -1, -1, -1}
。与任何其他数组类似。
现在我们可以通过编写两个函数来实现上述过程:一个函数从数组中删除第一个元素,另一个函数创建 expectedoutput
使用两个数组。
删除数组的第一个元素:
// remove_first(arr, len) removes the first element of arr[len]
// For example: arr[5] = {3, 2, 6, -1, -1}
// remove_first(arr, 5)
// arr[5] == {2, 6, -1, -1, -1}
void remove_first(int *arr, int len) {
for (int i = 0; i < len-1; ++i) {
arr[i] = arr[i+1];
}
arr[len-1] = -1;
}
创建二进制数组:
// create_binary_array(arr, arr1, len1, arr2, len2) updates arr by setting
// its elements to either 1 or 0, using arr1 and arr2
// For example: arr[10] = {}, arr1[10] = {3, 1}, arr2[10] = {2}
// create_binary_array(arr, arr1, 10, arr2, 10)
// arr == {1, 0, 1}, arr1 == {}, arr2 == {}
void create_binary_array(int *arr, int *arr1, int len1, int *arr2, int len2) {
int i = 0;
while (arr1[0] != -1 || arr2[0] != -1) {
if (arr1[0] == -1) {
while (arr2[0] != -1) {
arr[i] = 0;
++i;
remove_first(arr2, len2);
}
} else if (arr2[0] == -1) {
while (arr1[0] != -1) {
arr[i] = 1;
++i;
remove_first(arr1, len1);
}
} else if (arr1[0] > arr2[0]) {
arr[i] = 1;
++i;
remove_first(arr1, len1);
} else if (arr1[0] < arr2[0]) {
arr[i] = 0;
++i;
remove_first(arr2, len2);
}
}
}
现在你可以拥有:
int expectedoutput[10];
memset(expectedoutput, -1, sizeof(expectedoutput));
int array1[10] = {4, 1, 0, -1, -1, -1, -1, -1, -1, -1};
int array2[10] = {3, 2, -1, -1, -1, -1, -1, -1, -1, -1};
create_binary_array(expectedoutput, array1, 10, array2, 10);
// Now: expectedoutput == {1, 0, 0, 1, 1, -1, -1, -1, -1, -1}
关于对两个不同大小的数组进行组合和排序。然后在C程序中将其替换为0和1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42603694/