问题描述
- 请教如何用c语言去除一个数组中所有值为零的元素,而且这些零元素中有连续排列的?
-
能否给一个示例程序?感激不尽!
比如以下这个数组中有连续的0元素,如何去除所有的零元素?
double a[64]={4.63866e+020,1.456e+027,-7.67487e+017,9.86481e+016,0,0,-3.1101e+014,-9.38282e+010,
1.456e+027,4.60249e+033,-2.3969e+024,3.36857e+023,0,0,-9.64264e+020,-2.93898e+017,
-7.67487e+017,-2.3969e+024,1.27445e+015,-1.52231e+014,0,0,5.19276e+011,1.55469e+008,
9.86481e+016,3.36857e+023,-1.52231e+014,1.02833e+014,0,0,-4.00459e+010,-2.01495e+007,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
-3.1101e+014,-9.64264e+020,5.19276e+011,-4.00459e+010,0,0,2.17304e+008,62943,
-9.38282e+010,-2.93898e+017,1.55469e+008,-2.01495e+007,0,0,62943,19};
解决方案
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <math.h>
int trimzero(double arr[], int len)
{
int x = 0;
for (int i = 0; i < len; i++)
{
if (arr[i] == 0.0) continue;
arr[x] = arr[i];
x++;
}
return x;
}
int _tmain(int argc, _TCHAR* argv[])
{
double a[64] = { 4.63866e+020, 1.456e+027, -7.67487e+017, 9.86481e+016, 0, 0, -3.1101e+014, -9.38282e+010,
1.456e+027, 4.60249e+033, -2.3969e+024, 3.36857e+023, 0, 0, -9.64264e+020, -2.93898e+017,
-7.67487e+017, -2.3969e+024, 1.27445e+015, -1.52231e+014, 0, 0, 5.19276e+011, 1.55469e+008,
9.86481e+016, 3.36857e+023, -1.52231e+014, 1.02833e+014, 0, 0, -4.00459e+010, -2.01495e+007,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
-3.1101e+014, -9.64264e+020, 5.19276e+011, -4.00459e+010, 0, 0, 2.17304e+008, 62943,
-9.38282e+010, -2.93898e+017, 1.55469e+008, -2.01495e+007, 0, 0, 62943, 19 };
int n = trimzero(a, 64);
for (int i = 0; i < n; i++)
{
printf_s("%fn", a[i]);
}
return 0;
}
463866000000000000000.000000
1456000000000000000000000000.000000
-767487000000000000.000000
98648100000000000.000000
-311010000000000.000000
-93828200000.000000
1456000000000000000000000000.000000
4602490000000000100000000000000000.000000
-2396900000000000000000000.000000
336857000000000000000000.000000
-964264000000000000000.000000
-293898000000000000.000000
-767487000000000000.000000
-2396900000000000000000000.000000
1274450000000000.000000
-152231000000000.000000
519276000000.000000
155469000.000000
98648100000000000.000000
336857000000000000000000.000000
-152231000000000.000000
102833000000000.000000
-40045900000.000000
-20149500.000000
-311010000000000.000000
-964264000000000000000.000000
519276000000.000000
-40045900000.000000
217304000.000000
62943.000000
-93828200000.000000
-293898000000000000.000000
155469000.000000
-20149500.000000
62943.000000
19.000000
Press any key to continue . . .
确保顺序但是效率略低的算法
解决方案二:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <math.h>
int trimzero(double arr[], int len)
{
int last = len;
while (arr[--last] == 0.0 && last >= 0);
for (int i = 0; i < last; i++)
{
if (arr[i] == 0.0)
{
arr[i] = arr[last];
while (arr[--last] == 0.0 && last >= 0);
}
}
return last;
}
int _tmain(int argc, _TCHAR* argv[])
{
double a[64] = { 4.63866e+020, 1.456e+027, -7.67487e+017, 9.86481e+016, 0, 0, -3.1101e+014, -9.38282e+010,
1.456e+027, 4.60249e+033, -2.3969e+024, 3.36857e+023, 0, 0, -9.64264e+020, -2.93898e+017,
-7.67487e+017, -2.3969e+024, 1.27445e+015, -1.52231e+014, 0, 0, 5.19276e+011, 1.55469e+008,
9.86481e+016, 3.36857e+023, -1.52231e+014, 1.02833e+014, 0, 0, -4.00459e+010, -2.01495e+007,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
-3.1101e+014, -9.64264e+020, 5.19276e+011, -4.00459e+010, 0, 0, 2.17304e+008, 62943,
-9.38282e+010, -2.93898e+017, 1.55469e+008, -2.01495e+007, 0, 0, 62943, 19 };
int n = trimzero(a, 64);
for (int i = 0; i < n; i++)
{
printf_s("%fn", a[i]);
}
return 0;
}
解决方案三:
int del_zero(double p[], int n)
{
int i = 0, j = 0, len = n;
while (i < len){
if (p[i] != 0.0)
p[j++] = p[i];
i++;
}
return j;
}
解决方案四:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <math.h>
int trimzero(double arr[], int len)
{
int last = len;
while (arr[--last] == 0.0 && last >= 0);
for (int i = 0; i < last; i++)
{
if (arr[i] == 0.0)
{
arr[i] = arr[last];
while (arr[--last] == 0.0 && last >= 0);
}
}
return last + 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
double a[64] = { 4.63866e+020, 1.456e+027, -7.67487e+017, 9.86481e+016, 0, 0, -3.1101e+014, -9.38282e+010,
1.456e+027, 4.60249e+033, -2.3969e+024, 3.36857e+023, 0, 0, -9.64264e+020, -2.93898e+017,
-7.67487e+017, -2.3969e+024, 1.27445e+015, -1.52231e+014, 0, 0, 5.19276e+011, 1.55469e+008,
9.86481e+016, 3.36857e+023, -1.52231e+014, 1.02833e+014, 0, 0, -4.00459e+010, -2.01495e+007,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
-3.1101e+014, -9.64264e+020, 5.19276e+011, -4.00459e+010, 0, 0, 2.17304e+008, 62943,
-9.38282e+010, -2.93898e+017, 1.55469e+008, -2.01495e+007, 0, 0, 62943, 19 };
int n = trimzero(a, 64);
for (int i = 0; i < n; i++)
{
printf_s("%fn", a[i]);
}
return 0;
}
463866000000000000000.000000
1456000000000000000000000000.000000
-767487000000000000.000000
98648100000000000.000000
19.000000
62943.000000
-311010000000000.000000
-93828200000.000000
1456000000000000000000000000.000000
4602490000000000100000000000000000.000000
-2396900000000000000000000.000000
336857000000000000000000.000000
-20149500.000000
155469000.000000
-964264000000000000000.000000
-293898000000000000.000000
-767487000000000000.000000
-2396900000000000000000000.000000
1274450000000000.000000
-152231000000000.000000
-293898000000000000.000000
-93828200000.000000
519276000000.000000
155469000.000000
98648100000000000.000000
336857000000000000000000.000000
-152231000000000.000000
102833000000000.000000
62943.000000
217304000.000000
-40045900000.000000
-20149500.000000
-40045900000.000000
519276000000.000000
-964264000000000000000.000000
-311010000000000.000000
Press any key to continue . . .
解决方案五:
int del_zero(double p[],int n)
{
int i;
int j;
int len=n;
for(i=0;i<len;i++)
{
if(p[i]>=-DBL_EPSILON||p[i]<=DBL_EPSILON)
{
for(j=i;j<len;j++)
{
p[j]=p[j+1];
}
len--;
}
}
return len;
}
我试过了 ok
解决方案六:
int del_zero(double p[],int n)
{
int i,j,len=n;
for(i=0;i<len;i++){
if(p[i]==0.0){
for(j=i;j<len;j++){
p[j]=p[j+1];
}
len--;
}
}
return len;
}
这种方法不能去除连0,求大神帮忙,给多少分都行!
解决方案七:
楼上的这个方法可行,运行结果也正确!
解决方案八:
int del_zero(double p[], int n)
{
int i = 0, j, len = n;
while (i < len){
if (p[i] == 0.0){
for (j = i; j < len; j++){
p[j] = p[j + 1];
}
len--;
}
else
i++;
}
return len;
}
已修改,测试OK
解决方案九:
这是优化连续为0时效率的改进版。
int del_zero(double p[], int n)
{
int i = 0, j, k, len = n;
while (i < len){
if (p[i] == 0.0){
j = 1;
while (i + j < len){
if (p[i + j] == 0.0)
++j;
else
break;
}
for (k = i; k < len; k++){
p[k] = p[k + j];
}
len -= j;
}
else
++i;
}
return len;
}
解决方案十:
如果还需要优化性能可以写在评论里。。