标题:上次WPH发的那么多C#题目 我来发答案
只看楼主
wyb86359978
Rank: 2
来 自:江苏苏州
等 级:论坛游民
帖 子:13
专家分:23
注 册:2011-6-11
结帖率:100%
 问题点数:0 回复次数:8 
上次WPH发的那么多C#题目 我来发答案
1、编一个程序,从键盘上输入三个数,用if语句和逻辑表达式把最大数找出来。

double a,b,c;
            Console.Write("输入第一个数字:");
            a = double.Parse(Console.ReadLine());
            Console.Write("输入第二个数字:");
            b = double.Parse(Console.ReadLine());
            Console.Write("输入第三个数字:");
            c = double.Parse(Console.ReadLine());
            double x;
            if (a >= b)
                x = a;
            else
                x = b;
            if (x >= c)
                x = x;
            else
                x = c;
            Console.WriteLine("最大数是{0}", x);
            Console.ReadKey();

2、编一个程序,从键盘上输入三个数,用if语句和逻辑表达式把最小数找出来。

double a,b,c;
            Console.Write("输入第一个数字:");
            a = double.Parse(Console.ReadLine());
            Console.Write("输入第二个数字:");
            b = double.Parse(Console.ReadLine());
            Console.Write("输入第三个数字:");
            c = double.Parse(Console.ReadLine());
            double x;
            if (a <= b)
                x = a;
            else
                x = b;
            if (x <= c)
                x = x;
            else
                x = c;
            Console.WriteLine("最小数是{0}", x);
            Console.ReadKey();

3、编一个程序,定义一个字符变量,使用if else语句,输入一个字符,如果它是大写字母,则把它转换成小写字母,如果它是小写字母,则把它转换成大写字母,否则不转换。然后输出结果。

char ch;
            ch = (char)Console.Read();
            if (ch >= 'A' && ch <= 'Z')
            {
                ch = (char)((int)ch + 32);
                Console.WriteLine("{0}", ch);
            }
            else
                if (ch >= 'a' && ch <= 'z')
                    ch = (char)((int)ch - 32);
            Console.WriteLine("{0}", ch);
            Console.ReadKey();

4、编一个程序,定义三个float类型变量x,y和z,输入三个实数,要求对这三个变量按照升序排序,然后输出结果。

float x, y, z;
            x = int.Parse(Console.ReadLine());            
            y = int.Parse(Console.ReadLine());
            z = int.Parse(Console.ReadLine());
            if (x>y)
            {
                float temp = x;
                x=y;
                y=temp;
            }
            if(x>z)
            {
                float temp = x;
                x=z;
                z=temp;
            }
            if(y>z)
            {
                float temp = y;
                y = z;
                z = temp;
            }
            Console.WriteLine("{0}  {1}  {2}", x, y, z);
            Console.ReadKey();

5、一个程序,定义一个双精度浮点数变量,从键盘输入一个数,判定它是否在[500,1200]的区间里。

double x = double.Parse(Console.ReadLine());
            if (x >= 500 && x <= 1200)
                Console.WriteLine("Yes!");
            else
                Console.WriteLine("No");
            Console.ReadKey();

6、编写一个程序,定义三个float类型的变量,分别从键盘上输入值给它们,  然后用if  else选择语句找出它们中的最小数,最后输出结果。

float a, b, c, min;
            a = int.Parse(Console.ReadLine());
            b = int.Parse(Console.ReadLine());
            c = int.Parse(Console.ReadLine());
            if (a < b)
                min = a;
            else
                min = b;
            if (min > c)
                min = c;
            Console.WriteLine("{0}", min);
            Console.ReadKey();

7、编一个程序,首先输入一个成绩(0-100),然后用if  else语句判断该成绩是优、良、中、及格还是不及格。
int a;
            Console.Write("输入成绩:");
            a = int.Parse(Console.ReadLine());
            if (a < 0 || a > 100)
            {
                Console.WriteLine("分数输入错误!");
            }
            else
            {
                if (a >= 0 && a < 60)
                    Console.WriteLine("不及格");
                if (a >= 60 && a < 70)
                    Console.WriteLine("及格");
                if (a >= 70 && a < 80)
                    Console.WriteLine("中");
                if (a >= 80 && a < 90)
                    Console.WriteLine("良好");
                if (a >= 90 && a <= 100)
                    Console.WriteLine("优");
            }
            Console.ReadKey();

