zoukankan      html  css  js  c++  java
  • C#第八节课

    for的穷举迭代、while、do  while

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace for的穷举迭代
    {
    class Program
    {
    static void Main(string[] args)
    {
    //穷举
    //把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况
    //单位给发了张150元的购物卡,
    //拿着到超市买三类洗化用品。
    //洗发水15元,香皂2元,牙刷5元。
    //求刚好花完150元,有多少种买法
    //每种卖法都是各买几样
    //设洗发水x 150/15==10
    //牙刷y 150/5==30
    //香皂z 150/2==75
    //int biao = 0;
    //for (int x = 0; x <= 10; x++)
    //{
    // for (int y = 0; y <= 30; y++)
    // {
    // for (int z = 0; z <= 75; z++)
    // {
    // if (x * 15 + y * 5 + z * 2 == 150)
    // {
    // biao++;
    // Console.WriteLine("这是第"+biao+"种买法:洗发水买"+x+"种,牙刷买"+y+"种,香皂买"+z+"种");
    // }
    // }
    // }
    //}
    //Console.WriteLine("一共有"+biao+"种买法");
    //Console.ReadLine();

    //迭代:
    //从初始情况按照规律不断求解中间情况,最终推导出结果。

    //纸张可以无限次对折,纸张厚度为0.07毫米。
    //问多少次对折至少可以超过8848?
    //int a = 7;
    //int i = 1;
    //for (; ; i++)
    //{
    // a *= 2;
    // if(a>=884800000)
    // {
    // Console.WriteLine(i);
    // Console.WriteLine(a);
    // break;
    // }
    //}
    //Console.ReadLine();

    //while语句
    //int a = 7;
    //int b = 0;
    //while (a <= 884800000)
    //{
    // a *= 2;
    // b++;
    //}
    //Console.WriteLine(b);

    //do while语句

    int a = 7;
    int b = 0;
    do
    {
    a *= 2;
    b++;
    } while (a <= 884800000);
    Console.WriteLine(b);


    Console.ReadLine();
    }
    }
    }

  • 相关阅读:
    php设计模式-工厂模式(一)
    php Pthread 线程 互斥锁
    php Pthread 多线程 Worker
    PHP多进程实例
    C# this 关键字
    .NET FileStream文件流,StreamReader文本流,MemoryStream内存流几种流的实例
    深入理解Static关键字修饰符
    SQL Server 视图
    .NET初识委托
    RSA加密
  • 原文地址:https://www.cnblogs.com/xiongxiaobai/p/5258796.html
Copyright © 2011-2022 走看看