请实现以下算法,语言不限,也可以是伪代码。
1.有一个数组 a[1000]存放了1000整数,这 1000 个数都大于等于 1,小于等于999,并且只有两个数是相同的,剩下的 998 个数均不相同。请写一个最优搜索算法,找出相同的那个数的值,并给出该算法的时间复杂度。
两种方法,
1、先把数组从小到大排序,再用二分法查找。
2、运用冒泡排序
第一种方案代码
private void button13_Click(object sender, EventArgs e)
{
int[] data = new int[1000];
for (int k = 0; k < data.Length; k++)
{
data[k] = k+1;
}
data[569] = 567;
// 添加测试数据
result(data);
}
/**
* 调用分搜索算法的方法实现查找相同元素
* @param data
*/
public static void result(int[] data)
{
Array.Sort(data);
for (int i = 0; i < data.Length; i++)
{
int target = data[i];
data[i] = 0;
int result = binaryFind(data, target);
if (result != -1)
{
MessageBox.Show("result="+ i + "data[result]" + data[result].ToString());
//System.out.println("相同元素为:" data[result]);
break;
}
}
}
/*二分搜索算法实现
*
* @param data
* 数据集合
* @param target
* 搜索的数据
* @return 返回找到的数据的位置,返回-1表示没有找到。
*/
public static int binaryFind(int[] data, int target)
{
int start = 0;
int end = data.Length - 1;
while (start <= end)
{
int middleIndex = (start + end) / 2;
if (target == data[middleIndex])
{
return middleIndex;
}
if (target >= data[middleIndex])
{
start = middleIndex + 1;
}
else
{
end = middleIndex - 1;
}
}
return -1;
}
第二种方案代码
int[] data = new int[1000];
for (int k = 0; k < data.Length; k++)
{
data[k] = k + 1;
}
data[999] = 1;
int result=-1;
for (int i = 0; i < data.Length - 1; i++)
{
for (int j = 0; j < data.Length - 1; j++)
{
int k = j + 1;
if (data[j] > data[k])
{
//交换位置
data[j] = data[j] + data[k];
data[k] = data[j] - data[k];
data[j] = data[j] - data[k];
}
else
{
if (data[j] == data[k])
{
result = data[j];
break;
}
}
}
}
StringBuilder stringBuilder = new StringBuilder();
for (int k = 0; k < data.Length; k++)
{
stringBuilder.AppendLine($"data[{k}]={data[k]}");
}
MessageBox.Show("result=" + result + stringBuilder.ToString());
2.给出任意正整数x(x小于2的31次幂),求不比x小且是2的整数次幂中最小的值Y。例如X=7,则Y为8;X=8,则Y为8。
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a number X: ");
int X = Convert.ToInt32(Console.ReadLine());
int Y = FindNextPowerOfTwo(X);
Console.WriteLine($"The smallest power of two that is not less than X is {Y}");
}
static int FindNextPowerOfTwo(int X)
{
if ((X & (X - 1)) == 0)
return X;
return 1 << (31 - (31 - 1 - BitPosition(X - 1)));
}
static int BitPosition(int n)
{
int pos = 0;
while (n != 0)
{
n >>= 1;
pos++;
}
return pos;
}
}
3.现有一数据文件 data.csv,里面有1000万条时序数据(按时间升序),共两列,第1列为时间(日期时间类型,到秒),第2列为值(单精度类型)。请输出每分钟的平均值。数据格式如下:
...
2017\8\6 5:14:00,803.1387
2017\8\6 5:14:01,803.142
2017\8\6 5:14:02,803.1453
2017\8\6 5:14:03,803.1486
2017\8\6 5:14:04,803.152
2017\8\6 5:14:05,803.1553
2017\8\6 5:14:06,803.1586
2017\8\6 5:14:07,803.1619
2017\8\6 5:14:08,803.1652
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
public class Data
{
public DateTime Time { get; set; }
public float Value { get; set; }
}
public class Program
{
public static void Main()
{
var data = new List<Data>();
using (var reader = new StreamReader("data.csv"))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var parts = line.Split(',');
data.Add(new Data
{
Time = DateTime.Parse(parts[0]),
Value = float.Parse(parts[1])
});
}
}
var groupedData = data.GroupBy(x => x.Time.Minute)
.Select(g => new
{
Minute = g.Key,
AverageValue = g.Average(x => x.Value)
});
foreach (var item in groupedData)
{
Console.WriteLine($"Minute: {item.Minute}, Average Value: {item.AverageValue}");
}
}
}
4.请输出 2的1000 次方的值。
using System;
using System.Numerics;
class Program
{
static void Main()
{
BigInteger result = BigInteger.Pow(2, 1000);
Console.WriteLine("2的1000次方的值为: " + result);
}
}
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376