判断一个数组中的数字是否连续
来源:互联网 发布:初中生考编程 编辑:程序博客网 时间:2024/06/04 20:50
问题描述:一个数组中若干个非负整数是否连续,其中0可以代表任何数字
Input:
8
1 3 5 0 0 0 6 7
Output:
this array is continue
算法实现:
#include <iostream>
using namespace std;
void sort(int* a,int m)
{
int temp=0;
for (int i=0;i<m-1;i++)
{
for (int j=0;j<m-i-1;j++)
{
if (a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
bool Fun(int* a,int m)
{
int sum=0,j=0;
for (int i=1;i<m;i++)
{
if(a[i-1]!=0)
{
sum +=(a[i]-a[i-1]-1);
}
else
j++;
}
if(j >= sum)
return true;
else
return false;
}
void main()
{
int m;
cout<<"m=";
cin>>m;
int *a = new int[m];
for (int i=0; i<m;i++)
{
cin>> a[i];
}
for (int j=0;i<m;i++)
{
cout<<a[j]<<" ";
}
sort(a,m);
if(Fun(a,m))
{
cout<<"this array is continue!"<<endl;
}
else
{
cout<<"is not continue!"<<endl;
}
system("pause");
}
后来想了想觉得还是有漏洞,修改了一下
#include <iostream>
using namespace std;
void sort(int* a,int m)
{
int temp=0;
for (int i=0;i<m-1;i++)
{
for (int j=0;j<m-i-1;j++)
{
if (a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
bool Fun(int* a,int m)
{
int sum=0,j=0;
for (int i=1;i<m;i++)
{
if(a[i-1]!=0)
{
int temp = a[i]-a[i-1]-1;
if (temp>=0)
{
sum += temp;
}
else
return false;
}
else
j++;
}
if(j >= sum)
return true;
else
return false;
}
void main()
{
int m;
cout<<"m=";
cin>>m;
int *a = new int[m];
for (int i=0; i<m;i++)
{
cin>> a[i];
}
for (int j=0;i<m;i++)
{
cout<<a[j]<<" ";
}
sort(a,m);
if(Fun(a,m))
{
cout<<"this array is continue!"<<endl;
}
else
{
cout<<"is not continue!"<<endl;
}
system("pause");
}
- 判断一个数组中的数字是否连续
- 判断数组中的数字是否连续相邻
- 数组之判断一个数组中的数值是否连续相邻
- 判断一个数组中的数值是否连续相邻
- java 判断一个数组中的数值是否连续相邻
- 如何判断一个数组中的数值是否连续相邻
- 判断字符串中的数字是否相同,连续
- 判断数组中的数值是否连续
- 拆分出一个数组中的连续数字
- 输入5个数字,如果数组中有0则0可以替代任何数字,判断该数组中的数字是否是连续数字。
- 提取字符串中的数据,判断数字是否为连续递增
- 用java判断一个int数组是否连续
- 判断一个数组中的元素是否重复?
- 判断一个数字是否在一个数组中
- 给一串数字,判断是否连续
- 判断一个数字是否回文
- 判断数字是否可有连续数字和组成
- 程序员面试题目总结--数组(二)【二分查找、找出给定数字出现次数、两个有序整型数组交集、找出数组中唯一的重复元素、判断数组中的数值是否连续相邻】
- wince 蓝牙 学习 1
- openstack Multihost External Network 部署
- DC的卸载
- Ubuntu下安装VMware tools 的解决办法
- UVA - 10604 Chemical Reaction
- 判断一个数组中的数字是否连续
- jsp下拉框从数据库获取数据进行填充并且实现二级级联
- CONST的几种用法总结
- 折叠margin 折叠margin的消除
- 实用的CAM350快捷键命令
- Socket 通信原理 -- Android客户端和服务器以TCP&&UDP方式互通
- 如何配置OpenGL ES的上下文(OpenGL ES2.0官方文档翻译)
- 分布式文件系统及部署
- 拓扑排序 POJ 1094 Sorting It All Out