学习C#-第一章-练习题(流程控制语句)

1.已知变量c = 7,d = 12;判断c是否是能被3整除的数;判断d是否是4的倍数\

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int c = 3, d = 12;
            if (c % 3 == 0)
            {
                Console.WriteLine("c能被3整除");
            }
            else
            {
                Console.WriteLine("c不能被3整除");
            }
            if (d % 4 == 0 && d != 0)
            {
                Console.WriteLine("d是4的倍数");
            }
            else
            {
                Console.WriteLine("d不是4的倍数");
            }
        }
    }
}

2.定义一个变量保存一个分数,判断该分数属于什么等级:60分以下E,60~70D,70~80C,80~90B,90或90以上是A

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int myScore = Convert.ToInt32(Console.ReadLine());
            if (myScore >= 90)
            {
                Console.WriteLine("A");
            }
            else if (myScore >= 80)
            {
                Console.WriteLine("B");
            }
            else if (myScore >= 70)
            {
                Console.WriteLine("C");
            }
            else if (myScore >= 60)
            {
                Console.WriteLine("D");
            }
            else if (myScore >= 0)
            {
                Console.WriteLine("E");
            }

            Console.ReadKey();
        }
    }
}

3.定义一个变量保存一个不多于五位数的整数,要求:一、求它是几位数,二、逆序打印出各位数字。

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = Convert.ToInt32(Console.ReadLine());
            int a;
            int index = 0;
            bool flag = true;
            int num2 = num;
            while (num > 0)
            {
                num = num / 10;
                index++;
            }
            Console.WriteLine("这个数有{0}位", index);
            while (true)
            {
                int x;
                x = num2 % 10;
                num2 = num2 / 10;
                index--;
                Console.Write(x+" ");
                if (index == 0)
                {
                    break;
                }
            }

            Console.ReadKey();
        }
    }
}

4.定义一个变量保存一个整数,然候逆序输出各个数字,并组成一个全新的数字。比如:”123”则输出”321”,如果是”120”则输出”21”

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = Convert.ToInt32(Console.ReadLine());
            int ys = num;
            int num1 = num;
            int count = 0;
            double a = 0;
            while (num > 0)
            {
                num = num / 10;
                count++;
            }
            //Console.WriteLine(count);
            while (count >= 1)
            {
                ys = num1 % 10;
                num1 /= 10;
                a = ys * Math.Pow(10, count - 1) + a;
                count--;
            }
            Console.WriteLine(a);

            Console.ReadKey();
        }
    }
}

5.输出定义三个变量分别存储双精度数字,进行比较,输出其中最小数。

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            double num1 = 1.23, num2 = 2.324, num3 = 3.32, min;
            min = num1 < num2 ? num1 : num2;
            min = min < num3 ? min : num3;
            Console.WriteLine(min);

            Console.ReadKey();
        }
    }
}

6.定义三个变量x,y,z分别保存三个整数,请把这三个数由小到大输出。

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 321, num2 = 2, num3 = 43, min, mid = 0, max;
            if (num1 < num2)
            {
                min = num1;
                max = num2;
                if (min < num3) { }
                else { min = num3; }
                if (max > num3) { }
                else { max = num3; }
            }
            else
            {
                min = num2;
                max = num1;
                if (min < num3) { }
                else { min = num3; }
                if (max > num3) { }
                else { max = num3; }
            }
            Console.WriteLine(max);
            if (min < num1 && num1 < num3)
            {
                mid = num1;
            }
            else if (min < num2 && num2 < max)
            {
                mid = num2;
            }
            else if (min < num3 && num3 < max)
            {
                mid = num3;
            }
            Console.WriteLine("从小到大顺序依次是:{0}<{1}<{2}", min, mid, max);

            Console.ReadKey();
        }
    }
}

7..打印2,22,222,2222,22222;
方法一:(用字符串拼接的方式)

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            string z = "";//定义一个空字符串
            for (int i = 0; i < 5; i++)//循环5次,字符串"2"拼接5次
            {
                z += "2";//累加字符串
                Console.Write(z + " ");//输出每次累加的字符串
            }

            Console.ReadKey();
        }
    }
}

