《C程序設(shè)計(jì)》期末題庫_第1頁
《C程序設(shè)計(jì)》期末題庫_第2頁
《C程序設(shè)計(jì)》期末題庫_第3頁
《C程序設(shè)計(jì)》期末題庫_第4頁
《C程序設(shè)計(jì)》期末題庫_第5頁
已閱讀5頁,還剩39頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、C#考試題庫一、簡單題1.把輸入的字符串str1中的內(nèi)容逆置,并保存到字符串str2中,輸出str2的內(nèi)容。例如:str1=“abc”,str2則為space one class Program static void Main(string args) Console.WriteLine(請?輸?入?字?符?串?.); String str1 = Console.ReadLine(); Console.WriteLine(正y在處|理數(shù)y據(jù)Y.); char data = str1.ToCharArray(); Array.Reverse(data); String str2

2、 = new String(data); Console.WriteLine(翻-轉(zhuǎn)a后的?數(shù)y據(jù)Y是?:o+str2); Console.ReadLine(); 2.已知三角形三條邊長a,b,c,三邊由用戶輸入,計(jì)算三角形面積公式為: s=0.5*(a+b+c) area=s*s-a*s-b*(s-c) 求三角形的面積area。 3.輸入一個字符串str1,刪除str1中其中所有的小寫字符a,輸出處理后的字符串。namespace one class Program static void Main(string args) String str1; Console.WriteLine(請輸

3、入字符串.); str1 = Console.ReadLine(); Console.WriteLine(result is : + str1.Replace(a, ); Console.ReadLine(); 4.輸入7個數(shù),計(jì)算平均值,并把低于平均值的數(shù)據(jù)輸出。5.輸入7個數(shù),計(jì)算平均值,并把高于平均值的數(shù)據(jù)輸出。6.輸入10個數(shù),找出其中最大數(shù)和次最大數(shù)。7.輸入10個數(shù),找出其中最小數(shù)和次最小數(shù)。8.輸入若干(不超過10個)有序的正整數(shù),對于相同的數(shù)據(jù)只保留一個,輸出保留的數(shù)據(jù)。例如,輸入數(shù)據(jù)是: 2,2,2,3,3,4,5,5,6,6,9,9,9,10,10,10 最終的輸出結(jié)果是:

4、 2,3,4,5,6,9,10。namespace one class Program static void Main(string args) string str1; string ret = ; /任?意a值 str1 = Console.ReadLine(); for (int i = 0; i = 0) continue; ret = ret + ch.ToString(); Console.WriteLine(ret); Console.ReadLine(); 法二:for (int i = 1; i =num.Length;i+ ) if(i=num.Length) Conso

5、le.Write(numi-1); break; if (numi-1 = numi) continue; else Console.Write(numi-1 + ); 9.輸入由數(shù)字字符組成的字符串,將其轉(zhuǎn)換成為一個整數(shù)。例如若輸入字符串3425,得到結(jié)果為整數(shù)值3425。namespace one class Program static void Main(string args) string str1; int result; Console.WriteLine(請?輸?入?數(shù)y字?字?符?串?:o); str1 = Console.ReadLine(); try result =

6、int.Parse(str1); catch (Exception) Console.WriteLine(你?輸?入?的?字?符?串?有D誤!?,請?重?新?輸?入?); return; Console.WriteLine(result is :+result); Console.ReadLine(); 10.輸入20個正整數(shù),把其中的奇數(shù)和偶數(shù)分別保存并輸出。namespace one class Program static void Main(string args) int str1 = new int20; ArrayList list1 = new ArrayList(); Arr

7、ayList list2 = new ArrayList(); Console.WriteLine(請?輸?入?數(shù)y字?:o); for (int i = 0; i str1.Length;i+ ) str1i = int.Parse(Console.ReadLine(); for (int i = 0; i str1.Length; i+) if (str1i % 2 = 0) list2.Add(str1i); else list1.Add(str1i); Console.WriteLine(*奇?數(shù)y*); for (int i = 0; i list1.Count;i+ ) Conso

8、le.Write(list1i.ToString()+ ); Console.Write(n); Console.WriteLine(*偶?數(shù)y*); for (int i = 0; i list2.Count; i+) Console.Write(list2i.ToString() + ); Console.Write(n); Console.ReadLine(); 11.從終端輸入3個數(shù)a、b、c,按從大到小的順序輸出。class Program static void Main(string args) ArrayList list2 = new ArrayList(); Console

9、.WriteLine(start.); for (int i = 0; i 3;i+ ) list2.Add(Console.ReadLine(); Console.WriteLine(please waite.); list2.Sort(); for (int i = 0; i 3; i+) Console.Write(list2i+ ); Console.ReadKey(); 13.求n以內(nèi)(不包括n)同時能被3和7整除的所有自然數(shù)之和的平方根s,n從鍵盤輸入。例如若n為1000時,函數(shù)值應(yīng)為:s=153.909064。class Program static void Main(stri

10、ng args) int n; float sum=0; Console.WriteLine(please input n.); try n = int.Parse(Console.ReadLine(); catch (Exception) Console.WriteLine( n is not a number,?please input again.); n = int.Parse(Console.ReadLine(); for (int i = 0; i n;i+ ) if (i % 3 = 0) & (i % 7 = 0) sum += i; Console.WriteLine(ple

11、ase waite.); Console.WriteLine(result is +Math.Sqrt(sum); Console.ReadKey(); 14.輸入110之間的一個數(shù)字,輸出它對應(yīng)的英文單詞。例如輸入1 輸出one。 Switch(data) Case 1:Break;15.個位數(shù)為6且能被3整除但不能被5整除的三位自然數(shù)共有多少個,統(tǒng)計(jì)個數(shù),并輸出這些數(shù)。class Program static void Main(string args) ArrayList list = new ArrayList(); int n = 0; Console.WriteLine(pleas

12、e waite.); for (int i = 100; i 1000;i+ ) if(i%100%10=6 &i%3=0&i%5!=0 ) n+; Console.WriteLine(i= + i); Console.WriteLine(total n =+n); Console.ReadKey(); 16.輸入一個字符串,用foreach語句計(jì)算輸入的字符串的長度,并顯示長度。class Program static void Main(string args) Console.WriteLine(Please enter a string:); string a = Console.Re

13、adLine(); int i = 0; char ca = a.ToCharArray(); foreach (char x in ca) i+; Console.WriteLine(0 length is:1, a, i); Console.ReadKey(); 18.計(jì)算:1/2-2/3+3/4-4/5前100項(xiàng)。double sum = 0; double k = -1; for (double i = 1; i 101;i+ ) if (i % 2 = 0) sum = sum + (i / (i + 1) * k; else sum = sum + i / (i + 1); Con

14、sole.WriteLine(resilt is :+sum); Console.ReadLine();19.斐氏數(shù)列是公元13世紀(jì)數(shù)學(xué)家斐波拉契發(fā)明的。即:1,2,3,5,8,13,21,34,55,89,輸出其前15項(xiàng)。class Program static void Main(string args) Console.WriteLine(Please waite.); int but = new int15; for (int i = 0; i but.Length;i+ ) if (i = 0 | i = 1) but0 = 1; but1 = 2; else buti = buti

15、 - 1 + buti - 2; for (int j = 0; j but.Length;j+ ) Console.Write(butj+ ); Console.ReadKey(); 二、方法題(注意這些方法要在主函數(shù)里面調(diào)用測試是否正確)1.寫一個方法,判斷一個字符串str1,是否是回文,例如字符串str1,判斷其是否為回文,是回文返回true,否則返回false?;匚氖侵疙樧x和倒讀都一樣的字符串。 例如字符串LEVEL是回文,而字符串12312就不是回文。class Program static void Main(string args) Console.WriteLine(Pleas

16、e waite.); string data = Console.ReadLine(); if (check(data) Console.WriteLine(true); else Console.WriteLine(false); Console.ReadKey(); public static bool check(string data) bool result = false; char data2 = data.ToCharArray(); Array.Reverse(data2); string data3 = new string(data2); if (data.Equals(

17、data3) result = true; else result = false; return result; 2.寫一個方法,統(tǒng)計(jì)一個字符串中單詞的個數(shù),返回值為單詞個數(shù)。規(guī)定所有單詞由小寫字母組成,單詞之間由若干個空格隔開。例如若輸入字符串 it is a book ,得到結(jié)果為 4。 class Program static void Main(string args) Console.WriteLine(-+inint(); Console.ReadKey(); public static int inint() int n = 0; int i = 0; Console.Writ

18、eLine(please input some words.); string str = it is a book ; for (i = 0; i str.Length;i+ ) if(stri!= &stri+1= ) n+; return n; 6.寫一個方法,將一個字符串中所有數(shù)字字符前加一個$字符,并返回處理后的字符串。例如輸入:A1B23CD45,則方法返回值為:A$1B$2$3CD$4$5 先將字符串轉(zhuǎn)化為char,申明一個arraylist ,遍歷char數(shù)組中的數(shù)據(jù),如果為數(shù)字,Arraylist. Add($+listi); 否則array.Add(listi); 再申明一

19、個字符串?dāng)?shù)組str,接收arraylisti.tostring()最后把字符串?dāng)?shù)組轉(zhuǎn)化為字符串 string res = string.jion(“ ”,str);public static string inint() string data; char temp = $; Console.WriteLine(please input some words.); data = Console.ReadLine(); char but = data.ToCharArray(); for (int i = 0; i = 0 & buti = 9) Console.Write($0, buti);

20、 else Console.Write(buti); public static string change(string data) char list = data.ToCharArray(); ArrayList array = new ArrayList(); for (int i = 0; i list.Length;i+ ) if (listi = 0) array.Add($+listi); else array.Add(listi); string res = new stringarray.Count; for (int i = 0; i array.Count;i+ ) r

21、esi = arrayi.ToString(); string kk = string.Join(,res); return kk; 7.寫一個方法,將字符中所有的小寫字母轉(zhuǎn)換成大寫字母,大寫字母轉(zhuǎn)換成小寫字母,其余字符不變。方法返回轉(zhuǎn)變后的字符串。str=AbC 轉(zhuǎn)變?yōu)榇疄?aBcchai = (char)(chai - 32); public static void inint() string data; Console.WriteLine(please input some words.); data = Console.ReadLine(); char but = data.ToCh

22、arArray(); for (int i = 0; i = a & buti m) min= m; for(int i=1;im) max = n; for (int i = max; ;i+ ) if(i%n=0&i%m=0) min = i; break; return min; 11.寫一個方法,求s=a+aa+aaa+aaaa+aa.a的值,其中a是用戶定義的數(shù)字。例如2+22+222+2222+22222(此時共有5個數(shù)相加),返回值為和s。public static int change(int m) int res=0; int temp = 0; for (int i = 1

23、; i = 5;i+ ) temp = temp * 10 + m; res+=temp; return res; 12.寫一個方法,判斷一個數(shù)是否是完數(shù),返回值為boolean類型。一個數(shù)如果恰好等于它的因子之和,這個數(shù)就稱為“完數(shù)”。例如6=123。3.寫一個方法,求分?jǐn)?shù)序列:2/1,3/2,5/3,8/5,13/8,21/13.的前20項(xiàng)之和,并返回。14.寫一個方法,求1+2!+3!+.+n!的和,并將和作為返回值返回注意定義temp的位置,放在第一個for循環(huán)之后,每次都要重新定義為1 public static int change(int m) int sum=1; for (i

24、nt i = 2; i = m;i+ ) int temp = 1; for (int j = 1; j i) result = t; i = t.Length; return result; 18.寫一個方法,對于給定一個日期,返回該日為星期幾。例如2002-3-28返回星期四。class Program static void Main(string args) inint(); Console.ReadKey(); public static void inint() Console.WriteLine(please input a day .); string datetime = C

25、onsole.ReadLine(); Console.WriteLine(-); Console.WriteLine( datetime+ is + (DateTime.Parse(datetime) .DayOfWeek); 19.寫一個方法,隨機(jī)產(chǎn)生20個10,50的正整數(shù)存放到數(shù)組中,并輸出數(shù)組中的所有元素最大值、最小值、平均值及各元素之和。class Program static float one, two, avg, sum; static int but = new int20; static Random radom = new Random(); static int tem

26、p; static void Main(string args) Console.WriteLine(Please waite.); inint(); Console.ReadKey(); public static void inint() for (int i = 0; i but.Length;i+ ) buti=radom.Next(10,50); Console.WriteLine(*); for (int i = 0; i but.Length; i+) Console.Write(buti+ ); Console.WriteLine(n); Console.WriteLine(p

27、aixu.); for (int i = 0; i but.Length; i+) for (int j = 0; j i;j+ ) if(butibutj) temp = buti; buti = butj; butj = temp; Console.WriteLine(-); for (int i = 0; i but.Length; i+) Console.Write(buti + ); 20.已知一個數(shù)列的前三項(xiàng)分別為0,0,1,以后的各項(xiàng)都是其相鄰的前三項(xiàng)之和,寫一個方法,求計(jì)算并返回該數(shù)列前n項(xiàng)的平方根之和sum。例如,當(dāng)n10時,方法的返回結(jié)果應(yīng)為:23.197745。21.編寫

28、一個方法,判斷一個數(shù)是否能被3整除但不能被5整除,編寫控制臺應(yīng)用程序,輸出1-1000以內(nèi)的所有能被3整除但不能被5整除的數(shù)。22.編寫一個方法,計(jì)算:1-2+3-4+5.N,要求編寫控制臺應(yīng)用程序,來檢驗(yàn)方法的正確性。23.編寫一個方法,判斷一個三位數(shù)是否等于其每位數(shù)字的立方和,例如153=13+53+33,要求編寫控制臺應(yīng)用程序,來檢驗(yàn)方法的正確性。24.編寫一個方法,判斷一個數(shù)是否既能被3整除又能被7整除,要求編寫控制臺應(yīng)用程序,來檢驗(yàn)方法的正確性。三、類設(shè)計(jì)1.設(shè)計(jì)雇員類(Employee)及其子類經(jīng)理類(Manager),雇員類包含私有成員字段name,salary;并包含其屬性Name,Salary;經(jīng)理類還有自己的私有成員字段bonus,及其對應(yīng)屬性Bonus;雇員類、經(jīng)理類都要有自己的無參、有參構(gòu)造方法; 在main中創(chuàng)建一個經(jīng)理對象并設(shè)置其獎金金額,另建立員工數(shù)組(經(jīng)理作為其一個元素),要求打印輸出該員工數(shù)組的姓名和薪水信息。 static void Main(string args) Manager manager = new Manager(); manager.Name = Manager; manager.Salary = 5000; m

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論