if( (select count(distanct (id) ) from table = 255)) begin print '没有重复' end else begin print '有重复' edn
是不是把问题想复杂了?如果讲数组排序,然后再进行排查看是否有重复的,那么排序的时间,不就是浪费掉了么? a[255]为需要排查的数组。 for i = 1 to 255 if a[i-1] = a[i] then '有重复的数 return true end if next i '没有重复的数 return false
保证没复杂度
正解。对提议用sql的表示无语。下次考排序,你告诉他调Sort函数就行了?
int main()
{
int temp,a[256],i;
for(i=0;i<256;i++)
{
a[i]=0;
}
for(i=0;i<256;i++)
{
scanf("%d",&temp);
if(a[temp]==0)a[temp]=1;
else {
printf("%d\n",temp);
}
}
return 0;
}
这样?
int[] b = new int[256] ;
string s = string.Empty;
foreach (int ai in a)
{
if (b[ai] != 1)
b[ai] = 1;
else
s += ai+";";//s为重复的
}
int[] b = new int[256] ;
for(int i=0;i<a.length;i++)
{
if(b[a[i]]==0)
{
b[a[i]]=a[i] //打点?
}
else
{
已被赋值过一次
}
}
好方法,这就是为什么说int类型还告诉你1~256的原因吧
c++区里问过的..
int aa[256]; 都初始化0.然后然后每个数,作为数组下标. aa[5]++之; 全部搞完后, aa[x]>1的就是重复超过1次的数了啊
这样做:
for (int i=0;i<len;i++) {
while(true){
if(arr[i] == arr[arr[i]]){
//输出重复
break;
}
if(arr[i] == i){
break; //已在所在位置
}
swap(i, arr[i]); //放到对应位置
}
}
时间复杂度:看似双重循环,实际复杂度几乎为O(n)
空间复杂度:O(1)
for (int i=0;i<len;i++) {
while(true){
if(arr[i] == arr[arr[i]]){
//输出重复
break;
}
if(arr[i] == i){
break; //已在所在位置
}
swap(i, arr[i]); //放到对应位置
}
}
{
static int N = 1;
static int men = 1;
public static void Main(String[] args)
{
while (true)
{
men = 1;
string s = Console.ReadLine();
if (s == "q")
break; N = int.Parse(s);
infection(N);
Console.WriteLine("第" + N + "天共有" + men + "个人被感染!");
Console.WriteLine("************************************");
}
} //public static void infectionOld(int totalDay)
//{
// int day;
// if (totalDay <= 5)
// {
// return;
// } // if (totalDay > 5 && totalDay <= 10)
// {
// for (day = 6; day <= totalDay; day++)
// {
// men = men + 3;
// }
// }
// else
// {
// for (day = 6; day <= 10; day++)
// {
// men = men + 3;
// infection(totalDay - day + 1);
// infection(totalDay - day + 1);
// infection(totalDay - day + 1);
// } // men--;
// }
//} public static void infection(int totalDay)
{
int day;
if (totalDay <= 5)
return; for (day = 6; day <= totalDay; day++)
{
if (day > 10)
{
men--;
break;
}
men = men + 3;
infection(totalDay - day + 1);
infection(totalDay - day + 1);
infection(totalDay - day + 1);
}
}
}
a[1] = array(1,1,1,1,1)
a[2] = array(2,2,2,)
a[3] = array(3)
....
a[255] = array(255)现在找 3 是不是有重复的.count(a[3] ) = 1 无现在找 1 是不是有重复的
count(a[1]) > 1 : 有
cout<<"有重复"<<endl;
else
{
建立一个255个字符的数组。从给定数组做循环
分别放到以当前数组值为索引的数组上,如果当前值为0则写入。若不为0则说明是重复的。
}
不知道表达清楚没有。
具体到这个题目中:
由于最大的数十255,所以我们可以用bitmap的方法来做。
首先声明一个数组unsigned int a[8];这个数组中刚好有256位,每位标识一个数,起始时各位置零;
然后扫描要处理的数组,遇到一个数就将它放到相应的位中,这个可以用位运算来完成;
扫描的过程中遇到一位为0,则将其置1,如果遇到1,则说明此处的数字有重复,程序退出即可。
首先不懂你的arr是不是指输入数组,如果是的话,那你这个代码好像有问题。
假设int[] arr = { 256, 2, 8, 1, 7, 9, 6, 79, 82, 92, 64, 154, 154, 221, 85, 96, 241, 64, 42, 131, 22, 11, 9 };
那么i=0时,arr[arr[i]]=arr[256],IndexOutOfBoundsException...如果你指的arr是
int[] arr = new int[256];
那么输入数组在哪?
begin
print '没有重复'
end
else
begin
print '有重复'
edn
a[255]为需要排查的数组。 for i = 1 to 255
if a[i-1] = a[i] then
'有重复的数
return true
end if
next i '没有重复的数
return false
int j = 0;
for(int i = 0;i < n;i++)
{
if (b[i] > a[j])
{
a[j] = b[i];
j++;
}
if (b[i] == a[j])
{
i 重复
}
if (b[i] < a[j])
{
for (int zxii = 0;zxii < j;zxii++)
{
if (b[i] == a[zxii])
{
i重复
}
}
}
}
早上测了下,
arr是源数组,此方法在arr长度大于255时有效
另外,两个 break 条件位置写反了
先声明一个数组 int flag[256];全部置0, 遇到一个数字,比如23,就 flag[23]++;标记一下,如果flag[23]>1了,就说明有重复
int[] a = { 1, 2, 8, 1, 7, 9, 6, 79, 82, 92, 64, 154, 154, 221, 85, 96, 241, 64, 42, 131, 22, 11, 9 };
int[] b = new int[256] ;
for(int i=0;i<a.length;i++)
{
b[a[i]]=b[a[i]]+1;
if (b[a[i]]>1)
{
“哈哈有重复的了”
}
}
循环一次
{
const int MAX = 270;//源数组大小
const int iBitCount = 32;
int resut = 0; int[] item = new int[MAX];//要处理的数组(源数组)
Random rnd = new Random();
uint[] num = new uint[(MAX+iBitCount-1)/iBitCount];//位数组 //初始化源数组
for (int i = 0; i < MAX;i++)
{
item[i] = rnd.Next(1, 255);//取1-255随机数初始源数组
Console.Write( i + "-" + item[i]+"\r\n" );
} //找重复
for (int j = 0; j < MAX; j++)
{ if (((num[item[j] / iBitCount]) & (1 << (item[j] % iBitCount))) != 0)
{
resut=j;
break;
}
else
{
num[item[j] / iBitCount] |= (uint)1 << (item[j] % iBitCount);
} }
if (resut != 0)
{
Console.Write("重复的数字:" + item[resut]);
Console.Write("\r\n数组位置:" + resut);
}
else
{
Console.Write("没有重复的数字!");
}
Console.ReadLine();
}