方法二:用数学运算的方式解决

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;//定义一个变量
            for (int i = 0; i < 5; i++)//循环5次
            {
                sum = sum * 10 + 2;//累加
                Console.Write(sum + " ");//输出每次累加后的值
            }

            Console.ReadKey();
        }
    }
}

8.利用for循环控制100 - 999个数,每个数分解出个位,十位,百位。

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            for (var i = 100; i <= 999; i++)//循环100~999
            {
                int bw, sw, gw;//声明百位,十位,个位
                bw = i / 100;//赋值百位
                sw = i / 10 % 10;//赋值十位
                gw = i % 10;//赋值个位
                Console.WriteLine("百位是:{0}。十位是:{1}。个位是:{2}", bw, sw, gw);//输出
            }

            Console.ReadKey();
        }
    }
}

9.求出所有的5位数的回文数,显示在屏幕上。即12321是回文数,个位与万位相同,十位与千位相同。

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            for (var i = 10000; i <= 99999; i++)
            {
                int ww, qw, bw, sw, gw;//定义各个位数
                ww = i / 10000;//万位
                qw = i / 1000 % 10;//千位
                bw = i / 10 / 10 % 10;//百位
                sw = i / 10 % 10;//十位
                gw = i % 10;//个位
                if (gw == ww && sw == qw)//判断个位与万位,十位与千位是否相同
                {
                    Console.WriteLine(i);//为true。则输出i
                }
            }

            Console.ReadKey();
        }
    }
}

10.计算1 + 2 + 3 + 4 + 5 + 6 + 7.。。。+100的和////

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            for (int i = 1; i <= 100; i++)
            {
                sum += i;
            }
            Console.WriteLine(sum);

            Console.ReadKey();
        }
    }
}

11.计算1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 -。。。-100的差

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 0;
            for (int i = 1; i <= 100; i++)
            {
                num -= i;
            }
            Console.WriteLine(num);
        }
    }
}

8.计算1 * 2 * 3.。。。*100的积;

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 1;
            for (int i = 1; i <= 100; i++)
            {
                sum *= i;
            }
            Console.WriteLine(sum);
        }
    }
}

12.计算1 - 2 + 3 - 4 + 5.。。。-98 + 99 - 100的结果

int sum = 0;
for (int i = 1; i <= 100; i++)
{
if (i % 2 == 0)
{
sum -= i;
}
else
{
sum += i;
}
}
Console.WriteLine(sum);

13.题目:一球从h米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第n次落地时,共经过多少米?第n次反弹多高?

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            double h0 = 120;
            double s = 0;//定义第n次反弹到最高点后时共经过多少米
            int n = 6;//定义次数
            for (int i = 1; i <= 6; i++)  //循环6次
            {
                s = s + h0 + h0 / 2;//计算每一次,反弹到最高点共经过多少米
                h0 = h0 / 2;   //每一次反弹后的高度是上一次的一半
            }
            Console.WriteLine("第{0}次反弹{1}米,共经过{2}米", n, h0, s - h0);//(s-h0)表示反弹到最高点的经过的距离-此时反弹的高度

            Console.ReadKey();
        }
    }
}
14.题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
程序分析:采取逆向思维的方法,从后往前推断。
namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 1, sum = 0;
            for (int i = 1; i <= 9; i++)//循环9次
            {
                num = (num + 1) * 2;  //逆向思维。从后往前推
            }
            Console.WriteLine(num);

            Console.ReadKey();
        }
    }
}

15.古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月开始每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?

//思路分析:可推算得到每个月兔子总数按一定规律。 1 1 2 3 5 8 13。。。(即前两项之和等于第三项)

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int b = 0;
            int a1 = 1;
            int a2 = 1;
            for (int i = 3; i <= 12; i++)
            {
                b = a1 + a2;
                a1 = a2;
                a2 = b;
                //Console.WriteLine("总共有{0}兔子", 2 * b);
            }
            Console.WriteLine("总共有{0}对兔子", b);

            Console.ReadKey();
        }
    }
}

