<C#任务导引教程>练习二

//6,goto语句求1+++100之和
using System;
class Program
{
    static void Main()
    {
        int i=1,sum=0;
        loop:if(i<=100)
        {
            sum+=i;
            i++;
            goto loop;
        }
        Console.WriteLine("sum={0}",sum);
    }
}
//7,用辗转法求两个整数的最大公约数
using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("请输入两个正整数:");
        Console.Write("  请输入第一个正整数:");
        string x = Console.ReadLine();
        int num1 = int.Parse(x);
        Console.Write("  请输入第二个正整数:");
        string y = Console.ReadLine();
        int num2 = int.Parse(y);
        Console.Write("\n{0}和{1}的最大公约数为:", num1, num2);
        for (; ; )
        {
            int resd = num1 % num2;
            if (resd == 0)
                break;
            num1 = num2;
            num2 = resd;
        }
        Console.WriteLine(num2);
    }
}
//8,用辗转法求两个整数的最大公约数
using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("请输入两个正整数:");
        Console.Write("  请输入第一个正整数:");
        string x = Console.ReadLine();
        int num1 = int.Parse(x);
        Console.Write("  请输入第二个正整数:");
        string y = Console.ReadLine();
        int num2 = int.Parse(y);
        Console.Write("\n{0}和{1}的最大公约数为:", num1, num2);
        while(num2!=0)//WHILE循环,除数不为0,余数为0
        {
            int resd = num1 % num2;
            if (resd == 0)
                break;
            num1 = num2;
            num2 = resd;
        }
        Console.WriteLine(num2);
    }
}
//9,设计程序输出Fibonacci数列的前20项,要求每行输出5个数据
using System;
class Program
{
    static void Main()
    {
        int fib1 = 0, fib2 = 1, fib3;
        Console.Write("{0,5}{1,5}", fib1, fib2);
        for (int n = 3; n <= 20; n++)
        {
            fib3 = fib1 + fib2;
            Console.Write("{0,5}", fib3);
            if (n % 5 == 0)
                Console.WriteLine();
            fib1 = fib2;
            fib2 = fib3;
        }
        Console.WriteLine();
    }
}
//C语言求1-100素数的方式
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
cout<<"输出1-100之间的质数"<<endl;
int m=2,i,k;
cout<<m<<" ";
for(m=3;m<=100;m+=2)
{
    k=sqrt(m);
    for(i=2;i<=k;i++)
        if(m%i==0)
            break;
        if(i>k)
            cout<<m<<" ";
}
cout<<endl;
return 0;
}
//10,编程输出1-100之间的全部素数
using System;
class Program
{
    static void Main()
    {
        int m, i, k;
        m = 2;
        Console.Write("{0,3}", m);
        for (m = 3;m<=100 ; m += 2)
        {
            k = (int)Math.Sqrt(m);
            for (i = 2; i <= k; i++)
                if (m % i == 0)
                    break;
            if (i > k)
                Console.Write("{0,3}",m);
        }
        Console.WriteLine();
    }
}
//11,百鸡问题
using System;
class Program
{
    static void Main()
    {
        int i, j, k;
        Console.WriteLine("  公鸡       母鸡       小鸡");
        for (i = 0; i <= 20; i++)
            for (j = 0; j <= 33; j++)
            {
                k = 100 - i - j;
                if ((5 * i + 3 * j + k / 3 == 100) && (k % 3 == 0))//注意k%3==0这个条件
                    Console.WriteLine("{0,6}{1,10}{2,10}", i, j, k);
            }
    }
}
12//创建一个静态数组的控制台应用程序,使用foreach语句遍历数组,将数组中的元素输出
using System;
class Program
{
    static void Main()
    {
        int[]arr=new int[5]{1,3,5,7,9};
        Console.WriteLine("显示数组元素:");
        foreach(int n in arr)//foreach()的用法
        {
            Console.Write("{0}",n+"  ");
        }
        Console.WriteLine();
    }
}
//13,创建一个控制台应用程序,将用户输入的一组数据存入到数组中,然后使用foreach语句遍历数组,并将数组中的元素输出
using System;
class Program
{
    static void Main()
    {
        int[] arr = new int[5];
        Console.WriteLine("请输入一组数:");
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = Convert.ToInt32(Console.ReadLine());
        }
        Console.WriteLine("显示输入后的数组:");
        foreach (int n in arr)
        {
            Console.Write("{0}", n + " ");
        }
        Console.WriteLine();
    }
}

posted @ 2014-03-22 20:20  zhangyongjian  阅读(263)  评论(0编辑  收藏  举报