Csharp_pta2_2

发布于:2024-04-29 ⋅ 阅读:(23) ⋅ 点赞:(0)

7-7 C# 1.12 区间找数

编写控制台应用程序,根据用户输入的a、b、c、d值(均为正整数且a不大于b),输出在[a, b]区间中能被c整除,但是不能被d整除的数。

输入格式:

用户在一行中输入四个正整数,分别对应a、b、c、d,相邻的两个数中间用一个空格间隔。

输出格式:

输出在[a, b]区间中能被c整除,但是不能被d整除的数,每五个数换行(每行中的两个相邻数字之间用一个空格间隔)。

输入样例:

在这里给出一组输入。例如:

1 20 2 3

输出样例:

在这里给出相应的输出。例如:

2 4 8 10 14
16 20
class Solution7_7
{
    public static int a, b, c, d;
    public static string input;
    static void Main(string[] args)
    {
        List<int> list = new List<int>();
        string[] input = Console.ReadLine().Split(' ');
        a = Convert.ToInt32(input[0]);
        b = Convert.ToInt32(input[1]);
        c = Convert.ToInt32(input[2]);
        d = Convert.ToInt32(input[3]);
        for (int i = a; i <= b; i++)
        {
            if (i % c == 0 && i % d != 0)
            {
                list.Add(i);
            }
        }
        for (int i = 0; i < list.Count; i++)
        {
            Console.Write(list[i]);
            if ((i + 1) % 5 == 0)
            {
                Console.Write('\n');
            }
            else if (i != list.Count - 1)
            {
                Console.Write(' ');
            }
        }
    }
}

7-8 C# 1.13 成绩排序

编写控制台应用程序,用户输入n(n>0)位同学的学号(由3位数字构成)和成绩([0,100]),按照成绩的降序输出相应同学的学号。(每位同学的学号不同,如果多位同学成绩相同,则按学号升序输出)

输入格式:

用户输入n值。
逐行输入一位同学的学号和成绩,学号和成绩之间用一个空格间隔。共输入n行。

输出格式:

按成绩降序换行输出相应学生的学号。成绩相同的学生按学号升序在一行输出(每行中的两个相邻学号之间用一个空格间隔)。

输入样例:

在这里给出一组输入。例如:

4
988 70
782 80
777 80
451 67

输出样例:

在这里给出相应的输出。例如:

777 782
988
451
class Solution7_8
{
    static void Main(string[] args)
    {

        int n = Convert.ToInt32(Console.ReadLine());

        Dictionary<string, int> studentScores = new Dictionary<string, int>();

        for (int i = 0; i < n; i++)
        {
            string[] input = Console.ReadLine().Split(' ');
            string studentID = input[0];
            int score = Convert.ToInt32(input[1]);
            studentScores.Add(studentID, score);
        }

        // 使用List排序,根据成绩降序排序,成绩相同的学生按学号升序排列
        List<KeyValuePair<string, int>> sortedList = new List<KeyValuePair<string, int>>(studentScores);
        sortedList.Sort((x, y) =>
        {
            if (x.Value != y.Value)
                return y.Value.CompareTo(x.Value);
            else
                return x.Key.CompareTo(y.Key);
        });

        // 输出结果
        for(int i = 0; i < sortedList.Count;i++)
        {
            List<string> tmp = new List<string>();
            int j = i;
            while(j < sortedList.Count && sortedList[j].Value == sortedList[i].Value) {
                tmp.Add(sortedList[j].Key);
                j++;
            }
            for(int k = 0; k < tmp.Count; k++)
            {
                Console.Write(tmp[k]);
                if(k != tmp.Count - 1)
                {
                    Console.Write(' ');
                }
                else
                {
                    Console.WriteLine();
                }
            }
            i = j - 1;
        }
    }
}

7-9 C# 1.14 轮盘抽奖

用户定制一个抽奖轮盘,用户先输入轮盘上的扇形格子数n(n>0)。再按照轮盘顺时针方向依次输入n个格子上的奖项名称(字符串,可以重名)。最后输入一个整数m(1<=m<=100)代表轮盘顺时针旋转m个格子,输出从第一个轮盘扇形格子顺时针转动m个格子时轮盘的当前格子的奖项名称。

输入格式:

输入n(n>0)值。
输入n个奖项名称(可以重名),两个相邻奖项名称之间用一个空格间隔。
输入m([1,100])值。

输出格式:

输出轮盘顺时针旋转m个扇形格子后,转到的格子对应奖项名称。

输入样例:

在这里给出一组输入。例如:

4
a b c b
5

输出样例:

在这里给出相应的输出。例如:

b
class Solution7_9
{
    static void Main(string[] args)
    {

        int n = Convert.ToInt32(Console.ReadLine());
        String[] input = Console.ReadLine().Split(' ');
        int m = Convert.ToInt32(Console.ReadLine());

        int now = 0;
        now = (now + m) % n;
        Console.WriteLine(input[now]);
    }
}

7-10 C# 1.16 求累加和

编程计算a+aa+aaa+…+aa…a(n个a)的值,n和a由键盘输入。

输入格式:

第一行输入一个正整数值n(0<n<19)
第二行输出一个正整数值a(0<a<10)

输出格式:

对每一组输入,在一行中输出累加和。

输入样例:

在这里给出一组输入。例如:

4
1

输出样例:

在这里给出相应的输出。例如:

1234
class Solution7_10
{
    public static long GenerateNum(long len, long x)
    {
        long res = 0;
        for(int i = 0; i < len; i++)
        {
            res = res * 10 + x;
        }
        return res;
    }
    static void Main(string[] args)
    {
        long  n, a;
        n = Convert.ToInt32(Console.ReadLine());
        a = Convert.ToInt32(Console.ReadLine());

        long ans = 0;
        for(int i = 1; i <= n; i++)
        {
            ans += GenerateNum(i, a);
        }
        Console.WriteLine(ans);
    }
}

7-11 C# 1.15 回文数判定

从键盘上输入任意正整数,编程判断该数是否为回文数。所谓的回文数就是从左到右读这个数与从右到左读这个数是一样的。

输入格式:

输入一个正整数值

输出格式:

对每个输入,在一行中输出“yes”或“no”。

输入样例:

在这里给出一组输入。例如:

12021

输出样例:

在这里给出相应的输出。例如:

yes
using System;
using System.Collections.Generic;
using System.Text;

class Solution7_11
{
    public static bool is_palindrome(string s)
    {
        StringBuilder t = new StringBuilder("");
        for(int i = s.Length - 1; i >= 0; i--)
        {
            t.Append(s[i]);
        }
        return s == t.ToString();
    }
    static void Main(string[] args)
    {

        String s = Console.ReadLine();
        if (is_palindrome(s))
        {
            Console.WriteLine("yes");
        }
        else
        {
            Console.WriteLine("no");
        }
    }
}

网站公告

今日签到

点亮在社区的每一天
去签到