16.已知一个数字17,判断该数字是否是完数;该数刚好等于它的因子之和称为完数;例如:6 = 1 + 2 + 3

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 17;
            int add = 0;
            for (int i = 1; i < num; i++)
            {
                if (num % i == 0)//判断i是否是因子
                {
                    Console.WriteLine("因子:" + i);
                    add += i;//因子累加
                }
            }
            if (add == num)
            {
                Console.WriteLine("是完数");
            }
            else
            {
                Console.WriteLine("不是完数");
            }

            Console.ReadKey();
        }
    }
}

17.//打印一下图案:
//55555
//44444
//33333
//22222
//11111

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 5; i >= 1; i--)
            {
                for (int j = 1; j <= 4; j++)
                {
                    Console.Write(i);
                }
                Console.Write("\n");
            }

            Console.ReadKey();
        }
    }
}

18.打印一下图案:
//54321
//54321
//54321
//54321

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 5; j >= 1; j--)
                {
                    Console.Write(j);
                }
                Console.Write("\n");
            }

            Console.ReadKey();
        }
    }
}

19.打印一下图案:
//11111
//12221
//12221
//11111

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 4; i++)
            {
                if (i == 2 || i == 3) //判断是否是第二行或者第三行
                {
                    for (int j = 1; j <= 5; j++) //循环
                    {
                        if (j == 1 || j == 5) //判断是否是第一列或者是第五列
                        {
                            Console.Write("1"); //true 则输出1
                        }
                        else
                        {
                            Console.Write("2"); //false 则输出2
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < 5; j++) //不是属于第二三行的情况下,循环5次
                    {
                        Console.Write("1"); //1
                    }
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

20.打印一下图案:
1
11
111
1111
11111

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 5; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(1);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

21.打印一下图案:
11111
1111
111
11
1

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 5; i >= 1; i--)
            {
                for (int j = i; j >= 1; j--)
                {
                    Console.Write(1);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

22.打印一下图案:
1
111
11111
1111111

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 8; i += 2)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(1);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

23.题目:判断101 - 200之间有多少个素数,并输出所有素数。
程序分析:.
* 素数是:只能被1或本身整除的数,如:3,5,7,11,131…
* 判断素数的方法:用一个数分别去除2到平方根
* 其实用这个数分别去除2到他本身少1的数也可以,但是运算时间增加了
* 如果能被整除,则表明此数不是素数,反之是素数。

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 101; i <= 200; i++)//遍历101到00
            {
                bool isSuShu = true;//设置一个bool值,用来判断是否为素数
                for (int j = 2; j < Math.Sqrt(i); j++)//循环2-这个数的平方根之间的数
                {
                    if (i % j == 0)//判断是否能整除
                    {
                        isSuShu = false;//可以整除,则不是素数,
                        break;//然后跳出循环
                    }
                }
                if (isSuShu)//判断是否为素数
                {
                    Console.Write(i + " ");//true 则输出该数
                }
            }
            Console.ReadKey();
        }
    }
}

24.题目:输出1!+2!+3!+….+ 9!的结果,3!= 1 * 2 * 3;5!= 1 * 2 * 3 * 4 * 5
程序分析:此程序只是把累加变成了累乘。
1、先求该项阶乘的值
* 2、累加求求

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int add = 0, sum;//分别定义一个累加的和。和累乘的和
            for (int i = 1; i <= 9; i++)//循环1-9
            {
                sum = 1;//每次进行完累乘后,初始化sum
                for (int j = 1; j <= i; j++)//累乘循环
                {
                    sum = sum * j;//进行累乘
                }
                add = add + sum;//把每个累乘后的值累加
            }
            Console.WriteLine(add);//输出累加的值

            Console.ReadKey();
        }
    }
}

25.题目:输出9* 9口诀。
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
程序分析:分行与列考虑,共9行9列,i控制行,jC列。
表达式: i + “” + j + “=” + i j,这里要用两个for循环控制输出和换行

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 9; i++)//循环第一行到第九行
            {
                for (int j = 1; j <= i; j++)//循环列数,列数大小=该行行数
                {
                    Console.Write("{0}*{1}={2}\t", j, i, j * i);//输出表达式
                }
                Console.WriteLine();//换行
            }

            Console.ReadKey();
        }
    }
}

