• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Brian Warehouse

            Some birds aren`t meant to be caged, their feathers are just too bright... ...
            posts - 40, comments - 16, trackbacks - 0, articles - 1

            Nearly prime number is an integer positive number for which it is possible to find such primes P1 and P2 that given number is equal to P1*P2. There is given a sequence on N integer positive numbers, you are to write a program that prints “Yes” if given number is nearly prime and “No” otherwise.

            Input

            Input file consists of N+1 numbers. First is positive integer N (1£N£10). Next N numbers followed by N. Each number is not greater than 109. All numbers separated by whitespace(s).

            Output

            Write a line in output file for each number of given sequence. Write “Yes” in it if given number is nearly prime and “No” in other case.
            不用管Nearly prime numbers 到底是個什么數(shù),總之是兩個質(zhì)數(shù)的乘積就對了。枚舉的范圍: 2 ~ 32000 (104.5 )
            我的思路是: 首先把2~32000之間的所有素數(shù)都存放在一個數(shù)組里,然后當(dāng)你輸入一個數(shù)據(jù)時,先讓其逐一模除這個數(shù)組里的素數(shù),一旦模除結(jié)果為0,則計算出他們的商,再判斷商是否也為素數(shù)。注意數(shù)據(jù)是兩個數(shù)的平方的處理

            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse#include <stdio.h>
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse#include 
            <math.h>
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
            int prime[30000],M=0;
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
            int isP(int n) //判斷是否為素數(shù),非常精確而高效
            SGU 113 Nearly prime numbers  - Icho - Brian WarehouseSGU 113 Nearly prime numbers  - Icho - Brian Warehouse
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse{
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            int i=2,t=sqrt(n);
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            if ((n != 2 && !(n % 2)) || (n != 3 && !(n % 3)) || 
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        (n 
            != 5 && !(n % 5)) || (n != 7 && !(n % 7)))
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        
            return 0;
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            for (; i<=t; i++)
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        
            if (n%== 0)
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse            
            return 0;
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            return 1;
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse}

            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
            int isNP(int n)
            SGU 113 Nearly prime numbers  - Icho - Brian WarehouseSGU 113 Nearly prime numbers  - Icho - Brian Warehouse
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse{
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            int i=0,t=sqrt(n),r;
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            for (; prime[i]<=t; i++// 平方在此處理
            SGU 113 Nearly prime numbers  - Icho - Brian WarehouseSGU 113 Nearly prime numbers  - Icho - Brian Warehouse
                SGU 113 Nearly prime numbers  - Icho - Brian Warehouse{
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        
            if (n%prime[i] == 0// 模除試商
            SGU 113 Nearly prime numbers  - Icho - Brian WarehouseSGU 113 Nearly prime numbers  - Icho - Brian Warehouse
                    SGU 113 Nearly prime numbers  - Icho - Brian Warehouse{
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse            r
            =n/prime[i]; // 求商
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
                        if (isP(r))
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse                
            return 1;
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        }

            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    }

            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            return 0;
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse}

            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
            int main()
            SGU 113 Nearly prime numbers  - Icho - Brian WarehouseSGU 113 Nearly prime numbers  - Icho - Brian Warehouse
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse{
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            int i=3,N,m;    
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            for (prime[M++]=2; i<32000; i+=2)
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        
            if (isP(i))
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse            prime[M
            ++]=i;
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    scanf(
            "%d",&N);
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            while (N--)
            SGU 113 Nearly prime numbers  - Icho - Brian WarehouseSGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse{
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        scanf(
            "%d",&m);
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        
            if (m==6)
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse            printf(
            "Yes\n"); // 程序中唯一未解決的問題,望各路大牛指教!
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
                    else printf("%s\n",isNP(m) ? "Yes":"No");
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    }

            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
            return 0;
            SGU 113 Nearly prime numbers  - Icho - Brian Warehouse}

            posted @ 2010-08-17 13:23 Brian 閱讀(328) | 評論 (0)編輯 收藏

            You are given natural numbers a and b. Find ab-ba.

            Input

            Input contains numbers a and b (1≤a,b≤100).

            Output

            Write answer to output.

            Sample Input

            2 3
            

            Sample Output

            -1

            一看到這種題目,就想到高精度算法,Java的大數(shù)。
            此舉確實流氓了一點,不過確實過了,鄙人的第一題SGU啊,不許打擊。
            過段時間看看能不能寫出 (C++ Edition) ,啥也別說了,上代碼。

            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouseimport java.math.*;
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse
            import java.util.*;
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse
            public class Solution
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian WarehouseSGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse{
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse   
            public static void main(String[] args)
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian WarehouseSGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse   
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse{
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse      Scanner in 
            = new Scanner(System.in);
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse      
            int a = in.nextInt();
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse      
            int b = in.nextInt();
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse      
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse      BigInteger A
            =BigInteger.valueOf(a);
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse      BigInteger B
            =BigInteger.valueOf(b); // A^B
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse
                  BigInteger rA=BigInteger.valueOf(a);
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse      BigInteger rB
            =BigInteger.valueOf(b); // store the result after computing
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse
                  
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse      
            for(int i=1; i<b; i++// just b-1 times
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse
                      rA=rA.multiply(A);
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse      
            for(int i=1; i<a; i++)    
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse          rB
            =rB.multiply(B);
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse      System.out.println(rA.subtract(rB)); 
            // sub
            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse
               }

            SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse}

            附《Core Java I》里關(guān)于 大數(shù)的簡單介紹,講的算比較清晰的了:
            Big Numbers
            If the precision of the basic integer and floating-point types is not sufficient, you can
            turn to a couple of handy classes in the java.math package: BigInteger and BigDecimal. These
            are classes for manipulating numbers with an arbitrarily long sequence of digits. The
            BigInteger class implements arbitrary precision integer arithmetic, and BigDecimal does the
            same for floating-point numbers.
            Use the static valueOf method to turn an ordinary number into a big number:
            BigInteger a = BigInteger.valueOf(100);
            Unfortunately, you cannot use the familiar mathematical operators such as + and * to
            combine big numbers. Instead, you must use methods such as add and multiply in the big
            number classes.
            BigInteger c = a.add(b); // c = a + b
            BigInteger d = c.multiply(b.add(BigInteger.valueOf(2))); // d = c * (b + 2)
            C++ NOTE: Unlike C++, Java has no programmable operator overloading. There was no way
            for the programmer of the BigInteger class to redefine the + and * operators to give the add and
            multiply operations of the BigInteger classes. The language designers did overload the + operator
            to denote concatenation of strings. They chose not to overload other operators, and they
            did not give Java programmers the opportunity to overload operators in their own classes.
            Listing 3–6 shows a modification of the lottery odds program of Listing 3–5, updated to
            work with big numbers. For example, if you are invited to participate in a lottery in
            which you need to pick 60 numbers out of a possible 490 numbers, then this program
            will tell you that your odds are 1 in 7163958434619955574151162225400929334117176
            12789263493493351 013459481104668848. Good luck!
            The program in Listing 3–5 computed the statement
            lotteryOdds = lotteryOdds * (n - i + 1) / i;
            When big numbers are used, the equivalent statement becomes
            lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1)).divide(BigInteger.valueOf(i));
            Listing 3–6 BigIntegerTest.java
            1. import java.math.*;
            2. import java.util.*;
            3.
            4. /**
            5. * This program uses big numbers to compute the odds of winning the grand prize in a lottery.
            6. * @version 1.20 2004-02-10
            7. * @author Cay Horstmann
            8. */
            9. public class BigIntegerTest
            10. {
            11. public static void main(String[] args)
            12. {
            13. Scanner in = new Scanner(System.in);
            14.
            15. System.out.print("How many numbers do you need to draw? ");
            16. int k = in.nextInt();
            17.
            18. System.out.print("What is the highest number you can draw? ");
            19. int n = in.nextInt();
            20.
            21. /*
            22. * compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k)
            23. */
            24.
            25. BigInteger lotteryOdds = BigInteger.valueOf(1);
            26.
            27. for (int i = 1; i <= k; i++)
            28. lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1)).divide(
            29. BigInteger.valueOf(i));
            30.
            31. System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!");
            32. }
            33. }

            java.math.BigInteger

            ? BigInteger add(BigInteger other)
            ? BigInteger subtract(BigInteger other)
            ? BigInteger multiply(BigInteger other)
            ? BigInteger divide(BigInteger other)
            ? BigInteger mod(BigInteger other)
            returns the sum, difference, product, quotient, and remainder of this big integer and
            other.
            ? int compareTo(BigInteger other)
            returns 0 if this big integer equals other, a negative result if this big integer is less
            than other, and a positive result otherwise.
            ? static BigInteger valueOf(long x)
            returns a big integer whose value equals x.
            ? BigDecimal add(BigDecimal other)
            ? BigDecimal subtract(BigDecimal other)
            ? BigDecimal multiply(BigDecimal other)
            ? BigDecimal divide(BigDecimal other, RoundingMode mode) 5.0
            returns the sum, difference, product, or quotient of this big decimal and other.
            To compute the quotient, you must supply a rounding mode. The mode
            RoundingMode.HALF_UP is the rounding mode that you learned in school (i.e., round
            down digits 0 . . . 4, round up digits 5 . . . 9). It is appropriate for routine
            calculations. See the API documentation for other rounding modes.
            ? int compareTo(BigDecimal other)
            returns 0 if this big decimal equals other, a negative result if this big decimal is less
            than other, and a positive result otherwise.
            ? static BigDecimal valueOf(long x)
            ? static BigDecimal valueOf(long x, int scale)
            returns a big decimal whose value equals x or x /10scale.

            posted @ 2010-08-17 13:21 Brian 閱讀(579) | 評論 (0)編輯 收藏

            別看了,老子瘋了。SGU的進(jìn)展肯定相當(dāng)緩慢,但是我會用心做的。下面先轉(zhuǎn)一個大牛的Sgu袖珍分類。
            101 Domino 歐拉路
            102 Coprime 枚舉/數(shù)學(xué)方法
            103 Traffic Lights 最短路
            104 Little Shop of Flowers 動態(tài)規(guī)劃
            105 Div 3 找規(guī)律
            106 The Equation 擴(kuò)展歐幾里德
            107 987654321 Problem 找規(guī)律
            108 Self-numbers II 枚舉+篩法遞推
            109 Magic of David Copperfield II 構(gòu)造
            110 Dungeon 計算幾何+模擬
            111 Very Simple Problem 模擬筆算開方
            112 a^b-b^a 高精度
            113 Nearly Prime Numbers 判質(zhì)數(shù)
            114 Telecasting Station 找中位數(shù)
            115 Calendar 模擬
            116 Index of Super-prime 動態(tài)規(guī)劃
            117 Counting 快速冪
            118 Digital Root 模擬
            119 Magic Pairs 枚舉
            120 Archipelago 計算幾何
            121 Bridges Painting 構(gòu)造
            122 The Book 構(gòu)造哈密頓回路
            123 The Sum 遞推
            124 Broken line 計算幾何
            125 Shtirlits 搜索
            126 Boxes 數(shù)學(xué)方法
            127 Telephone directory 統(tǒng)計
            128 Snake 并查集 + 樹狀數(shù)組
            129 Inheritance 計算幾何
            130 Circle 卡特蘭數(shù)
            131 Hardwood floor 狀態(tài)壓縮動規(guī)
            132 Another Chocolate Maniac 狀態(tài)壓縮動規(guī)
            133 Border 貪心
            134 Centroid 樹型DP
            135 Drawing Lines 找規(guī)律
            136 Erasing Edges 數(shù)學(xué)方法
            137 Funny Strings 構(gòu)造
            138 Games of Chess 構(gòu)造
            139 Help Needed! 數(shù)學(xué)方法
            140 Integer Sequences 擴(kuò)展歐幾里德
            141 Jumping Joe 擴(kuò)展歐幾里德
            142 Keyword 枚舉
            143 Long Live the Queen 樹型DP
            144 Meeting 數(shù)學(xué)方法
            145 Strange People 二分答案+搜索
            146 The Runner 數(shù)學(xué)方法
            147 Black-white King 枚舉
            148 B-Station 枚舉+快排/二分/堆
            149 Computer Network 樹型DP
            150 Mr. Beetle II 枚舉
            151 Construct a triangle 計算幾何構(gòu)造
            152 Making round 構(gòu)造
            153 Playing With Matches 動態(tài)規(guī)劃+找循環(huán)節(jié)
            154 Factorial 二分 數(shù)學(xué)方法
            155 Cartesian Tree 建笛卡爾樹
            156 Strange Graph 縮團(tuán)+歐拉回路
            157 Patience 搜索+開表
            158 Commuter Train 枚舉
            159 Self-replicating Numbers 擴(kuò)展隊列+高精度
            160 Magic Multiplying Machine 動態(tài)規(guī)劃
            161 Intuitionistic Logic 搜索*
            162 Pyramids 數(shù)學(xué)方法
            163 Wise King 模擬
            164 Airlines 貪心
            165 Basketball 構(gòu)造
            166 Editor 模擬
            167 I-country 動態(tài)規(guī)劃
            168 Matrix 動態(tài)規(guī)劃
            169 Numbers 數(shù)學(xué)方法
            170 Particles 貪心
            171 Sarov zones 貪心
            172 eXam 判斷二分圖
            173 Coins 高斯消元
            174 Wall 并查集+Hash
            175 Encoding 搜索/動態(tài)規(guī)劃
            176 Flow construction 上下界網(wǎng)絡(luò)流
            177 Sqare 倒序染色
            178 Chain 數(shù)學(xué)方法
            179 Brackets light 找規(guī)律
            180 Inversions 歸并排序/高級數(shù)據(jù)結(jié)構(gòu)
            181 X-Sequence 找循環(huán)節(jié)
            182 Open the Brackets 搜索
            183 Painting the balls 動態(tài)規(guī)劃優(yōu)化
            184 Patties 直接計算
            185 Two shortest 網(wǎng)絡(luò)流
            186 The chain 貪心
            187 Twist and whirl -- want to cheat 塊狀鏈表
            188 Factory guard 數(shù)學(xué)方法
            189 Perl-like Substr 模擬
            190 Dominoes 二分圖匹配
            191 Exhibition 貪心
            192 RGB 離散化 + 統(tǒng)計
            193 Chinese Girls' Amusement 數(shù)學(xué)方法 + 高精度
            194 Reactor Cooling 網(wǎng)絡(luò)流
            195 New Year Bonus Grant 貪心
            196 Matrix Multiplication 數(shù)學(xué)方法
            197 Nice Patterns Strike Back 動態(tài)規(guī)劃+矩陣
            198 Get Out! 計算幾何
            199 Beautiful People 最長非降子序列
            200 Cracking RSA 高斯消元
            201 Non Absorbing DFA 動態(tài)規(guī)劃
            202 The Towers of Hanoi Revisited 動態(tài)規(guī)劃構(gòu)造
            203 Hyperhuffman 貪心
            204 Little Jumper 二分+數(shù)學(xué)方法*
            205 Quantization Problem 動態(tài)規(guī)劃

            posted @ 2010-08-17 13:17 Brian 閱讀(1196) | 評論 (0)編輯 收藏

                 摘要: /*Title: 文件管理Author: BrianDate: 2010/06/17*/#include <iostream>#include <fstream>#include <cstdlib>#include <cstring>using namespace&nbs...  閱讀全文

            posted @ 2010-08-17 13:04 Brian 閱讀(1879) | 評論 (5)編輯 收藏

            一、實驗內(nèi)容

            利用高級語言,設(shè)計一個采用二級或二級以上的多級文件目錄管理系統(tǒng),實現(xiàn)對文件的基本操作,如:文件的建立、打開、關(guān)閉、復(fù)制、撤銷、檢索等。

            二、實驗?zāi)康?/span>

            用高級語言編寫和調(diào)試一個簡單的文件系統(tǒng),模擬文件管理的工作過程。從而對各種文件操作命令的實質(zhì)內(nèi)容和執(zhí)行過程有比較深入的了解。

            要求設(shè)計一個 n個用戶的文件系統(tǒng),每次用戶可保存m個文件,用戶在一次運行中只能打開一個文件,對

            文件必須設(shè)置保護(hù)措施,且至少有Createdeleteopenclosereadwrite等命令。

            、實驗環(huán)境

            1PC微機(jī)。

            2Windows 操作系統(tǒng)。

            3C/C++/VB開發(fā)集成環(huán)境。

            四、實驗題目

            設(shè)計一個10個用戶的文件系統(tǒng),每次用戶可保存10個文件,一次運行用戶可以打開5個文件。 程序采用二級文件目錄(即設(shè)置主目錄[MFD])和用戶文件目錄(UED)。另外,為打開文件設(shè)置了運行文件目錄(AFD)。 為了便于實現(xiàn),對文件的讀寫作了簡化,在執(zhí)行讀寫命令時,只需改讀寫指針,并不進(jìn)行實際的讀寫操作。

            算法設(shè)計思想:

            (1)       因系統(tǒng)小,文件目錄的檢索使用了簡單的線性搜索。

            (2)       文件保護(hù)簡單使用了三位保護(hù)碼:允許讀寫執(zhí)行、對應(yīng)位為 1,對應(yīng)位為0,則表示不允許讀寫、執(zhí)行。

            (3)       程序中使用的主要設(shè)計結(jié)構(gòu)如下:

            主文件目錄和用戶文件目錄( MFDUFD),  打開文件目錄( AFD)(即運行文件目錄)

            M D F

            U F D

            A F D

            用戶名

            文件名

            打開文件名

            文件目錄指針

            保護(hù)碼

            打開保護(hù)碼

            用戶名

            文件長度

            讀寫指針

            文件目錄指針

            文件名

             

             

            ·

            ·

            ·

             

            posted @ 2010-08-17 13:03 Brian 閱讀(1181) | 評論 (0)編輯 收藏

                 摘要: /*Title: 首次適應(yīng)算法Author: BrianDate: 2010/05/31*/#include <iostream>#include <cstdlib> // system()using namespace std;typedef struct SNo...  閱讀全文

            posted @ 2010-08-17 13:02 Brian 閱讀(1596) | 評論 (0)編輯 收藏

                 摘要: 一、實驗內(nèi)容 利用高級語言,實現(xiàn)存儲分配算法,開發(fā)一個存儲管理的模擬程序,對內(nèi)存空間的管理和分配。內(nèi)存空間的管理可采用固定分區(qū)管理方式,可變分區(qū)管理方式,頁式存儲管理,段式存儲管理等方案。 二、實驗?zāi)康?一個好的計算機(jī)系統(tǒng)不僅要有一個足夠容量的、存取速度高的、穩(wěn)定可靠的主存儲器,而且要能合理地分配和使用這些存儲空間。當(dāng)用戶提出申請存儲器空間時,存儲管理必須根據(jù)申請者的要求,按一定的策略分析主...  閱讀全文

            posted @ 2010-08-17 13:00 Brian 閱讀(2200) | 評論 (2)編輯 收藏

            /*
            Title: 時間片輪轉(zhuǎn)法
            Author: Brian
            Date: 2010/04/09
            */
            #include 
            <iostream>
            #include 
            <cstdlib>
            using namespace std;

            typedef 
            struct PNode { // PCB
               struct PNode *next; //指向下一個節(jié)點的指針
               char name[12];    // 進(jìn)程名
               int All_Time;    // 總運行時間
               int Runed_Time;    // 已運行時間
               char state;        // 進(jìn)程狀態(tài) Ready / End
            }* Proc; // 指向該PCB的指針

            int ProcNum; // 全局變量,用于用戶自己確定進(jìn)程個數(shù)

            void InitPCB(Proc &H) { //初始化就緒隊列
                cout<<"輸入總進(jìn)程個數(shù): ";
                cin
            >>ProcNum; //進(jìn)程總個數(shù)
                int Num=ProcNum;
                H
            =(Proc)malloc(sizeof(PNode));    
                H
            ->next=NULL;
                Proc p
            =H;
                cout
            <<"總進(jìn)程個數(shù)默認(rèn)為 "<<ProcNum<<" 個,請輸入相應(yīng)信息\n\n";
                
                
            while (Num--) {
                    p
            =p->next=(Proc)malloc(sizeof(PNode));
                    cout
            <<"進(jìn)程名 總運行時間 已運行時間 :";
                    cin
            >>p->name>>p->All_Time>>p->Runed_Time;
                    p
            ->state='R';
                    p
            ->next=NULL;
                }
                p
            ->next=H->next; // 構(gòu)造循環(huán)隊列
            }

            void DispInfo(Proc H) { //輸出運行中信息
                Proc p=H->next;
                
            do {
                    
            if (p->state != 'E')
                    {
                        cout
            <<"進(jìn)程名:"<<p->name<<"\t總運行時間:"<<p->All_Time
                            
            <<"\t已運行時間:"<<p->Runed_Time
                            
            <<"\t狀態(tài):"<<p->state<<endl;
                        p
            =p->next;
                    }
                    
            else p=p->next;
                } 
            while (p != H->next); // 整個進(jìn)程鏈條始終完整,只是狀態(tài)位有差異
            }

            void SJP_Simulator(Proc &H) { // 時間片輪轉(zhuǎn)法模擬器
                cout<<endl<<"-------------------START--------------------\n";
                
            int flag=ProcNum; // 記錄剩余進(jìn)程數(shù)
                int round=0// 記錄輪轉(zhuǎn)數(shù)
                Proc p=H->next;
                
            while (p->All_Time > p->Runed_Time) {
                        round
            ++;
                        cout
            <<endl<<"Round "<<round<<"--正在運行 "<<p->name<<" 進(jìn)程"<<endl;
                        p
            ->Runed_Time++;
                        DispInfo(H);    
                        
            if (p->All_Time == p->Runed_Time) {
                            p
            ->state='E';
                            flag
            --;
                            cout
            <<p->name<<" 進(jìn)程已運行結(jié)束,進(jìn)程被刪除!\n";
                        }
                        p
            =p->next;
                        
            while (flag && p->All_Time == p->Runed_Time)
                            p
            =p->next; // 這一步非常重要,跳過先前已結(jié)束的進(jìn)程

                }
                cout
            <<endl<<"--------------------END---------------------\n";
            }

            void main() {
                Proc H;
                InitPCB(H); 
            // 數(shù)據(jù)初始化
                DispInfo(H); // 初始化成功后的進(jìn)程狀態(tài)
                SJP_Simulator(H); // 模擬時間片輪轉(zhuǎn)法
                system("pause");
            }

             

            /*
            Title: 高響應(yīng)比優(yōu)先算法
            Author: Brian
            Date: 2010/04/11
            */
            #include 
            <iostream>
            #include 
            <cstdlib>
            #include 
            <cstring>
            using namespace std;

            typedef 
            struct PNode {  //PCB
                struct PNode *next; //指向下一個節(jié)點的指針
                char name[12];        //進(jìn)程名
                int All_Time;        //要求運行時間
                int Wait_Time;        //等待時間
                float Res_Ratio;    //響應(yīng)比    
                char state;            //狀態(tài) Ready/End    
            }* Proc; //指向該PCB的指針

            int ProcNum; // 全局變量,用于用戶自己確定進(jìn)程個數(shù)

            void ComputeRes(Proc &H) //計算響應(yīng)比
            {
                Proc p
            =H->next;
                
            while (p) {
                    
            if (p->state == 'R') {
                        p
            ->Wait_Time++;
                        p
            ->Res_Ratio=1+(float)(p->Wait_Time)/p->All_Time;
                    }
                    
            else p->Res_Ratio=0.0;
                    p
            =p->next;
                }
            }

            void InitProc(Proc &H)
            {
                cout
            <<"輸入總進(jìn)程個數(shù): ";
                cin
            >>ProcNum; //進(jìn)程總個數(shù)
                int Num=ProcNum;
                H
            =(Proc)malloc(sizeof(PNode));    
                H
            ->next=NULL;
                Proc p
            =H;
                cout
            <<"總進(jìn)程個數(shù)默認(rèn)為 "<<ProcNum<<" 個,請輸入相應(yīng)信息\n\n";
                
                
            while (Num--) {
                    p
            =p->next=(Proc)malloc(sizeof(PNode));
                    cout
            <<"進(jìn)程名 總運行時間 等待時間 :";
                    cin
            >>p->name>>p->All_Time>>p->Wait_Time;
                    p
            ->state='R';
                    p
            ->Res_Ratio=1+(float)(p->Wait_Time)/p->All_Time;
                    p
            ->next=NULL;
                }
            }

            void DispInfo(Proc H) { //輸出運行中信息
                Proc p=H->next;
                
            while (p) {
                    cout
            <<endl<<"進(jìn)程名:"<<p->name<<"\t總運行時間:"<<p->All_Time
                        
            <<"\t等待時間:"<<p->Wait_Time
                        
            <<"\t響應(yīng)比:"<<p->Res_Ratio<<"\t狀態(tài):"<<p->state<<endl;
                    p
            =p->next;
                }
            }

            void RelocateMax(Proc &H)  // 進(jìn)程排序 (逆序算法) , 首節(jié)點是響應(yīng)比最高節(jié)點
            {
                
            if(H->next==NULL || H->next->next==NULL)
                    
            return// 只剩一個節(jié)點或沒有節(jié)點時無需排序
                Proc p=H->next,q,r;
                
            if (p) {
                    r
            =p->next;
                    p
            ->next=NULL;
                    p
            =r;
                    
            while (p) {
                        r
            =p->next;
                        q
            =H;
                        
            while (q->next && q->next->Res_Ratio < p->Res_Ratio)
                            q
            =q->next;
                        p
            ->next=q->next;
                        q
            ->next=p;
                        p
            =r;
                    }
                }
                p
            =H->next;
                H
            ->next=NULL;
                
            while (p) {
                    q
            =p->next;
                    p
            ->next=H->next;
                    H
            ->next=p;
                    p
            =q;
                }
            }

            void HRN_Simulator(Proc &H) //高響應(yīng)比算法模擬器
            {
                cout
            <<endl<<"-------------------START--------------------\n";    
                
            int flag=ProcNum; // 記錄剩余進(jìn)程數(shù)
                 while (flag)
                {
                    Proc p
            =H->next;
                    p
            ->All_Time--;
                    p
            ->Wait_Time=0;
                    p
            ->Res_Ratio=1.0;
                    
            if (p->All_Time == 0)
                    {
                        p
            ->state = 'E';
                        ComputeRes(H);
                        DispInfo(H);    
                        
            if (p->next == NULL)
                            H
            ->next = NULL;
                        
            else H->next = p->next; //將首節(jié)點刪除    
                        cout<<endl<<p->name<<" 進(jìn)程已運行結(jié)束\n";
                        flag
            --;
                    }
                    
            else 
                    {
                        
                        DispInfo(H);ComputeRes(H);
                    }
                    RelocateMax(H);    
                }
                cout
            <<endl<<"--------------------END---------------------\n\n";
            }

            void main()
            {
                Proc H;
                InitProc(H); 
            // 數(shù)據(jù)初始化
                DispInfo(H); // 初始化成功后的進(jìn)程狀態(tài)
                HRN_Simulator(H); // 模擬高響應(yīng)比優(yōu)先算法
                system("pause");
            }

            posted @ 2010-08-17 12:59 Brian 閱讀(2216) | 評論 (1)編輯 收藏

            一、實驗內(nèi)容

            利用高級語言模擬進(jìn)程的時間片輪轉(zhuǎn)調(diào)度算法,響應(yīng)比高者優(yōu)先調(diào)度算法。

            二、實驗?zāi)康?/span>

            在采用多道程序設(shè)計的系統(tǒng)中,往往有若干個進(jìn)程同時處于就緒狀態(tài)。當(dāng)就緒進(jìn)程個數(shù)大于處理器數(shù)時,就必須依照某種策略來決定哪些進(jìn)程優(yōu)先占用處理器。本實驗?zāi)M在單處理器情況下的處理器調(diào)度,幫助學(xué)生加深了解處理器調(diào)度的工作。

            、實驗環(huán)境

            1PC微機(jī)。

            2Windows 操作系統(tǒng)。

            3C/C++/VB開發(fā)集成環(huán)境。

            四、實驗題目

            本實驗有兩個小題。

            第一題:設(shè)計一個按時間片輪轉(zhuǎn)法實現(xiàn)處理器調(diào)度的程序。

            算法設(shè)計思想:

            (1) 假定系統(tǒng)有五個進(jìn)程,每一個進(jìn)程用一個進(jìn)程控制塊PCB來代表。進(jìn)程控制塊的格式為:

            進(jìn)程名

            指針

            要求運行時間

            已運行時間

            狀態(tài)

            其中,進(jìn)程名——作為進(jìn)程的標(biāo)識,假設(shè)五個進(jìn)程的進(jìn)程名分別為Q1Q2Q3Q4Q5

            指針——進(jìn)程按順序排成循環(huán)隊列,用指針指出下一個進(jìn)程的進(jìn)程控制塊的首地址,最后一個進(jìn)程的指針指出第一個進(jìn)程的進(jìn)程控制塊首地址。

            要求運行時間——假設(shè)進(jìn)程需要運行的單位時間數(shù)。

            已運行時間——假設(shè)進(jìn)程已經(jīng)運行的單位時間數(shù),初始值為“0”。

            狀態(tài)——有兩種狀態(tài),“就緒”和“結(jié)束”,初始狀態(tài)都為“就緒”,用“R”表示。當(dāng)一個進(jìn)程運行結(jié)束后,它的狀態(tài)為“結(jié)束”,用“E”表示。

            (2) 每次運行所設(shè)計的進(jìn)程調(diào)度程序前,為每個進(jìn)程任意確定它的“要求運行時間”。

            (3) 把五個進(jìn)程按順序排成循環(huán)隊列,用指針指出隊列連接情況。另用一標(biāo)志單元記錄輪到運行的進(jìn)程。例如,當(dāng)前輪到P2執(zhí)行,則有:

            標(biāo)志單元

                     K2   

            K1

            Q1

             K2

            Q2

             K3

            Q3

             K4

            Q4

             K5

            Q5

             

            K2

             

            K3

             

            K4

             

            K5

             

            K1

             

            2

             

            3

             

            1

             

            2

             

            4

             

            1

             

            0

             

            0

             

            0

             

            0

             

            R

             

            R

             

            R

             

            R

             

            R

             

            PCB1

             

            PCB2

             

            PCB3

             

            PCB4

             

            PCB5

             

            (4) 處理器調(diào)度總是選擇標(biāo)志單元指示的進(jìn)程運行。由于本實驗是模擬處理器調(diào)度的功能,所以,對被選中的進(jìn)程并不實際的啟動運行,而是執(zhí)行:

            已運行時間+1

            來模擬進(jìn)程的一次運行,表示進(jìn)程已經(jīng)運行過一個單位的時間。

            請注意:在實際的系統(tǒng)中,當(dāng)一個進(jìn)程被選中運行時,必須置上該進(jìn)程可以運行的時間片值,以及恢復(fù)進(jìn)程的現(xiàn)場,讓它占有處理器運行,直到出現(xiàn)等待事件或運行滿一個時間片。在這時省去了這些工作,僅用“已運行時間+1”來表示進(jìn)程已經(jīng)運行滿一個時間片。

            (5) 進(jìn)程運行一次后,應(yīng)把該進(jìn)程的進(jìn)程控制塊中的指針值送到標(biāo)志單元,以指示下一個輪到運行的進(jìn)程。同時,應(yīng)判斷該進(jìn)程的要求運行時間與已運行時間,若該進(jìn)程的要求運行時間?已運行時間,則表示它尚未執(zhí)行結(jié)束,應(yīng)待到下一輪時再運行。若該進(jìn)程的要求運行時間=已運行時間,則表示它已經(jīng)執(zhí)行結(jié)束,應(yīng)指導(dǎo)它的狀態(tài)修改成“結(jié)束”(E)且退出隊列。此時,應(yīng)把該進(jìn)程的進(jìn)程控制塊中的指針值送到前面一個進(jìn)程的指針位置。

            (6) 若“就緒”狀態(tài)的進(jìn)程隊列不為空,則重復(fù)上面的(4)和(5)的步驟,直到所有的進(jìn)程都成為“結(jié)束”狀態(tài)。

            (7) 在所設(shè)計的程序中應(yīng)有顯示或打印語句,能顯示或打印每次選中進(jìn)程的進(jìn)程名以及運行一次后進(jìn)程隊列的變化。

            (8) 為五個進(jìn)程任意確定一組“要求運行時間”,啟動所設(shè)計的處理器調(diào)度程序,顯示或打印逐次被選中的進(jìn)程名以及進(jìn)程控制塊的動態(tài)變化過程。

             

            第二題:設(shè)計一個按響應(yīng)比高者優(yōu)先調(diào)度算法實現(xiàn)進(jìn)程調(diào)度的程序。

            算法設(shè)計思想:

             (1) 假定系統(tǒng)有五個進(jìn)程,每一個進(jìn)程用一個進(jìn)程控制塊PCB來代表,進(jìn)程控制塊的格式為:

            進(jìn)程名

            指針

            要求運行時間

            等待時間

            響應(yīng)比

            狀態(tài)

            其中,進(jìn)程名——作為進(jìn)程的標(biāo)識,假設(shè)五個進(jìn)程的進(jìn)程名分別為P1P2P3P4P5

            指針——按優(yōu)先數(shù)的大小把五個進(jìn)程連成隊列,用指針指出下一個進(jìn)程的進(jìn)程控制塊的首地址,最后一個進(jìn)程中的指針為“0”。

            要求運行時間——假設(shè)進(jìn)程需要運行的單位時間數(shù)。

            等待時間——自最近一次調(diào)度運行至今等待的時間數(shù),當(dāng)進(jìn)程被調(diào)度時等待時間清零。

            響應(yīng)比——進(jìn)程調(diào)度程序運行前計算每個進(jìn)程的響應(yīng)比,調(diào)度時總是選取響應(yīng)比大的進(jìn)程先執(zhí)行,每次執(zhí)行一個固定的時間片。

            狀態(tài)——可假設(shè)有兩種狀態(tài),“就緒”狀態(tài)和“結(jié)束”狀態(tài)。五個進(jìn)程的初始狀態(tài)都為“就緒”,用“R”表示,當(dāng)一個進(jìn)程運行結(jié)束后,它的狀態(tài)為“結(jié)束”,用“E”表示。

            (2) 在每次運行你所設(shè)計的處理器調(diào)度程序之前,為每個進(jìn)程任意確定它的“等待時間”和“要求運行時間”。

            (3) 為了調(diào)度方便,把五個進(jìn)程按給定的響應(yīng)比從大到小連成隊列。用一單元指出隊首進(jìn)程,用指針指出隊列的連接情況。例:

              隊首標(biāo)志

                     K2   

            K1

            P1

             K2

            P2

             K3

            P3

             K4

            P4

             K5

            P5

             

            0

             

            K4

             

            K5

             

            K3

             

            K1

             

            2

             

            3

             

            1

             

            2

             

            4

             

            1

             

            5

             

            3

             

            4

             

            2

             

            R

             

            R

             

            R

             

            R

             

            R

             

            PCB1

             

            PCB2

             

            PCB3

             

            PCB4

             

            PCB5

             

            (4) 處理器調(diào)度總是選隊首進(jìn)程運行。采用動態(tài)改變響應(yīng)比的辦法,進(jìn)程每運行一次重新計算各進(jìn)程的響應(yīng)比。由于本實驗是模擬處理器調(diào)度,所以,對被選中的進(jìn)程并不實際的啟動運行,而是執(zhí)行:要求運行時間-1、等待時間為0。其它進(jìn)程等待時間+1,重新計算各進(jìn)程的響應(yīng)比,并從大到小排序。

            提醒注意的是:在實際的系統(tǒng)中,當(dāng)一個進(jìn)程被選中運行時,必須恢復(fù)進(jìn)程的現(xiàn)場,讓它占有處理器運行,直到出現(xiàn)等待事件或運行結(jié)束。在這里省去了這些工作。

            (5) 進(jìn)程運行一次后,若要求運行時間?0,則再將它加入隊尾(因其響應(yīng)比最小。);若要求運行時間=0,則把它的狀態(tài)修改成“結(jié)束”(E),且退出隊列。

            (6) 若“就緒”狀態(tài)的進(jìn)程隊列不為空,則重復(fù)上面(4)和(5)的步驟,直到所有進(jìn)程都成為“結(jié)束”狀態(tài)。

            (7) 在所設(shè)計的程序中應(yīng)有顯示或打印語句,能顯示或打印每次被選中進(jìn)程的進(jìn)程名以及運行一次后進(jìn)程隊列的變化及各進(jìn)程的參數(shù)。

            (8) 為五個進(jìn)程任意確定一組“等待時間”和“要求運行時間”,啟動所設(shè)計的進(jìn)程調(diào)度程序,顯示或打印逐次被選中進(jìn)程的進(jìn)程名以及進(jìn)程控制塊的動態(tài)變化過程。

            posted @ 2010-08-17 12:57 Brian 閱讀(1912) | 評論 (0)編輯 收藏

            本學(xué)期學(xué)習(xí)了操作系統(tǒng),實驗課共有三個大實驗,共有五個題目:時間片輪轉(zhuǎn)法、高響應(yīng)比優(yōu)先、首次適應(yīng)算法、位示圖法、文件管理。每部分都會附上實驗指導(dǎo)書和代碼。每個實驗都有點問題,請大家指教。

            還有就是要提醒一下找到這里的學(xué)弟學(xué)妹,火哥可是知道我寫代碼的風(fēng)格的,這些代碼他也都看過,如果要用在實驗作業(yè)上,請你們?nèi)肌?/p>

            如果你們原封不動的復(fù)制粘貼,那就太令我失望了,師大的學(xué)風(fēng)要上來啊。

            posted @ 2010-08-17 12:56 Brian 閱讀(1060) | 評論 (0)編輯 收藏

            僅列出標(biāo)題
            共4頁: 1 2 3 4 
            99久久免费国产特黄| 国产精品9999久久久久| 久久久久一级精品亚洲国产成人综合AV区| 国产成人久久精品一区二区三区| 久久99中文字幕久久| 久久青青草原精品国产不卡| 国内精品久久久久久久久电影网| 中文精品久久久久人妻不卡| 久久久中文字幕| 久久国产亚洲精品| 国产精品免费福利久久| 久久精品夜色噜噜亚洲A∨| 亚洲精品蜜桃久久久久久| 色成年激情久久综合| 国产美女亚洲精品久久久综合| 久久亚洲精品成人AV| 最新久久免费视频| 一本大道加勒比久久综合| 久久久久久精品免费看SSS| 一本大道加勒比久久综合| 精品国产乱码久久久久久人妻| 久久精品国产秦先生| 久久ww精品w免费人成| 久久久久波多野结衣高潮| 久久久久久久国产免费看| 国产美女久久久| 99久久人妻无码精品系列| 久久国产劲爆AV内射—百度| 久久精品夜色噜噜亚洲A∨| 夜夜亚洲天天久久| 热99re久久国超精品首页| 久久精品国产亚洲av日韩| 东方aⅴ免费观看久久av| 欧美精品九九99久久在观看| 久久精品国产欧美日韩| 99久久精品国产综合一区| 欧美激情精品久久久久| AV狠狠色丁香婷婷综合久久| 国内精品久久久久影院免费| 国产欧美一区二区久久| 91精品国产高清久久久久久国产嫩草|