• 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();
    }
    }
    }

  • 相关阅读:
    NOI2004 郁闷的出纳员 [Splay]
    关押罪犯 [二分]
    SCOI2010 传送带 [三分/模拟退火]
    POI2007 MEG-Megalopolis [树状数组]
    食物链 [并查集]
    SDOI2011 染色 [树链剖分]
    国家集训队 数颜色 [莫队]
    JSOI2008 星球大战 [并查集]
    [NOI2002] 银河英雄传说 (带权并查集)
    种树 [堆]
  • 原文地址:https://www.cnblogs.com/xiongxiaobai/p/5258796.html
Copyright © 2020-2023  润新知