26.题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的, 问海滩上原来最少有多少个桃子 /?
程序分析:从1个桃子开始计算是否满足,不满足继续增加桃子数计算,一直到满足以上条件才结束查找;

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; true; i++)//从1开始循环
            {               
                if (i%5!=1)//把不满足条件的数字跳过,降低循环次数,减少资源消耗
                {
                    continue;
                }
                float res = i;//把
                for (int j = 0; j < 5; j++)//循环5次,
                {
                    res = (res - 1) * 4 / 5;//每一次吃过后剩下的桃子
                }
                //Console.WriteLine(res);
                int temp = (int)res;//res取整
                //Console.WriteLine(temp);
                if (temp == res)//res如果是整数的话,就会满足条件。res的值就有可能是小数,这样取整的话就不会与temp相等,就不会满足条件。
                {
                    Console.WriteLine(i);//输出满足条件后的桃子数
                    break;//跳出循环
                }
            }
            Console.ReadKey();
        }
    }
}

27题目:打印.
00A00
0A0A0
A000A
0A0A0
00A00

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 5; i++)
            {
                for (int j = 1; j <= 5; j++)
                {

                    if ((i + j) % 4 == 0 || (i == 2 && j == 4) || (i == 4 && j == 2))
                    {
                        Console.Write("A" + " ");
                    }
                    else
                    {
                        Console.Write("0" + " ");
                    }
                }
                Console.WriteLine();
            }

            Console.ReadKey();
        }
    }
}

提高题(不懂得可以滤过哈):
28:打印万年历

namespace _007_test6_万年历_
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入你要查询的年份:");
            int currentYear = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();
            int x, y;
            for (int i = 1; i <= 12; i++)//遍历每个月
            {
                 x = WhatDay(currentYear, i);//得出某年某月的第一天是星期几
                 y = EveryMonthDays(currentYear, i);//得出某年某月的天数
                int index;//计数器
                Console.WriteLine("            2018年{0}月\n",i);
                Console.WriteLine("一   二   三   四   五   六   日");
                Console.WriteLine("---------------------------------");


                 index = x;
                int monthIndex2 = 1;

                while (monthIndex2<=y)
                {
                    for (int j = 1; j < index; j++)
                    {
                        Console.Write("  "+"   ");
                    }
                    for (int k = index; k <=7; k++)
                    {

                        if (monthIndex2>y)
                        {
                            break;
                        }

                        if (monthIndex2<=9)
                        {
                            Console.Write(" "+monthIndex2+ "   ");
                        }
                        else
                        {
                            Console.Write(monthIndex2 + "   ");
                        }

                        monthIndex2++;
                    }
                    Console.WriteLine();
                    index = 1;
                }
            Console.WriteLine("\n\n");

            }
            Console.ReadKey();

        }
        static bool IsLeapYear(int year)//判断某年是不是闰年
        {
            if (year%4==0&&year%100!=0||year%400==0)
            {
                 //   Console.WriteLine("shi 闰年");
                return true;
            }
            else
            {
             //   Console.WriteLine("不是 闰年");
                return false;
            }
        }

        static int WhatDay(int currentYear,int month)//判断从某年某月第一天是星期几
        {
            int num;
            int totalDays=0;
            for (int i = 1900; i < currentYear; i++) 
            {
                if (IsLeapYear(i))
                {
                    totalDays += 366;
                }
                else
                {
                    totalDays += 365;
                }

            }
            for (int j = 1; j < month; j++)
            {
                totalDays += EveryMonthDays(currentYear,j);
            }

            num = totalDays % 7;
            return num+1;
        }
        static int EveryMonthDays(int year, int month)//判断某年每个月的天数
        {
            int i = month;
            int monthDay;
            if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
            {
                monthDay = 31;
            }

            else if (i == 4 || i == 6 || i == 9||i==11)
            {
                monthDay = 30;
            }

           else if (i==2&&IsLeapYear(year)==true)
            {
                monthDay = 29;
            }
            else
            {
                monthDay = 28;
            }
            return monthDay;
        }
    }
}

你可能感兴趣的:(C#基础)