8、编一个程序,输入一个成绩小于等于100的正整数,然后用switch语句判定其成绩等第。90到100分为优秀,80到89分为良好,60到79分为及格,59分以下为不及格。

int a, b;
            Console.Write("成绩:");
            a = int.Parse(Console.ReadLine());
            if (a < 0 || a > 100)
            {
                Console.WriteLine("分数输入错误!");
            }
            else
            {
                b = a / 10;
                switch (b)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                        Console.WriteLine("不及格");
                        break;
                    case 6:
                        Console.WriteLine("及格");
                        break;
                    case 7:
                        Console.WriteLine("中");
                        break;
                    case 8:
                        Console.WriteLine("良");
                        break;
                    case 9:
                    case 10:
                        Console.WriteLine("优");
                        break;
                }
            }
            Console.ReadKey();

9、编写一个程序,利用while循环语句,输入十个单精度浮点数,求出它们的平均值。


float sum = 0;
            float[] arr = new float[10];
            int a = 0;
            while(a<10)
            {
                arr[a] = int.Parse(Console.ReadLine());
                sum = sum + arr[a];
                a++;
            }
            Console.WriteLine("平均数为{0}", sum / 10);
            Console.ReadKey();

10、编一个程序,用的do while(while、for)循环语句来计算PI=4*(1-1/3+1/5+...+1/n),要求当n>1000时停止计算。

Do while:
double sum=0,flag=1;
            int n=1;
            do
            {
                sum = sum + flag * (1.0 / (double)n);
                n = n + 2;
                flag = -flag;

            } while (n <= 1000);
            Console.WriteLine("PI={0}", sum * 4.0);
            Console.ReadKey();

While:
double  sum=0, a = 1, n = 1;
            while (n <= 1000)
            {
                sum= sum + a / n;
                n += 2;
                a = -a;
            }
            Console.WriteLine("PI={0}", sum * 4);
            Console.ReadKey();

For:
double sum = 0, a = 1;
            for (int n = 1; n <= 1000; n += 2)
            {
                sum = sum + a / n;
                a = -a;
            }
            Console.WriteLine("PI={0}", sum * 4);
            Console.ReadKey();

11、编一个程序,定义一个有8个元素的整数数组,输入8个值后对该数组进行冒泡升序排序,然后顺序输出该数组的元素。

