C#.NET第一二单元C#课后习题
最大公约数与最小公倍数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 最大公约数与最小公倍数
{
class Program
{
static void Main(string[] args)
{
int m, n;
string s1, s2;
s1=Console.ReadLine();
s2=Console.ReadLine();
m = int.Parse(s1);
n = int.Parse(s2);
int a;
if(m>n)
{
a = m;
m = n;
n = a;
}
for(int tmp= m;tmp>=1;tmp--)
{
if(m%tmp==0&&n%tmp==0)
{
Console.WriteLine("最大公约数是{0}", tmp);
break;
}
}
long p = m * n;
for (long sum = m * n; sum >= n; sum--)
{
if (sum % m == 0 && sum % n == 0)
{
p = sum;
}
}
Console.WriteLine("最小公倍数是{0}",p);
Console.ReadLine();
}
}
}
通过单击按钮在标签上显示中国龙
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "中国龙";
}
四舍五入
namespace 四舍五入
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string s = textBox2.Text;
double a = Convert.ToDouble(s);
int b=((int)(a + 0.5));
textBox1.Text = b.ToString();
}
}
判断是否为闰年
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string s;
s = Console.ReadLine();
int n = int.Parse(s);
bool a = false;
if ((n % 4 == 0 && n % 100 == 0 && n % 400 == 0) || n % 4 == 0 && n % 100 != 0)
{
a = true;
}
else
a = false;
Console.Write(a);
Console.ReadLine();
}
}
}
namespace Console_一元二次方程的根
{
class Program
{
static void Main(string[] args)
{
string s1, s2, s3;
double a, b, c,n;
s1=Console.ReadLine();
s2 = Console.ReadLine();
s3 = Console.ReadLine();
a = double.Parse(s1);
b = double.Parse(s2);
c = double.Parse(s3);
n =( b * b )- (4 * a * c);
if (n < 0)
{
Console.WriteLine("实数范围内内无解");
Console.ReadLine();
}
else if (n == 0)
{
Console.WriteLine("有两个相同的解,值为" + (-b / 2 * a));
Console.ReadLine();
}
else
{
double p = (-b + Math.Sqrt(n)) /( 2 * a);
double q = (-b - Math.Sqrt(n)) / (2 * a);
Console.WriteLine("有两个不同的解,它们是:x1={0},x2={1}", p, q);
Console.ReadLine();
}
}
}
}
namespace Console输出较大的数
{
class Program
{
static void Main(string[] args)
{
double a = Convert.ToDouble(Console.ReadLine());
double b = Convert.ToDouble(Console.ReadLine());
double tmp= (a > b)? a:b;
Console.WriteLine(tmp);
Console.ReadLine();
}
}
}
namespace Console分段函数3._1
{
class Program
{
static void Main(string[] args)
{
double x = Convert.ToDouble(Console.ReadLine());
int y;
if (x > 0)
{
y = 1;
}
else if (x == 0)
{
y = 0;
}
else
y = -1;
Console.WriteLine("f({0})={1}", x, y);
Console.ReadLine();
}
}
}
namespace Console斐波那契
{
class Program
{
static void Main(string[] args)
{
int []a=new int[11];
a[1]=1;
a[2] = 1;
int i = 3;
Console.WriteLine(a[1]);
Console.WriteLine(a[2]);
while(i<=10)
{
a[i] = a[i - 1] + a[i-2];
Console.WriteLine(a[i]);
i++;
}
Console.ReadLine();
}
}
}
namespace Console成绩从百分制化为等级制
{
class Program
{
static void Main(string[] args)
{
double score = Convert.ToDouble(Console.ReadLine());
if(score>100||score<0)
{
Console.Write("输入不合法");
return;
}
if(score>=90)
{
Console.WriteLine("优秀");
Console.ReadLine();
}
else if (score >= 80)
{
Console.WriteLine("良好");
Console.ReadLine();
}
else if (score >= 70)
{
Console.WriteLine("中等");
Console.ReadLine();
}
else if (score >= 60)
{
Console.WriteLine("及格");
Console.ReadLine();
}
else
{
Console.WriteLine("不及格");
Console.ReadLine();
}
}
}
}