C#练习题答案: 猜猜数组矩阵【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#练习题答案: 猜猜数组矩阵【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4359字,纯文字阅读大概需要7分钟。
内容图文
![C#练习题答案: 猜猜数组矩阵【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战](/upload/InfoBanner/zyjiaocheng/736/2e3d1d38e89f4d20a0453bf0bd6fd675.jpg)
猜猜数组矩阵【难度:2级】:
答案1:
namespace Solution
{
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
[TestFixture]
public class Sample_Tests
{
private static IEnumerable<TestCaseData> testCases
{
get
{
yield return new TestCaseData(new ArrayWrapper().Wrap(new int[] {2, 3, 3}), 3).Returns(new int[] {2, 3, 3});
yield return new TestCaseData(new ArrayWrapper().Wrap(new int[] {6, 6, 6}), 3).Returns(new int[] {6, 6, 6});
yield return new TestCaseData(new ArrayWrapper().Wrap(new int[] {1, 2, 3, 4, 5, 6, 7}), 7).Returns(new int[] {1, 2, 3, 4, 5, 6, 7});
yield return new TestCaseData(new ArrayWrapper().Wrap(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), 10).Returns(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
}
}
[Test, TestCaseSource("testCases")]
public int[] SampleTests(Func<int, int, int> f, int i) => Kata.Guess(f, i);
}
[TestFixture]
public class Random_Tests
{
private static Random rnd = new Random();
[Test, Description("Random Tests")]
public void RandomTests()
{
const int Tests = 100;
for (int i = 0; i < Tests; ++i)
{
int[] arr = new int[rnd.Next(100, 1000)].Select(_ => rnd.Next(0, 10000)).ToArray();
Func<int, int, int> f = new ArrayWrapper().Wrap(arr);
Assert.AreEqual(arr, Kata.Guess(f, arr.Length));
}
}
}
}?
答案2:
using System;
public class Kata
{
public static int[] Guess(Func<int, int, int> function, int length)
{
int[] res = new int[length];
for (int i = 1; i < length - 1; i++)
{
res[i] = (function(i - 1, i) + function(i, i + 1) - function(i - 1, i + 1)) / 2;
}
res[0] = function(0, 1) - res[1];
res[length - 1] = function(length - 2, length - 1) - res[length - 2];
return res;
}
}?
答案3:
using System;
using System.Collections.Generic;
using System.Linq;
public class Kata
{
public static int[] Guess(Func<int, int, int> f, int i)
{
int sum = (f(0, 1) + f(1, 2) + f(0, 2)) / 2;
int first = sum - f(1, 2);
List<int> result = new List<int> {first};
for (int j = 1; j < i; ++j)
{
result.Add(f(j, j - 1) - result.Last());
}
return result.ToArray();
}
}?
答案4:
using System;
using System.Collections.Generic;
using System.Linq;
public class Kata
{
public static int[] Guess(Func<int, int, int> f, int i)
{
int inv=(f(0,1)+f(1,2)+f(0,2))/2,
ff=inv-f(1,2);
List<int> lst=new List<int>{ff};
for(int n=1; n<i; ++n)
lst.Add(f(n,n-1)-lst.Last());
return lst.ToArray();
}
}?
答案5:
using System;
public class Kata {
public static int[] Guess( Func<int, int, int> f, int i ) {
var result = new int[i];
var c1 = f( 0, 1 );
var c2 = f( 1, 2 );
var c3 = f( 0, 2 );
result [ 0 ] = ( c3 + c1 - c2 )/2;
result [ 1 ] = c1 - result [ 0 ];
result [ 2 ] = c2 - c1 + result [ 0 ];
var prev = result [ 2 ];
for ( int j = 3; j < result.Length; j++ ) {
var sum = f( j - 1, j );
result [ j ] = sum - prev;
prev = result [ j ];
}
return result;
}
}?
答案6:
using System;
public class Kata
{
public static int[] Guess(Func<int, int, int> f, int i)
{
var arr = new int[i];
var a = f(0, 1);
var b = f(1, 2);
var c = f(0, 2);
arr[0] = (a - b + c) / 2;
arr[1] = (a + b - c) / 2;
arr[2] = (-a + b + c) / 2;
for (int j = 3; j < arr.Length; j++)
{
var r = f(j - 1, j);
arr[j] = r - arr[j - 1];
}
return arr;
}
}?
答案7:
using System;
using System.Linq;
public class Kata
{
public static int[] Guess(Func<int, int, int> f, int l)
{
int[] ns = new int[l];
int a = f(0, 1), b = f(0, 2) - f(1, 2);
ns[0] = (a + b) / 2;
ns[1] = a - ns[0];
for (int i = 2; i < l; i++) ns[i] = f(i - 1, i) - ns[i - 1];
return ns;
}
}
?
答案8:
using System;
using System.Linq;
public class Kata
{
public static int[] Guess(Func<int, int, int> f, int l)
{
int[] ns = new int[l];
int a = f(0, 1), b = f(1, 2), c = f(0, 2);
ns[0] = (a + c - b) / 2;
ns[1] = (a + b - c) / 2;
int i = 2;
while (i < l - 1)
{
a = b;
b = f(i, i + 1);
c = f(i - 1, i + 1);
ns[i++] = (a + b - c) / 2;
}
ns[i] = (c + b - a) / 2;
return ns;
}
}
?
答案9:
using System;
public class Kata
{
public static int[] Guess(Func<int, int, int> f, int l)
{
int a;
int s1 = f(0,1);
int s2 = f(0,2);
int s3 = f(1,2);
a = (s2 + s1 - s3)/2;
int[] r = new int[l];
r[0] = a;
for(int i = 1; i < l; i++)
{
r[i] = f(i-1, i) - r[i-1];
}
return r;
}
}?
答案10:
算法游戏拼图Python编程练习```python
using System;
public class Kata
{
public static int[] Guess(Func<int, int, int> f, int i)
{
int[] result = new int[i];
for (int j=0; j<i-2; j++) {
int sum1 = f(j,j+1);
int sum2 = f(j,j+2);
int sum3 = f(j+1, j+2);
result[j] = (sum1-sum3+sum2) /2;
result[j+1] = sum1 - result[j];
result[j+2] = sum2 - result[j];
}
return result;
}
}?
Python面试题库和答案
内容总结
以上是互联网集市为您收集整理的C#练习题答案: 猜猜数组矩阵【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战全部内容,希望文章能够帮你解决C#练习题答案: 猜猜数组矩阵【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。