1.题目: 输出hello world
題目內容
参考以前的案例,编写程序输出以下内容:
******************** Hello World!********************
******************** Hello World!********************
输入输出说明:
输出:
********************
Hello World!
********************
输出:
********************
Hello World!
********************
参考答案:
using System;
namespace Main
{
class Program {
static void Main(String[] args) {
Console.WriteLine("***********************");
Console.WriteLine(" Hello World!");
Console.WriteLine("***********************");
}
}
}
2.题目: 数据加密
題目內容
输入1个四位数,将其加密后输出。
方法是将该数每一位上的数字加9,
然后除以10取余,做为该位上的新数字,
最后将第1位和第3位上的数字互换,第2位和第4位互换。
说明:输入输出的是4位数,首位可以为0。
输入输出说明:
方法是将该数每一位上的数字加9,
然后除以10取余,做为该位上的新数字,
最后将第1位和第3位上的数字互换,第2位和第4位互换。
说明:输入输出的是4位数,首位可以为0。
输入输出说明:
输入:
1257
1257
输出:
4601
4601
参考答案:
using System;
namespace Main
{
class Program {
static void Main(String[] args) {
int number, digit1, digit2, digit3, digit4;
int temp;
number = int.Parse(Console.ReadLine());
digit1 = number / 1000;
digit2 = (number % 1000) / 100;
digit3 = ((number % 1000) % 100) / 10;
digit4 = number % 10;
digit1 = (digit1 + 9) % 10;
digit2 = (digit2 + 9) % 10;
digit3 = (digit3 + 9) % 10;
digit4 = (digit4 + 9) % 10;
temp = digit1;
digit1 = digit3;
digit3 = temp;
temp = digit2;
digit2 = digit4;
digit4 = temp;
Console.WriteLine("{0}{1}{2}{3} ",digit1,digit2,digit3,digit4);
}
}
}
3.题目:整数相除求余数
題目內容
输入两个整数,返回这两个数相除的余数,要求第一个数除以第二个数。不考虑出数为0。
输入输出说明:
输入:
7
3
输出:
1
输入输出说明:
输入:
7
3
输出:
1
参考答案;
using System;
namespace Main
{
class Program {
static void Main(String[] args) {
int num1 = int.Parse(Console.ReadLine());
int num2 = int.Parse(Console.ReadLine());
Console.WriteLine(num1 % num2);
}
}
}
4.题目:判断是否为闰年
題目內容
输入一个正整数,表示某一年,判断是否为闰年。
输入输出说明:
输入:
2013
输出:
2013 is not a leap year.
输入:
2000
输出:
2000 is a leap year.
输入输出说明:
输入:
2013
输出:
2013 is not a leap year.
输入:
2000
输出:
2000 is a leap year.
参考答案:
using System;
namespace Main
{
class Program {
static void Main(String[] args) {
int year = int.Parse(Console.ReadLine());
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
Console.WriteLine("{0} is a leap year.", year);
else
Console.WriteLine("{0} is not a leap year.", year);
}
}
}
5.打印水仙花数
題目內容
打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
输入输出说明:
输入:无
输出:153 370 371 407
参考答案:
using System;
namespace Main
{
class Program {
static void Main(String[] args) {
int b1, b2, b3;
for (int m = 101; m < 1000; ++m) {
b3 = m / 100;
b2 = m % 100 / 10;
b1 = m % 10;
if ((b3 * b3 * b3 + b2 * b2 * b2 + b1 * b1 * b1) == m)
Console.Write(m + " ");
}
}
}
}
6.判断n是否为素数
題目內容
输入正整数n,判断n是否为素数,输出1代表是,0代表不是。
素数是指只可以被1和其本身整除的数字。
输入输出说明:
输入:
3
输出:
1
输入:
6
输出:
0
素数是指只可以被1和其本身整除的数字。
输入输出说明:
输入:
3
输出:
1
输入:
6
输出:
0
参考答案:
using System;
namespace Main
{
class Program {
static void Main(String[] args) {
int n, i, flag = 1;
n = int.Parse(Console.ReadLine());
for (i = 2; i <= n / 2; i++) {
if ((n % i) == 0) {
flag = 0;
}
else {
flag = 1;
}
}
Console.WriteLine(flag);
}
}
}
7.题目:Dog类
題目內容
定义一个Dog 类,包含age、weight等属性,以及对这些属性进行操作的方法GetAge(),SetAge (),GetWeight(),SetWeight () (分别用来设置和输出dog的年龄和体重)。
要求初始化一个名为jack的Dog类对象,设置jack的初始age为2,初始weight为10并输出。再设置jack的age为8,weight为30并输出。
输入输出说明:
输入:
无
输出:
2 10
8 30
要求初始化一个名为jack的Dog类对象,设置jack的初始age为2,初始weight为10并输出。再设置jack的age为8,weight为30并输出。
输入输出说明:
输入:
无
输出:
2 10
8 30
参考答案:
using System;
namespace Main
{
class Dog
{
private int age;
private int weight;
public Dog(int initialAge, int initialWeight)
{
this.age = initialAge;
this.weight = initialWeight;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public int getWeight()
{
return weight;
}
public void setWeight(int weight)
{
this.weight = weight;
}
}
class Program
{
static void Main(String[] args)
{
Dog Jack =new Dog(2,10);
Console.WriteLine(Jack.getAge()+" "+Jack.getWeight());
Jack.setAge(8);
Jack.setWeight(30);
Console.WriteLine(Jack.getAge()+" "+Jack.getWeight());
}
}
8.题目:circle类
題目內容
定义一个circle类,类的定义及具体成员方法见代码.现要求在Main方法中从键盘中输入圆的半径,然后输出圆的周长和面积。
输入输出说明:
输入:
3
输出:
18.84 28.26
输入输出说明:
输入:
3
输出:
18.84 28.26
参考答案:
using System;
namespace Main
{
class Circle
{
private const float PI = (float)3.14;
private float radius;
public Circle(float r)
{
this.radius = r;
}
//返回圆的周长
public float Circumference()
{
return 2 * PI * radius;
}
//返回圆的面积
public float Area()
{
return PI * radius * radius;
}
}
class Program
{
static void Main(String[] args)
{
float radius = float.Parse(Console.ReadLine());
Circle circle = new Circle(radius);
Console.WriteLine(circle.Circumference() + " " + circle.Area());
}
}
}
9.题目:冒泡排序
題目內容
输入一个整型数组,用冒泡法将其排序。先输入要输入排序数据的个数n,再输入数据
输入输出说明:
输入:
5
5 3 4 1 2
输出:
1 2 3 4 5
输入输出说明:
输入:
5
5 3 4 1 2
输出:
1 2 3 4 5
参考答案:
using System;
namespace Main
{
class Program
{
static void Main(String[] args)
{
int iNum = int.Parse(Console.ReadLine());
string[] inputs = Console.ReadLine().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
int[] Arr = new int[iNum];
for(int i = 0 ; i < Arr.Length ; ++i)
{
Arr[i] = int.Parse(inputs[i]);
}
BubbleSort(Arr);
print_arr(Arr);
}
static void BubbleSort(int[] array)
{
int temp = 0;
for (int j = 0; j < array.Length; ++j)
for (int k = 0; k < array.Length - j - 1; ++k)
{
if (array[k] > array[k + 1])
{
temp = array[k];
array[k] = array[k + 1];
array[k + 1] = temp;
}
}
}//BubbleSort
static void print_arr(int[] arr)
{
for(int i = 0 ; i < arr.Length ; ++i)
Console.Write(arr[i]+" ");
}
}
}
10.题目:输出斐波拉契数列
題目內容
输入一个正整数n(3<=n<=20),将斐波那契数列的前n项保存到一维数组a[20]中,并输出。
斐波那契数列:
a(0) = 0;
a(1) = 1;
a(n) = a(n-1) + a(n-2) n>=3
输入输出说明:
输入:
10
输出:
0 1 1 2 3 5 8 13 21 34
斐波那契数列:
a(0) = 0;
a(1) = 1;
a(n) = a(n-1) + a(n-2) n>=3
输入输出说明:
输入:
10
输出:
0 1 1 2 3 5 8 13 21 34
参考答案:
using System;
namespace Main
{
class Program
{
static void Main(String[] args)
{
int n = int.Parse(Console.ReadLine());
int[] a = new int[n];
a[0] = 0;
a[1] = 1;
int i;
for (i = 2; i < n; i++)
{
a[i] = a[i - 1] + a[i - 2];
}
// 输出结果
for (i = 0; i < n; i++)
{
Console.Write(a[i] + " ");
}
}
}
}
11.题目:简单选择排序
題目內容
给定一个长度为7的无序一维整型数组,用简单选择排序法对其进行升序排序。
简单选择排序思想:
每趟排序选择待排序列中的最小值,将它与已排序列后一位的元素交换。
示例:3 4 6 8 5 10 2
各趟排序过程如下:
2 4 6 8 5 10 3(最小数2放到第一位,已排序数组为2)
2 3 6 8 5 10 4(未排序数组中的最小值3放到第二位,已排序数组为2 3)
2 3 4 8 5 10 6(未排序数组中的最小值4放到第三位,已排序数组为2 3 4)
2 3 4 5 8 10 6
2 3 4 5 6 10 8
2 3 4 5 6 8 10
输入输出说明:
输入:
3 4 6 8 5 10 2
输出:
2 3 4 5 6 8 10
简单选择排序思想:
每趟排序选择待排序列中的最小值,将它与已排序列后一位的元素交换。
示例:3 4 6 8 5 10 2
各趟排序过程如下:
2 4 6 8 5 10 3(最小数2放到第一位,已排序数组为2)
2 3 6 8 5 10 4(未排序数组中的最小值3放到第二位,已排序数组为2 3)
2 3 4 8 5 10 6(未排序数组中的最小值4放到第三位,已排序数组为2 3 4)
2 3 4 5 8 10 6
2 3 4 5 6 10 8
2 3 4 5 6 8 10
输入输出说明:
输入:
3 4 6 8 5 10 2
输出:
2 3 4 5 6 8 10
参考答案:
using System;
namespace Main
{
class Program
{
static void Main(String[] args)
{
string[] inputs = Console.ReadLine().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
int[] a = new int[inputs.Length];
for (int i = 0; i < a.Length; ++i)
{
a[i] = int.Parse(inputs[i]);
}
int pos = 0;//已有几个数字排好顺序了
int temp;//交换数字的中间变量
for (int i = 0; i < a.Length; i++)
{
for (int j = pos; j < a.Length - 1; j++)
{
if (a[pos] > a[j + 1])
{
temp = a[pos];
a[pos] = a[j + 1];
a[j + 1] = temp;
}
}
pos++;
}
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] + " ");
}
}
}
}
12.题目:抽象类:计算矩形和圆的周长面积
題目內容
已知一个抽象类Shape,该类有两个抽象方法,分用来计算面积和周长。在此基础上派生出Rectangle和Circle类,分别实现GetArea方法和GetPerimeter方法,用来计算矩形和圆的面积以及周长。
输入输出说明:
输出:
20
18
113.09733552923255
37.69911184307752
输入输出说明:
输出:
20
18
113.09733552923255
37.69911184307752
参考答案:
using System;
namespace Main
{
abstract class Shape
{
public abstract double GetArea();
public abstract double GetPerimeter();
}
class Rectangle: Shape
{
int width;
int height;
public Rectangle(int w,int h)
{
width = w;
height = h;
}
public override double GetArea()
{
return width*height;
}
public override double GetPerimeter()
{
return 2*(width+height);
}
}
class Circle: Shape
{
int r;
public Circle(int r)
{
this.r = r;
}
public override double GetArea()
{
return Math.PI*r*r;
}
public override double GetPerimeter()
{
return 2*Math.PI*r;
}
}
class Program
{
static void Main(String[] args)
{
Rectangle a = new Rectangle(5,4);
Console.WriteLine(a.GetArea());
Console.WriteLine(a.GetPerimeter());
Circle c = new Circle(6);
Console.WriteLine(c.GetArea());
Console.WriteLine(c.GetPerimeter());
}
}
}
13.题目:接口的实现类
題目內容
有如下类:
interface Paper {
interface Paper {
public String GetName();}
class Printer {
public void Print(Paper p) {
Console.WriteLine(p.GetName());
}
}
要求创建 A4Paper,A6Paper两个类,实现Paper接口,并实现GetName方法
A4Paper类的GetName返回字符串 "A4"
A6Paper类的GetName返回字符串 "A6"
在main方法中创建Printer对象,并调用Print()方法两次,分别传入A4,A6类的对象
输入输出说明:
输入:无
输出:
A4
A6
A4Paper类的GetName返回字符串 "A4"
A6Paper类的GetName返回字符串 "A6"
在main方法中创建Printer对象,并调用Print()方法两次,分别传入A4,A6类的对象
输入输出说明:
输入:无
输出:
A4
A6
参考答案:
using System;
namespace Main
{
interface Paper
{
String GetName();
}
class A4Paper: Paper
{
public String GetName()
{
return "A4";
}
}
class A6Paper: Paper
{
public String GetName()
{
return "A6";
}
}
class Printer
{
public void Print(Paper p)
{
Console.WriteLine(p.GetName());
}
}
class Program
{
static void Main(String[] args)
{
Printer pri = new Printer();
Paper a4 = new A4Paper();
Paper a6 = new A6Paper();
pri.Print(a4);
pri.Print(a6);
}
}
}
14.题目:判断是否为数字
題目內容
输入一个字符串,判断是否为数字,若是则输出True;否则输出False。
输入输出说明:
输入:
-2.3
输出:
True
using System;
namespace Main
{
class Program
{
static void Main(String[] args)
{
string input = Console.ReadLine();
try
{
double.Parse(input);
Console.WriteLine(true);
}
catch
{
Console.WriteLine(false);
}
}
}
}
15.题目Dog类
題目內容
现定义一个类体系,基类为Dog,派生类为斑点狗SpottedDog类和非斑点狗UnspottedDog类,在基类中记录狗的品种breed,体重weight以及颜色color,斑点狗类中还有斑点颜色spotColor。基类中有showBreed方法显示狗的品种,斑点狗派生类中有spotInfo方法显示斑点颜色。分别定义并实现上述类,并通过主程序中的测试。
(说明:每个输出后请加一个回车)
输入输出说明:
输出:
Dalmatian
red
Labrador Retriever
(说明:每个输出后请加一个回车)
输入输出说明:
输出:
Dalmatian
red
Labrador Retriever
参考答案:
using System;
namespace Main
{
class Dog
{
protected String breed;
protected int weight;
protected String color;
public Dog()
{
}
public Dog(String breed, int weight, String color)
{
this.breed = breed;
this.weight = weight;
this.color = color;
}
public virtual void showBreed()
{
Console.WriteLine(breed);
}
}
class UnspottedDog: Dog
{
public UnspottedDog(String name, int weight, String color):base(name, weight, color)
{
}
public override void showBreed()
{
Console.WriteLine(breed);
}
}
class SpottedDog: Dog
{
private String spotColor;
public SpottedDog(String name, int weight, String color, String spotColor)
: base(name, weight, color)
{
this.spotColor = spotColor;
}
public void spotInfo()
{
Console.WriteLine(spotColor);
}
}
class Program
{
static void Main(String[] args)
{
SpottedDog redSpot = new SpottedDog("Dalmatian", 60, "white", "red");
UnspottedDog rover = new UnspottedDog("Labrador Retriever", 40, "yellow");
redSpot.showBreed();
redSpot.spotInfo();
rover.showBreed();
}
}
}