找牛客网做做基础题,争取每个单词要自己打,尽量不要百度( 定义 写法 什么的)
题目描述1
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
array = int[2][];
array[0] = new int[]{1,2,3,4,5};
array[1] = new int[]{6,7,8,9,10};
target = 8;
foreach(int[] arr in array)
{
System.Collections.IEnumerator arr1 = arr.GetEnumerator();
while (arr1.MoveNext())
{
if (arr1.Current.Equals(c))
{
return true;
}
}
}
true false;
题目描述2
计算字符串最后一个单词的长度,单词以空格隔开。
// 用Trim()是因为如果最后一空格结尾,我认为最后一个单词应该是空格前面的
// 还有一点,复制文本进小黑框,会莫名其妙要按两回回车,读取就多一个空格
string str = ReadLine().Trim();
while (str.Length > 0 && str.Length < 5000)
{
int start = str.LastIndexOf(' ') + 1;
WriteLine(str.Length - start);
break;
}
题目描述3
写出一个程序,接受一个由字母和数字和空格组成的字符串,再输入一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
转载博客园一个大神总结的正则表达式https://www.cnblogs.com/hehehehehe/p/6043710.html。其实正则很简单的,只要你找到匹配的正则表达,如空格是\s(\\s就是\s),0-9就是0-9,大写A-Z就是A-Z,然后直接拼就行了,就是这么简单!
string str = ReadLine().ToUpper().Trim();
while (Regex.IsMatch(str, "^+[0-9A-Z\\s]+$"))
{
string byt = ReadLine().ToUpper().Trim();
if (byt.Length ==1)
{
string[] arr = str.Split(byt);
WriteLine(arr.Length - 1);
break;
}
}
题目描述4
输入N代表有N个数,然后依次输入N个数,对这组数进行“去重”和“排序”后,按照从小到大的次序输出。
public class Program
{
static void Main(string[] args)
{
int count;
while (int.TryParse(Console.ReadLine(),out count))
{
List<int> list = new List<int>();
for (int i = 0; i < count; i++)
{
int num;
if(int.TryParse(Console.ReadLine(),out num))
{
if (!list.Contains(num))
{
list.Add(num);
}
}
else
{
return;
}
}
for (int j = 0; j < list.Count; j++)
{
for (int k = j + 1; k < list.Count; k++)
{
if (list[j] > list[k])
{
int value = list[j];
list[j] = list[k];
list[k] = value;
}
}
}
foreach (int numID in list)
{
Console.WriteLine(numID);
}
}
int dasiodjaisjd = 10;
}
}
方法二:
public class Program
{
static void Main(string[] args)
{
int count;
while (int.TryParse(Console.ReadLine(), out count))
{
string str = ",";
for (int i = 0; i < count; i++)
{
string num = Console.ReadLine();
if (Regex.IsMatch(num, "^+[0-9]+$"))
{
if (str.Contains("," + num + ","))
{
continue;
}
str += num + ",";
}
else
{
return;
}
}
string[] id = str.Split(",");
for (int j = 0; j < id.Length; j++)
{
for (int k = j + 1; k < id.Length; k++)
{
if (id[j].Equals(""))
{
break;
}
else if (!id[k].Equals("") && (int.Parse(id[j]) > int.Parse(id[k])))
{
string value = id[j];
id[j] = id[k];
id[k] = value;
}
}
}
foreach (string numID in id)
{
if (!numID.Equals(""))
{
Console.WriteLine(numID);
}
}
}
}
}
题目描述5
输入两组字符串,每组都按8位截取,输出,不足8位的补0
class Program{
static void Main(string[] args)
{
string[] input = new string[2];
input[0] = Console.ReadLine();
input[1] = Console.ReadLine();
foreach (string str in input)
{
if(!string.IsNullOrEmpty(str))
{
GetNewStr(str);
}
}
}
static void GetNewStr(string str)
{
string newStr=null;
if (str.Length == 8)
{
Console.WriteLine(str);
}
else if(str.Length < 8)
{
newStr = str;
for (int i=0;i<8-str.Length;i++)
{
newStr += "0";
}
Console.WriteLine(newStr);
}
else
{
for (int j=0;j<(str.Length/8)+1;j++)
{
if (j == str.Length / 8)
{
if (str.Length % 8 == 0)
{
return;
}
newStr = str.Substring(j * 8, str.Length-j*8);
}
else
{
newStr = str.Substring(j * 8, 8);
}
GetNewStr(newStr);
}
}
}
}
题目描述6
写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。
class Program
{
static void Main(string[] args)
{
string str = null;
while ((str=Console.ReadLine())!=null)
{
str = str.Substring(2).ToUpper();
char[] c = str.ToCharArray();
int[] er = new int[4 * c.Length];
for (int i = 0; i < c.Length; i++)
{
switch (c[i])
{
case 'A':
Rhuan((c.Length - i) * 4, "10", er);
break;
case 'B':
Rhuan((c.Length - i) * 4, "11", er);
break;
case 'C':
Rhuan((c.Length - i) * 4, "12", er);
break;
case 'D':
Rhuan((c.Length - i) * 4, "13", er);
break;
case 'E':
Rhuan((c.Length - i) * 4, "14", er);
break;
case 'F':
Rhuan((c.Length - i) * 4, "15", er);
break;
default:
Rhuan((c.Length - i) * 4, c[i].ToString(), er);
break;
}
}
// 转十进制
int sum = 0;
for (int k = 0; k < er.Length; k++)
{
int s = 0;
if (k == 0 && er[k] == 1)
{
sum = 1;
}
else if (er[k] == 1)
{
s = 1;
for (int z = 0; z < k; z++)
{
s = s * 2 * er[k];
}
}
sum += s;
}
Console.WriteLine(sum);
}
}
public static void Rhuan(int i, string num,int[] er)
{
if (num == "0")
{
for (int j = 1; j < 5; j++)
{
er[i - 1] = 0;
}
}
else
{
int a = int.Parse(num);
er[i - 4] = a % 2;
er[i - 3] = (a / 2) % 2;
er[i - 2] = (a / 4) % 2;
er[i - 1] = a / 8;
}
}
}
题目描述7
写出一个程序,接受一个自然数,输出该数的所有质数因子。
using static System.Console;
using System.Collections.Generic;
namespace Test{
class programe{
static void Main(string[] args){
int num;
string str = ReadLine();
List<int> vs = new List<int>();
if (int.TryParse(str,out num))
{
for (int i=2;i<=num;i++)
{
if (num % i == 0)
{
vs.Add(i);
num = num / i;
i = 1;
}
}
foreach (int a in vs)
{
Write(a+" ");
}
}
}
}
}
题目描述8
写出一个程序,接受一个正浮点数,输出该数四舍五入后的整数。
using System;
namespace Test{
class program{
static void Main(string[] args){
string str = Console.ReadLine();
string[] arg = str.Split('.');
char[] ch = str.ToCharArray();
int num;
int.TryParse(ch[Array.IndexOf(ch, '.') + 1].ToString(),out num);
if(num >= 5){
Console.Write(int.Parse(arg[0])+1);
}
else{
Console.Write(int.Parse(arg[0]));
}
}
}
}
题目描述9
输入N对【索引key 记录值value】(小标和值由空格隔开),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。
static void Main(string[] args)
{
string str = Console.ReadLine();
Dictionary<int, int> dic = new Dictionary<int, int>();
int num;
if (int.TryParse(str, out num))
{
for (int i = 0; i < num; i++)
{
string[] a = Console.ReadLine().Split(" ");
if(dic.ContainsKey(int.Parse(a[0])))
{
dic[int.Parse(a[0])] = dic[int.Parse(a[0])] + int.Parse(a[1]);
}
else
{
dic.Add(int.Parse(a[0]), int.Parse(a[1]));
}
}
}
var yy = dic.OrderBy(x => x.Key).ToList();
foreach (var a in yy)
{
Console.WriteLine(a.Key.ToString()+" "+a.Value.ToString());
}
}
题目描述10
输入一个int型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
public class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
char[] ch = str.ToCharArray();
string[] c = new string[ch.Count()];
for (int i = ch.Count() - 1; i >= 0; i--)
{
if (!c.Contains(ch[i].ToString()))
{
c[i] = ch[i].ToString();
Console.Write(ch[i]);
}
}
}
}