int[] arr = new int[8];
            for (int n = 0; n <= 7; n++)
            {
                Console.Write("输入要进行排序的第{0}个数:", n + 1);
                arr[n] = int.Parse(Console.ReadLine());
            }
            int i, j, temp;
            for (int n = 0; n < arr.Length; n++)
            {
                i = n;
                for (j = n + 1; j < 8; j++)
                {
                    if (arr[i] > arr[j])
                    {
                        temp = arr[n];
                        arr[n] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
            Console.Write("从小到大排序为:");
            for (int n = 0; n < 8; n++)
                Console.Write("{0}   ", arr[n]);
            Console.ReadKey();

12、编一个程序,定义一个数组,有10个元素,赋予常量值{97,65,32,17,58,43,79,81,23,10},找出这组数据中的最大数和最小数。

int[] arr = new int[10] { 97, 65, 32, 17, 58, 43, 79, 81, 23, 10 };
            int temp;
            for (int n = 0; n < arr.Length - 1; n++)
            {
                for (int m = n + 1; m < arr.Length; m++)
                {
                    if (arr[n] > arr[m])
                    {
                        temp = arr[n];
                        arr[n] = arr[m];
                        arr[m] = temp;
                    }
                }
            }
            Console.Write("最大数为{0},最小数为{1}", arr[9],arr[0]);
            Console.ReadKey();

13、编一个程序,定义数组,要求用for语句从键盘输入8个数,要求找出最大数和次大数。

int[] arr = new int[8];
            for (int a = 0; a < 8; a++)
            {
                arr[a] = int.Parse(Console.ReadLine());
            }
            for (int a = 0; a < 2; a++)
            {
                for (int b = a + 1; b < 8; b++)
                {
                    if (arr[a] < arr[b])
                    {
                        int temp = arr[a];
                        arr[a] = arr[b];
                        arr[b] = temp;
                    }
                }
            }
            Console.WriteLine("最大数为{0}  次大数为{1}", arr[0],arr[1]);
            Console.ReadKey();

14、编一个程序,由程序产生8个随机数,并且把这8个数按从小到大的顺序输出。

Random ran = new Random();
            int[] arr=new int[8];
            for (int a = 0; a < 8; a++)
            {
                arr[a]=ran.Next();
            }
            for (int a = 0; a < 7; a++)
            {
                for (int b = a + 1; b < 8; b++)
                {
                    if (arr[a] > arr[b])
                    {
                        int temp = arr[a];
                        arr[a] = arr[b];
                        arr[b] = temp;
                    }
                }
            }
            for (int a = 0; a < 8; a++)
            {
                Console.Write("{0}    ", arr[a]);
            }
            Console.ReadKey();

15、编一个程序,输入一个字符串,用foreach循环语句读出该字符串,统计其中大写字母的个数,小写字母的个数,以及其它字符的个数,最后输出结果。

string str;
            int upp = 0, low = 0, other = 0;
            str = Console.ReadLine();
            foreach (char ch in str)
            {
                if (ch >= 'A' && ch <= 'Z')
                    upp++;
                else
                {
                    if (ch >= 'a' && ch <= 'z')
                        low++;
                    else
                        other++;
                }         
            }
            Console.WriteLine("大写有{0}个,小写有{1}个,其他有{2}", upp, low, other);
            Console.ReadKey();

16、编写一个程序,用while语句嵌套,求出1!+2!+…+5!之和。

int a = 1, sum = 0;
            while (a <= 5)
            {
                int c = 1;
                for (int b = a; b > 0; b--)
                {
                    c = c * b;
                }
                sum = sum + c;
                a++;
            }
            Console.WriteLine("{0}", sum);
            Console.ReadKey();

17、编一个程序,用while循环语句,从键盘输入5个双精度浮点数,求出它们中的正数之和,以及负数之和。

double[] arr = new double[5];
            double sum1 = 0, sum2 = 0;
            int a=0;
            while (a < 5)
            {
                arr[a] = double.Parse(Console.ReadLine());
                if (arr[a] >= 0)
                    sum1 = sum1 + arr[a];
                if (arr[a] < 0)
                    sum2 = sum2 + arr[a];
                a++;
            }
            Console.WriteLine("正数和为{0},负数和为{1}。",sum1,sum2);
            Console.ReadKey();
18、编一个程序,输入三角形的三条边,求出它的面积。

double a, b, c;
            double s;
            a = double.Parse(Console.ReadLine());
            b = double.Parse(Console.ReadLine());
            c = double.Parse(Console.ReadLine());
            if (a < b)
            {
                double temp = a;
                a = b;
                b = temp;
            }
            if (a < c)
            {
                double temp = a;
                a = c;
                c = temp;
            }
            if (b < c)
            {
                double temp = b;
                b = c;
                c = temp;
            }
            if (a - b > c)
                Console.WriteLine("输入的三边不能组成三角形!");
            else
            {
                double q = (a + b + c) / 2;
                s = Math.Sqrt((q-a)*(q-b)*(q-c));
                Console.WriteLine("三角形面积为{0}", s);
            }
            Console.ReadKey();

19、编一个程序,用for循环语句,不用数组,从键盘输入5个浮点数,要求找出最大数和最小数。

int max,min,a, b;
            a = int.Parse(Console.ReadLine());
            max = min = a;
            for (int n = 0; n < 4; n++)
            {
                b = int.Parse(Console.ReadLine());
                if (b > max)
                    max = b;
                if (b < min)
                    min = b;
            }
            Console.WriteLine("最大值是{0},最小值是{1}", max, min);
            Console.ReadKey();

20、编写一个程序,利用for循环语句,不用数组,输入5个实数,把它们中的最大数和最小数找出来,并且求出这5个实数的平均值。

double x, max, min, sum;
            sum = max = min = double.Parse(Console.ReadLine());
            for (int i = 0; i < 4; i++)
            {
                x = double.Parse(Console.ReadLine());
                if (max < x)
                    max = x;
                if (min > x)
                    min = x;
                sum = sum + x;
            }
            Console.WriteLine("max={0},min={1},sum={2},average={3}", max, min, sum, sum / 5);
            Console.ReadKey();

21、编一个程序,定义一个字符串变量,输入字符串,然后再输入一个字符,在字符串中查找该字符出现的次数。

string str;
            int n = 0;
            str = Console.ReadLine();   
            char a =(char)Console.Read();
            Console.WriteLine("");
            foreach (char b in str)
            {
                if (b == a)
                {
                    n++;
                }
            }
            Console.WriteLine("{0}", n);
            Console.ReadKey();

22、编一个程序,利用for语句,打印一个如下形式的一个等腰三角形,顶点在屏幕中线上,行数从键盘输入。
           *******
            *****
             ***
              *

int n;
            n = int.Parse(Console.ReadLine());
            for (int a = 0; a < n; a++)
            {
                for (int b = 0; b < a; b++)
                {
                    Console.Write(" ");
                }
                for (int c = 1; c <= (n - a) * 2-1; c++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
            Console.ReadKey();

23、编一个程序,输入一个整数,判定它为几位数。例如,99是2位数,-100是3位数。

int a;
            a =int.Parse(Console.ReadLine());
            if (a < 0)
                a = -a;
            Console.WriteLine("{0}", a.ToString().Length);
            Console.ReadKey();

24、编一个程序,输入a,b,c的值,定义一个非静态方法,求出一元二次方程a*x*x+b*x+c=0的二个根。

int a, b, c;
            a = int.Parse(Console.ReadLine());
            b = int.Parse(Console.ReadLine());
            c = int.Parse(Console.ReadLine());
            double xa,xb;
            if (b * b > 4 * a * c)
            {
                 xa = (-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a);
                 xb = (-b - Math.Sqrt(b * b - 4 * a * c)) / (2 * a);
            Console.WriteLine("{0},{1}", xa, xb);
            }
            if (b * b == 4 * a * c)
            {
                xa = (-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a);
                Console.WriteLine("{0}", xa);
            }
            if (b * b < 4 * a * c)
                Console.WriteLine("无实数解!");
            Console.ReadKey();

25、编一个程序,要求输入一个正整数,判断它是否既是2又是3的整倍数。若是,则输出yes,否则输出no。

int a;
            a = int.Parse(Console.ReadLine());
            if (a % 5 == 0 && a % 7 == 0)
                Console.WriteLine("YES!");
            else
                Console.WriteLine("NO!");
            Console.ReadKey();

26、编一个程序,有一个水果店老板进了1888个西瓜,第一天卖了一半多二个,以后每天卖剩下的一半多二个,请问,这批西瓜几天能够卖完?

int a,n=0;
            a = int.Parse(Console.ReadLine());
            bool k=true;
            do
            {
                a = a / 2 - 2;
                n++;
                if (a<= 0)
                    k = false;
                if (k == false)
                    continue;
            } while (k==!false);
            Console.WriteLine("{0}", n);
            Console.ReadKey();

27、编一个程序,求出1到6000之间能够满足“用3除余2,用5除余3,用7除余2”的数。要求每行输出五个数。

int n=0;
            for (int a = 1; a <= 10000; a++)
            {
                int x = a % 3;
                int y = a % 5;
                int z = a % 7;
                if (x == 2 && y == 3 && z == 2)
                {
                    Console.Write("{0,-6}", a);
                    n++;
                    if (n % 5 == 0 && n != 0)
                        Console.WriteLine("");
                }
            }
            Console.ReadKey();

28、编一个程序,输入一个正整数,统计这个正整数的各位数字中零的个数,并且求出各位数字中的最大者。

string str = Console.ReadLine();
            int max = 0,n=0;
            foreach (char a in str)
            {
                if (a == '0')
                {
                    n++;
                };
                if (a > max)
                    max = a;
            }
            Console.WriteLine("{0}个0,最大数是{1}", n, (char)max);
            Console.ReadKey();

29、编一个程序,输出1到1000中能够被3整除但不能被5整除的数,并统计有多少个这样的数。

int n = 0, a;
            for (a = 1; a <= 100; a++)
            {
                if (a % 3 == 0 && a % 5 != 0)
                {
                    Console.Write("{0,-5}", a);
                    n++;
                }
            }
            Console.WriteLine();
            Console.WriteLine("{0}ge", n);
            Console.ReadKey();

30、编一个程序,定义一个字符串变量str,输入字符串,然后再输入一个字符或者字符串,通过使用字符串方法IndexOf()在字符串str中查找该字符或者字符串出现的次数。

string str1, str2;
            int sum = 0, n = 0, len;
            str1 = Console.ReadLine();
            str2 = Console.ReadLine();
            len = str1.Length;
            while (len > 0)
            {
                n = str1.IndexOf(str2);
                if (n >= 0)
                {
                    sum++;
                    str1 = str1.Substring(n + str2.Length, str1.Length - (n + str2.Length));
                    len = len - n - str2.Length;
                }
                else
                    len = n;
            }
            Console.WriteLine("sum={0}", sum);
            Console.ReadKey();

31、编一个程序,定义一个字符串变量,输入字符串,判断有没有连续重复的字符出现,统计重复字符出现次数。例如,xxxbayydzzz,其中x重复出现三次,y重复出现二次,z重复出现三次,共计字符重复八次。
方法一:
string str = Console.ReadLine();
            string a, b, c="";
            int n = 0;
            for (int i = 0; i < str.Length-1; i++)
            {
                a=str.Substring(i,1);
                b=str.Substring(i+1,1);
               
               
                if (a == b && c != b)
                {
                    n += 2;
                    c = a;
                }
                else if (a == b && c == b)
                {
                    n++;
                }
                else
                {
                    c = "";
                }
                //Console.WriteLine("sum={0}", n) ;
            }
            Console.WriteLine("{0}", n);
            Console.ReadKey();

方法二:
Console.Write("输入字符串:");
            string str = Console.ReadLine();
            int n = 0, len = str.Length;
            while (len > 0)
            {
                char ch = str[0];
                str = str.Substring(1, str.Length - 1);
                len = str.Length;
                int a = str.IndexOf(ch);
                if (a == 0)
                {
                    n += 1;
                    int b=0;
                    while (b == 0)
                    {
                        str = str.Substring(1, str.Length - 1);
                        b = str.IndexOf(ch);
                        n++;                     
                        len = str.Length;
                    }
                }
            }
            Console.WriteLine("重复次数为:{0}", n);
            Console.ReadKey();
方法三:
Console.WriteLine("输入字符串:");
            string str = Console.ReadLine();
            char[] arr = new char[str.Length];
            int x = 0;
            foreach (char ch in str)
            {
                arr[x] = ch;
                x++;
            }
            for(int k=0;k<str.Length;k++)
                Console.Write("{0}  ",arr[k]);
            Console.WriteLine();
            int n = 0;
            char b=' ';
            for ( int a=0; a < str.Length-1;a++ )
            {
                char ch1 = arr[a];
                if (arr[a + 1] == ch1 && arr[a + 1] != b)
                {
                    n += 2;
                    b = ch1;
                }
                else if (ch1 == arr[a + 1] && b == arr[a + 1])
                    n++;
                else
                    b = ' ';
            }
            Console.WriteLine("{0}", n);
            Console.ReadKey();

32、编一个程序,为五个考生3门课,建立一个整型二维数组(五行4列),做静态初始化输入考生成绩数据,计算出各个考生的总成绩。然后输出各个考生的各门课程的成绩,并且同时输出各个考生的总成绩。

int[,] arr = new int[5, 4];
            for (int a = 0; a < 5; a++)
            {
                arr[a, 3] = 0;
                for (int b = 0; b < 3; b++)
                {
                    Console.Write("输入第{0}位同学的第门成绩{1}:", a + 1, b + 1);
                    arr[a, b] = int.Parse(Console.ReadLine());
                    arr[a, 3] = arr[a, 3] + arr[a, b];
                }
            }
            for (int a = 0; a < 5; a++)
            {
                for (int b = 0; b < 3; b++)
                {
                    Console.Write("{0,-4}", arr[a, b]);
                }
                Console.WriteLine("总分为{0}。", arr[a, 3]);
            }
            Console.ReadKey();

33、编一个程序,为五个考生3门课,建立一个整型二维数组(五行4列),做静态初始化输入考生成绩数据,计算出各个考生的总成绩。然后按总成绩降序排序,再输出各个考生的各门课程的成绩和各个考生的总成绩。

int[,] arr = new int[5, 4];
            for (int a = 0; a < 5; a++)
            {
                arr[a, 3] = 0;
                for (int b = 0; b < 3; b++)
                {
                    Console.Write("输入第{0}位同学的第门成绩{1}:", a + 1, b + 1);
                    arr[a, b] = int.Parse(Console.ReadLine());
                    arr[a, 3] = arr[a, 3] + arr[a, b];
                }
            }
            for (int a = 0; a < 5; a++)
            {
                for (int b = 0; b < 3; b++)
                {
                    Console.Write("{0,-4}", arr[a, b]);
                }
                Console.WriteLine("总分为{0}。", arr[a, 3]);
            }
            for (int a = 0; a < 4; a++)
            {
                for (int b = a + 1; b < 5; b++)
                {
                    if (arr[a, 3] < arr[b, 3])
                    {
                        for (int c = 0; c < 4; c++)
                        {
                            int temp = arr[a, c];
                            arr[a, c] = arr[b, c];
                            arr[b, c] = temp;
                        }
                    }
                }            
            }
            Console.WriteLine("按降序排列后次序为:");
            for (int a = 0; a < 5; a++)
            {
                for (int b = 0; b < 3; b++)
                {
                    Console.Write("{0,-4}", arr[a, b]);
                }
                Console.WriteLine("总分为{0}",arr[a,3]);
            }
                Console.ReadKey();

34、编一个程序,从键盘输入3个数,找出最大数,并输出。

int[] arr = new int[3];
            for (int a = 0; a < 3; a++)
            {
                arr[a] = int.Parse(Console.ReadLine());
            }
            for (int a = 0; a < 2; a++)
            {
                for (int b = a + 1; b < 3; b++)
                {
                    if (arr[a] < arr[b])
                    {
                        int temp = arr[a];
                        arr[a] = arr[b];
                        arr[b] = temp;
                    }
                }
            }
            Console.WriteLine("{0}", arr[0]);
            Console.ReadKey();

35、编一个程序,从键盘输入3个数,找出最小数,并输出。

int[] arr = new int[3];
            for (int a = 0; a < 3; a++)
            {
                arr[a] = int.Parse(Console.ReadLine());
            }
            for (int a = 0; a < 2; a++)
            {
                for (int b = a + 1; b < 3; b++)
                {
                    if (arr[a] > arr[b])
                    {
                        int temp = arr[a];
                        arr[a] = arr[b];
                        arr[b] = temp;
                    }
                }
            }
            Console.WriteLine("{0}", arr[0]);
            Console.ReadKey();

36、编一个程序,输入十个实数,存入一个数组,用冒泡排序方法按升序输出

int[] arr = new int[10];
            for (int a = 0; a < 10; a++)
            {
                arr[a] = int.Parse(Console.ReadLine());
            }
            for (int a = 0; a < 9; a++)
            {
                for (int b = a + 1; b < 10; b++)
                {
                    if (arr[a] < arr[b])
                    {
                        int temp = arr[a];
                        arr[a] = arr[b];
                        arr[b] = temp;
                    }
                }
            }
            for (int a = 0; a < 10; a++)
                Console.Write("{0} ", arr[a]);
            Console.ReadKey();

37、编一个程序,定义一个字符串变量s,输入一个字符串,然后把字符串倒置,再输出字符串变量s。

string str="";
            string s = Console.ReadLine();
            for (int b = s.Length - 1; b >= 0; b--)
                str = str + s[b];
            Console.WriteLine("{0}", str);
            Console.ReadKey();

38、编一个程序,统计一笔开支。每个苹果0.8元,第一天买2个苹果,第二天开始,每天买的苹果数是前一天的二倍,直到每天购买的苹果数要超过100的时候,就不购买了。求出平均每天花费多少钱?

double s, sum = 0;
            int a=2,n=0;
            do
            {
                s = a * 0.8;
                sum = sum + s;
                a = a * 2;
                n++;
            } while (a <= 100);
            Console.WriteLine("{0}",sum/n);
            Console.ReadKey();

39、编一个程序,找出1到99之间所有的同构数。同构数是这样的一组数:它出现在平方数的右边。例如:5是25右边的数,25是625右边的数,5和25都是同构数。

int a;
            for (a = 1; a < 100; a++)
            {
                int b = a * a;
                string k= a.ToString();
                string s = b.ToString();
                int n = s.IndexOf(k);
                if (n == s.Length - k.Length)
                    Console.WriteLine("{0}", a);
                else
                    continue;
            }
            Console.ReadKey();

40、编一个程序,从三个红球,五个白球,六个黑球中任意取出八个球,且其中必须有白球,输出所有可能的方案。

for (int a = 1; a <=5; a++)
            {
                for (int b = 0; b <= 3; b++)
                {
                    for (int c = 0; c <= 6; c++)
                    {
                        if (a + b + c == 8)
                            Console.WriteLine("white={0},red={1},blank={2}", a, b, c);
                    }
                }               
            }
            Console.ReadKey();
搜索更多相关主题的帖子: 表达式 键盘 
2011-06-14 16:51
wyb86359978
Rank: 2
来 自:江苏苏州
等 级:论坛游民
帖 子:13
专家分:23
注 册:2011-6-11
得分:0 
竟然没人回复!情何以堪哇。。。
2011-06-14 17:09
dwwwing
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:1
帖 子:284
专家分:986
注 册:2008-10-11
得分:0 
建议lz学习下用三目运算符
这样程序看起来比较简洁。
  如第一题
   
程序代码:
   if (a >= b)
       x = a;
    else
        x = b;
    if (x >= c)
        x = x;
    else
         x = c;

 
   

换成
  x = a >= b ? a : b >= c ? x : c;
2011-06-14 17:14
wyb86359978
Rank: 2
来 自:江苏苏州
等 级:论坛游民
帖 子:13
专家分:23
注 册:2011-6-11
得分:0 
回复 3楼 dwwwing
学习了
2011-06-14 17:47
wyb86359978
Rank: 2
来 自:江苏苏州
等 级:论坛游民
帖 子:13
专家分:23
注 册:2011-6-11
得分:0 
回复 3楼 dwwwing
还有什么不好的就直说啊~我其实也是新手 很多东西不懂  谢谢了~~
2011-06-14 17:49
小蜗a3999
Rank: 1
等 级:新手上路
帖 子:2
专家分:3
注 册:2011-6-17
得分:0 
第一题这样对不对:
static void Mian()
{
 string s=Console.ReadLine();
 int x=Convert.ToInt32(s);
 string s=Console.ReadLine();
 int y=Convert.ToInt32(s);
 string s=Console.ReadLine();
 int z=Convert.ToInt32(s);
 if(x>y)
 {
  s=x;
 }
 else
 {
  s=y;
 }
 if(z>s) s=z;
 {
   Console.WriteLine("最大值为{0}",s);
   Console.ReadLine();
 }
}
2011-06-17 20:29
左边口袋
Rank: 1
等 级:新手上路
帖 子:2
专家分:3
注 册:2011-6-15
得分:0 
汗,楼主不错!
三目运算较短,但是只要能算出来就可以了!
一条路,只是有人开飞机.有人开火车,有人开车.有人走路罢了!
最终都会到,当然,当然最好不要走路啦!
2011-06-18 23:40
微风e
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-6-20
得分:0 
真用功~~
2011-06-20 12:58
ninghz
Rank: 2
等 级:论坛游民
帖 子:34
专家分:34
注 册:2011-7-8
得分:0 
偶是刚刚学C#的,这贴不错!!学习了!!

程序改变生活
2011-07-08 19:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-342834-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.080933 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved