******************** Hello World!********************
输出:
********************
Hello World!
********************
using System;
namespace Main
{
class Program {
static void Main(String[] args) {
Console.WriteLine("***********************");
Console.WriteLine(" Hello World!");
Console.WriteLine("***********************");
}
}
}方法是将该数每一位上的数字加9,
然后除以10取余,做为该位上的新数字,
最后将第1位和第3位上的数字互换,第2位和第4位互换。
说明:输入输出的是4位数,首位可以为0。
输入输出说明:
1257
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);
}
}
}输入输出说明:
输入:
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.
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 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是否为素数
素数是指只可以被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);
}
}
}要求初始化一个名为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());
}
}
输入输出说明:
输入:
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());
}
}
}输入输出说明:
输入:
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]+" ");
}
}
}斐波那契数列:
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] + " ");
}
}
}
}简单选择排序思想:
每趟排序选择待排序列中的最小值,将它与已排序列后一位的元素交换。
示例: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] + " ");
}
}
}
}输入输出说明:
输出:
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());
}
}
}interface Paper {
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);
}
}
}输入输出说明:
输入:
-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);
}
}
}
}(说明:每个输出后请加一个回车)
输入输出说明:
输出:
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();
}
}
}
1293



被折叠的 条评论
为什么被折叠?



