精典算法
一、請讓使用者輸入十個整數,最後印出輸入的最大值和最小值。
using System;
using System.Collections.Generic;
using System.Text;
namespace Max_Min
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入十個數");
int[] a = new int[10]; //設10個陣列//
int i = 0;
for (i = 0; i < 10; ++i)
a[i] = Convert.ToInt32(Console.ReadLine());
int max = a[0], min = a[0]; //最大與最小先設0//
for (i = 0; i < 10; ++i)
{
max = Math.Max(max, a[i]); //跟a[i]比最大值//
min = Math.Min(min, a[i]); //跟a[i]比最小值//
}
Console.WriteLine("最大值為{0}", max); //列出質//
Console.WriteLine("最小值為{0}", min); //列出質//
Console.Read();
}
}
}
二、宣告一個陣列,利用隨機數產生器Random.Next(),來設計一個程式由電腦產生出1~13的數字共13個,且數字不得重複。
EX:1、6、2、9、11、5、8、10、3、12、7、4、13。
using System;
using System.Collections.Generic;
using System.Text;
namespace Rnd13
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
//1~13取亂數 不重複
int n = 13;
int[] b = new int[13]; //設13個陣列//
while (n != 0)
{
int t = rnd.Next(13); //取亂數//
if (b[t] != -1) //b[t]不等於-1//
{
Console.Write("{0} ", t + 1); //把質列出//
b[t] = -1; //b[t]等於-1就不取//
--n;
}
}
}
}
}
三、找錢程式:
你現在有一千元大鈔,去買一個1~999元的東西(數值請自行輸入),金錢面額為1元、5元、10元、50元、100元、500元,所找的錢以最大面額為優先。
EX: 買901元的東西,找99元,相當於1枚50元、4枚10元、1枚5元、4枚1元。
using System;
using System.Collections.Generic;
using System.Text;
namespace Change
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請請輸入價錢 介於1~999");
int n = Convert.ToInt32(Console.ReadLine());
int nd = 1000 - n, i = 0;
Console.WriteLine("找回");
int[] a = new int[6] { 500, 100, 50, 10, 5, 1 };
for (i = 0; i < 6; ++i) //有6種錢種//
{
int t = 0;
while (nd >= a[i]) //總錢數>=a[i]//
{
nd = nd - a[i]; //總錢數減去找的錢錢//
++t;
}
if (t != 0 & a[i] >= 100)
Console.WriteLine("{0}張{1}元 ", t, a[i]); //列出張數//
else if (t !=0 & a[i] <= 50)
Console.WriteLine("{0}枚{1}元 ", t, a[i]); //列出枚數//
}
Console.Read();
}
}
}
四、最大公因數
讓使用者輸入兩個正整數,並求其最大公因數
(1) 請用iteration方式實作之
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
while (a != 0)
{
if (b > a) //a比b大//
{
int tmp = a; //交換//
a = b;
b = tmp;
}
a = a % b;
}
Console.WriteLine(b);
}
}
}
(2) 請用遞迴方式實作之
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
Console.Write(abc(a, b));
}
static int abc(int a, int b) //遞回//
{
if (b == 0)
return a;
else
return abc(b, a % b);
}
}
}
五、判斷大小寫
輸入一英文字串(字串內大小寫字母都有),將字串內大寫字母改成小寫字母,小寫字母改成大寫字母;顛倒其輸出次序。
例如:輸入AbcDeFg則輸出為GfEdCBa
using System;
using System.Collections.Generic;
using System.Text;
namespace High1
{
class Program
{
static void Main(string[] args)
{
string eng = Console.ReadLine ();
for (int i = eng.Length - 1 ;i >= 0 ;--i) //倒過來輸出//
{
if (char.IsUpper(eng[i])) //判斷大小寫//
Console.Write(char.ToLower(eng[i])); //轉換小寫輸出//
else
Console.Write(char.ToUpper(eng[i])); //轉換大寫輸出//
}
Console.Read();
}
}
}
六、阿婆賣蛋
阿婆賣蛋,七個一數餘二、十一個一數餘二、三個一數餘二,求小於10000之內所有可能的結果。
using System;
using System.Collections.Generic;
using System.Text;
namespace 阿婆賣蛋
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 10000; i++)
{
if (i % 231 == 0) //最大公因數//
{
i += 2; //餘數2//
Console.WriteLine("{0}", i); //列出//
}
}
Console.Read();
}
}
}
七、數字題
計算1-2+3-4+5-6....n的總和是多少。
using System;
using System.Collections.Generic;
using System.Text;
namespace 數字題
{
class Program
{
static void Main(string[] args)
{
int a, i;
int ans = 0;
Console.Write("enter a number");
a = Convert.ToInt32(Console.ReadLine());
for (i = 1; i <= a; i++)
{
if (i % 2 == 1)
{
ans += i; //如果是奇數就+//
}
else
{
ans -= i; //是偶數就-//
}
}
Console.Write("{0}", ans); //列出質//
Console.ReadLine();
}
}
}
八、銀行存款計算方式如下:
N天後的存款=開始存入的錢 *(1+RATE/365)^ N。其中 RATE代表年利率。試寫一程式包含下列兩個功能:
(1) 輸入開始的存款經過N天後,輸出存款為何?
using System;
using System.Collections.Generic;
using System.Text;
namespace 銀行存款計算方式
{
class program
{
static void Main(string[] args)
{
Console.Write("請輸入你想存的金額:");
double money = Double.Parse(Console.ReadLine());//double.parse和Convert.ToDouble是一樣的
Console.Write("你想存多少天:");
double n = Double.Parse(Console.ReadLine());
Console.Write("你希望的利率是:");
double rate = Double.Parse(Console.ReadLine());
double sum;
sum = money * Math.Pow((1 + rate / 365), n);
Console.WriteLine("你得到的金額為{0:f6}", sum);//{0:fx}x為取小數點之後多少位
Console.Read();
}
}
}
(2) 輸入N天後的存款,求開始時要存入多少錢?
using System;
using System.Collections.Generic;
using System.Text;
namespace 銀行存款計算方式
{
class program
{
static void Main(string[] args)
{
Console.Write("請輸入你想得到的金額:");
double money2 = Double.Parse(Console.ReadLine());
Console.Write("你想在幾天之內獲得:");
double n2 = Double.Parse(Console.ReadLine());
Console.Write("你希望的利率:");
double rate2 = Double.Parse(Console.ReadLine());
double sum2;
sum2 = money2 / Math.Pow((1 + rate2 / 365), n2);
Console.WriteLine("你需要存入的金額為{0:f6}", sum2);
Console.Read();
}
}
}

浙公网安备 33010602011771号