目錄
1、給定一個整數數組 nums,將數組中的元素向右輪轉 k 個位置,其中 k 是非負數。
2、無重復字符的最長字符串 ,給定一個字符串 s 請你找出其中不含有重復字符的最長字符串的長度。
3、給定兩個字符串 s 和 t ,它們只包含小寫字母。字符串 t 由字符串 s 隨機重排,然后在隨機位置添加一個字母。請找出在 t 中被添加的字母。
4、定義一個函數,用來取得一個數字的所有因子,把所有因子返回
5、輸入一個字符串,判斷其是否是合法標識符
6、輸入兩個整數 輸出兩個整數的最大公約數
1、給定一個整數數組 nums,將數組中的元素向右輪轉 k 個位置,其中 k 是非負數。
示例 1:
輸入: nums = [1, 2, 3, 4, 5, 6, 7], k = 3
輸出: [5, 6, 7, 1, 2, 3, 4]
解釋:
向右輪轉 1 步: [7, 1, 2, 3, 4, 5, 6]
向右輪轉 2 步: [6, 7, 1, 2, 3, 4, 5]
向右輪轉 3 步: [5, 6, 7, 1, 2, 3, 4]
示例 2: ? ? ? ? ? ?
輸入:nums = [-1, -100, 3, 99], k = 2
輸出:[3, 99, -1, -100]
解釋:向右輪轉 1 步: [99, -1, -100, 3]
向右輪轉 2 步: [3, 99, -1, -100]
Console.Write("請輸入一串整數,以空格隔開:");
string[] nums = (Console.ReadLine()).Split(',');
Console.Write("請輸入向右輪轉k個位置:");
int k = int.Parse(Console.ReadLine());for (int i = 0; i < k; i++)
{string temp = nums[nums.Length - 1];for (int j = nums.Length - 2; j >= 0; j--){nums[j + 1] = nums[j]; }nums[0] = temp;
}// 輸出結果,用逗號分隔
Console.WriteLine(string.Join(",", nums));
2、無重復字符的最長字符串 ,給定一個字符串 s 請你找出其中不含有重復字符的最長字符串的長度。
Console.Write("請輸入一個字符串:");string s = Console.ReadLine();int Maxlong = 0; //記錄不含有重復字符的最長字符的長度// 外層循環遍歷每個字符作為起始點for (int i = 0; i < s.Length; i++){bool[] charExists = new bool[256]; // 定義一個長度為256的空的布爾數組(每一位默認值為false),用于記錄字符是否出現過int count = 0; // 記錄不含有重復字符的長度// 內層循環檢查后續字符for (int j = i; j < s.Length; j++){char c = s[j];if (charExists[c]){break; // 發現重復字符,終止當前子串檢查}charExists[c] = true; //存儲在對應ASCII編號的位置,ASCII編號 s[j]=a; a=97; charExists[a]=charExists[97]= truecount++;}//保存最大值if (count > Maxlong){Maxlong = count;} }Console.WriteLine($"不含有重復字符的最長字符串的長度為: {Maxlong}");
3、給定兩個字符串 s 和 t ,它們只包含小寫字母。字符串 t 由字符串 s 隨機重排,然后在隨機位置添加一個字母。請找出在 t 中被添加的字母。
示例 1:
輸入:s = "abcd", t = "abcde"
輸出:"e"
解釋:'e' 是那個被添加的字母。示例 2:
輸入:s = "", t = "y"
輸出:"y"
Console.Write("請輸入一個只包含小寫字母字符串:");
string s = Console.ReadLine();Console.Write("請再輸入一個與上面輸入相同但在隨機位置添加一個字母的字符串:");
string t = Console.ReadLine();int sign = 1;
for (int i = 0; i < s.Length; i++)
{if (s[i] == t[i]){sign++;continue;}else {Console.WriteLine($"'{t[i]}'是那個被添加的字母");break;}}
if (sign == t.Length)
{ Console.WriteLine($"'{t[s.Length]}'是那個被添加的字母"); }
4、定義一個函數,用來取得一個數字的所有因子,把所有因子返回
static void Main(string[] args)
{//定義一個函數,用來取得一個數字的所有因子,把所有因子返回foreach (var t in Factor(6)){ Console .Write(t+" ");}
}static int[] Factor(int n)
{int count = 0;for (int i = 1; i <= n; i++){ if (n%i==0){count++;} }int[] ints = new int[count];int m = 0;for (int i = 1; i <= n; i++){if (n % i == 0){ints[m] =i;m++;}}return ints;
}
5、輸入一個字符串,判斷其是否是合法標識符
//輸入一個字符串,判斷其是否是合法標識符
Console.WriteLine("請輸入一個字符串:");
string str = Console.ReadLine();
Char[] s = str.ToCharArray(); //把字符串轉換為字符數組
bool b=true;
for (int i = 0; i < s.Length; i++)
{if (s[0] >= '0' && s[0] <= '9'){b = false;Console.WriteLine("不合法");break;}if (b){if (s[i] >= '0' && s[i] <= '9' || s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z' || s[i] == '_'){b = true;}else{b = false;}}
}if (b){Console.WriteLine("合法");}else{Console.WriteLine("不合法");}
6、輸入兩個整數 輸出兩個整數的最大公約數
static void Main(string[] args)
{//輸入兩個整數 輸出兩個整數的最大公約數Console.WriteLine("輸入兩個整數:");int n=int.Parse (Console .ReadLine());int m=int.Parse (Console .ReadLine());int gcd = CalculateGCD(n, m);Console.WriteLine($"最大公約數是: {gcd}");
}static int CalculateGCD(int a, int b)
{// 處理負數輸入(轉換為正數計算)a = Math.Abs(a);b = Math.Abs(b);while (b != 0){int temp = b;b = a % b;a = temp;}return a;
}