做一个控制台程序,要求输入三个任意整数,将三个数按从大到小的顺序输出。
根据排列组合,知道有6中情况,一是可以采用排序,不过有些小题大做。二是可以交换,输出。
方法一
采用交换,让a最大,b第二大,就行了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" put into 3 numbers");
int a, b, c;
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
c = int.Parse(Console.ReadLine());
display(a, b, c);
}
static void display(int a, int b, int c)
{
//保证a最大
if(a<b)
swap(ref a, ref b);
if(a<c)
swap(ref a, ref c);
//保证b第二大
if(b<c)
swap(ref b, ref c);
Console.WriteLine("{0},{1},{2}", a, b, c);
}
// 交换函数
static void swap(ref int a,ref int b)
{
int temp = a;
a = b;
b = temp;
}
}
}
put into 3 numbers
55
88
111
111,88,55
请按任意键继续. . .
采用冒泡排序,搞定
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homework1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" put into 3 numbers");
int a,b,c;
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
c = int.Parse(Console.ReadLine());
Console.WriteLine("the array:");
int[] num = { a, b, c };
display(num);
sort(num);
Console.WriteLine(" sort the end");
display(num);
}
static void sort(int[] str)
{
for(int i=0; i<str.Length-1; i++)
for (int j = 0; j < str.Length - i - 1; j++)
{
if (str[j] < str[j + 1])
{
int temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
static void display(int[] str)
{
foreach (int n in str)
Console.WriteLine(n);
}
}
}
put into 3 numbers
11
22
66
the array:
11
22
66
sort the end
66
22
11
请按任意键继续. . .
定义一个student的结构体,有姓名,分数属性,输入3组数据,在输出,并求出平均分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homework3
{
class Program
{
struct Student
{
public string name; //姓名
public float math; //数学成绩
}
static void Main(string[] args)
{
Student[] s = new Student[4];
s= input(s);
display(s);
// double ave = s[0].math + s[1].math + s[2].math;
float ave = s[s.Length - 1].math;
Console.WriteLine("the average mark is :" + ave/3);
Console.WriteLine();
}
static Student[] input(Student[] s)
{
float sum = 0;
for(int i=0; i<s.Length-1; i++)
{
Console.WriteLine("put "+(i+1)+" student name and mark:");
s[i].name = Console.ReadLine();
s[i].math = (float)Convert.ToDouble(Console.ReadLine());
sum += s[i].math;
}
s[s.Length - 1].math = sum;
return s;
}
static void display(Student[] s)
{
for(int i=0; i<s.Length; i++)
{
Console.WriteLine("{0}的数学成绩是:{1}",s[i].name,s[i].math);
}
}
}
}
结果是:
put 1 student name and mark:
a
85
put 2 student name and mark:
b
90.5
put 3 student name and mark:
c
76.5
a的数学成绩是:85
b的数学成绩是:90.5
c的数学成绩是:76.5
的数学成绩是:252
the average mark is :84
网友评论