public static char GetFirstChar(string str)
{
char result = ' ';
for (int i = 0; i < str.Length; i++)
{
int num = 0;
for (int j = 0; j < str.Length; j++)
{
if (str[i].Equals(str[j]))
{
num++;
}
}
if (num == 1)
{
result = str[i];
break;
}
}
return result;
}
2.输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。
如果有多对数字的和等于输入的数字,输出任意一对即可。例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4和11
public
static void GetTheRightValue(int[] arr, int a)
{
List<int> list = new List<int>();
for (int i = 0; i < arr.Length; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if ((arr[i] + arr[j]) == a)
{
list.Add(arr[i]);
list.Add(arr[j]);
}
}
}
for (int i = 0; i < list.Count; i++)
{
Console.Write(list[i].ToString() + " ");
if (i % 2 != 0)
{
Console.WriteLine();
}
}
}
3.求1+2+…+n,
要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
public
static int add(int n) {
int s = n;
if (n != 0) {
s +=add(n - 1);
}
return s;
}
4.输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数。
例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次。
//下面代码是100以内的数字的处理
public static int GetTheNumber(int n) {
int num = 0;
int s = n / 10;
int s1 = n % 10;
if (s >= 2 && s1 >= 1)
{
num = 12 + s - 1;
}
else if (s >= 2 && s1 <1)
{
num = 12 + s - 2;
}
else if (s == 1 && s1 >= 1)
{
num = s1 + 3;
}
else if (s == 1 && s1 < 1)
{
num = 2;
}
else {
num = 1;
}
return num;
}