zoukankan      html  css  js  c++  java
  • c#认证考试试题。

    1.1、输入一个正整数n,如果是偶数则求n以内的所有偶数和,奇数则求n以内的所有奇数的和。

    static void Main(string[] args)
    {
    int result=0;
    Console.WriteLine("请输入一个正整数:");
    int n = Convert.ToInt32(Console.ReadLine());
    if(n%2==0)
    {
    for (int i = 0; i <= n; i++)
    if (i % 2 == 0)
    result += i;
    }
    else
    {
    for (int i = 0; i <= n; i++)
    if (i % 2 != 0)
    result += i;
    }
    Console.WriteLine("result为:{0}",result);
    Console.ReadLine();

    }

    1.2、输入一个正整数,输出所有约数。

    static void Main(string[] args)
    {
    Console.WriteLine("请输入一个正整数:");
    int n = Convert.ToInt32(Console.ReadLine());
    if (n % 2 == 0)
    {
    for (int i = 2; i <= n / 2; i++)
    {
    if (n % i == 0)
    {
    Console.WriteLine(i);
    }
    }
    }
    else
    {
    for(int i=3;i<=(n-1)/2;i++)
    {
    if(n%i==0)
    {
    Console.WriteLine(i);
    }
    }
    }
    Console.ReadLine();
    }

    1.3-1.4、输入两个正整数,求最大公约数,最小公倍数.

    static void Main(string[] args)
    {
    int result=1;
    Console.WriteLine("请输入第一个正整数:");
    int a = Convert.ToInt32(Console.ReadLine());
    int x = a;
    Console.WriteLine("请输入第二个正整数:");
    int b = Convert.ToInt32(Console.ReadLine());
    int y = b;
    if (a > b)
    {
    int t = a;
    a = b;
    b = t;

    }
    while(true)
    {

    if (b % a == 0)
    {
    result = a;
    break;
    }
    else
    {
    int ys = b % a;
    b = a;
    a = ys;
    }

    }
    int result1 = x * y / result;
    Console.WriteLine("最大公约数为:{0}",result);
    Console.WriteLine("最小公倍数为:{0}", result1);
    Console.ReadLine();
    }

    1.5、判断素数。

    static void Main(string[] args)
    {
    bool prime=true;
    Console.WriteLine("请输入一个正整数:");
    int n = Convert.ToInt32(Console.ReadLine());
    for(int i=2;i<=n/2;i++)
    {
    if (n % i == 0)
    prime = false;
    break;
    }
    if (prime)
    Console.WriteLine("{0}是素数", n);
    else
    Console.WriteLine("{0}不是素数", n);
    }

    1.6、

    1.7、验证15位身份证并判断身份证主人性别

    static void Main(string[] args)
    {
    Console.WriteLine("请输入15位身份证号码:");
    string id = Convert.ToString(Console.ReadLine());
    char ch;

    if(id.Length<15||id.Length>15)
    {
    Console.WriteLine("号码格式不正确!");

    }
    else
    {

    for(int i=0;i<id.Length;i++)
    {
    ch = id[i];
    if (!(ch>='0'&&ch<='9'))
    {
    Console.WriteLine("号码格式不正确!");
    break;
    }


    }
    if (id[id.Length-1] % 2 == 0)
    Console.WriteLine("性别为女!");
    else
    Console.WriteLine("性别为男!");
    }

    Console.ReadLine();
    }

    1.9、求字符串中ASCII最大的字符

    static void Main(string[] args)

            {

                Console.WriteLine("请输入一个字符串:");

                string s_text = Convert.ToString(Console.ReadLine());

                char ch ;

                ch = s_text[0];

                for (int i = 0; i < s_text.Length; i++)

                {

                    if (ch < s_text[i])

                        ch = s_text[i];

                }

                Console.WriteLine("ASCII最大的字符是{0}",ch);

                Console.ReadLine();

            }

    1.10、求字符串中ASCII最小的字符

    static void Main(string[] args)

            {

                Console.WriteLine("请输入一个字符串:");

                string s_text = Convert.ToString(Console.ReadLine());

                char ch;

                ch = s_text[0];

                for (int i = 0; i < s_text.Length; i++)

                {

                    if (ch > s_text[i])

                        ch = s_text[i];

                }

                Console.WriteLine("ASCII最小的字符是{0}", ch);

                Console.ReadLine();

            }

    1.11、输入原字符串和密钥字符串,输出对字符串进行加密的结果。

    static void Main(string[] args)
    {
    string s_text, s_key;
    string s_result = null;
    char ch;
    Console.WriteLine("请输入原字符串:");
    s_text =Convert.ToString( Console.ReadLine());
    Console.WriteLine("请输入密钥字符串:");
    s_key = Convert.ToString(Console.ReadLine());
    if (s_text.Length != s_key.Length)
    {
    Console.WriteLine("密钥字符串与原字符串长度必须相等!");
    }
    else
    {
    for(int i=0;i<s_text.Length;i++)
    {
    ch = s_text[i];
    s_result += ch ^ s_key[i];

    }
    Console.Write("加密后的字符串为:");

    }
    Console.WriteLine(s_result);
    Console.ReadLine();
    }

    2.1继承与派生

    public class Animal
    {
    private bool m_sex;
    private int m_age;
    public bool Sex
    {
    get { return m_sex; }
    set { m_sex = value; }
    }

    public int Age
    {
    get { return m_age; }
    set { m_age = value; }
    }
    public Animal()
    {
    Sex = false;
    }
    public virtual string Introduce()
    {
    if (Sex)
    return "This is a male Animal!";
    else
    return "This is a female Animal!";
    }

    }
    public class Dog:Animal
    {
    public Dog()
    {
    Sex = true;
    }
    public override string Introduce()
    {
    if (Sex)
    return "This is a male Dog!";
    else
    return "This is a female Dog!";
    }
    }
    public class Cat:Animal
    {

    public override string Introduce()
    {
    if (Sex)
    return "This is a male Cat!";
    else
    return "This is a female Cat!";
    }
    }

    static void Main(string[] args)
    {
    Animal animal = new Animal();
    Console.WriteLine(animal.Introduce());
    Dog dog = new Dog();
    Console.WriteLine(dog.Introduce());
    Cat cat = new Cat();
    Console.WriteLine(cat.Introduce());
    }

    2.11

    public class Animal
    {
    private bool m_sex;
    private string m_sound;
    public bool Sex
    {
    get { return m_sex; }
    set { m_sex = value; }
    }
    public Animal()
    {
    Sex = false;
    Sound = "How1...";
    }
    public string Sound
    {
    get { return m_sound; }
    set { m_sound = value; }
    }
    public virtual string Roar()
    {
    return Sound;
    }
    }
    public class Dog:Animal
    {
    public Dog()
    {
    Sex = true;
    Sound = "Wow...";
    }
    public override string Roar()
    {
    return "Dog:"+Sound;
    }
    }
    public class Cat:Animal
    {
    public Cat()
    {
    Sound = "Miaow...";
    }
    public override string Roar()
    {
    return "Cat:"+Sound;
    }
    }
    public class Cow:Animal
    {
    public Cow()
    {
    Sound = "Moo...";
    }
    public override string Roar()
    {
    return "Cow:" + Sound;
    }
    }

    static void Main(string[] args)
    {
    Animal animal;
    Dog dog = new Dog();
    animal = dog;
    Console.WriteLine(animal.Roar());
    Cat cat = new Cat();
    animal = cat;
    Console.WriteLine(animal.Roar());
    Cow cow = new Cow();
    animal=cow;
    Console.WriteLine(animal.Roar());
    }

  • 相关阅读:
    重装Win10系统的非常简单的操作教程
    Python
    Delphi
    Libs
    Windows Server
    Windows Server
    Delphi
    Delphi
    Delphi
    Delphi
  • 原文地址:https://www.cnblogs.com/-sbz/p/7532185.html
Copyright © 2011-2022 走看看