• <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 到底是個什么數,總之是兩個質數的乘積就對了。枚舉的范圍: 2 ~ 32000 (104.5 )
            我的思路是: 首先把2~32000之間的所有素數都存放在一個數組里,然后當你輸入一個數據時,先讓其逐一模除這個數組里的素數,一旦模除結果為0,則計算出他們的商,再判斷商是否也為素數。注意數據是兩個數的平方的處理

            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) //判斷是否為素數,非常精確而高效
            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 閱讀(323) | 評論 (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的大數。
            此舉確實流氓了一點,不過確實過了,鄙人的第一題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》里關于 大數的簡單介紹,講的算比較清晰的了:
            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 閱讀(573) | 評論 (0)編輯 收藏

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

            posted @ 2010-08-17 13:17 Brian 閱讀(1188) | 評論 (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 閱讀(1872) | 評論 (5)編輯 收藏

            一、實驗內容

            利用高級語言,設計一個采用二級或二級以上的多級文件目錄管理系統,實現對文件的基本操作,如:文件的建立、打開、關閉、復制、撤銷、檢索等。

            二、實驗目的

            用高級語言編寫和調試一個簡單的文件系統,模擬文件管理的工作過程。從而對各種文件操作命令的實質內容和執行過程有比較深入的了解。

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

            文件必須設置保護措施,且至少有Createdeleteopenclosereadwrite等命令。

            、實驗環境

            1PC微機。

            2Windows 操作系統。

            3C/C++/VB開發集成環境。

            四、實驗題目

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

            算法設計思想:

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

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

            (3)       程序中使用的主要設計結構如下:

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

            M D F

            U F D

            A F D

            用戶名

            文件名

            打開文件名

            文件目錄指針

            保護碼

            打開保護碼

            用戶名

            文件長度

            讀寫指針

            文件目錄指針

            文件名

             

             

            ·

            ·

            ·

             

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

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

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

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

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

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

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

            int ProcNum; // 全局變量,用于用戶自己確定進程個數

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

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

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

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

            void main() {
                Proc H;
                InitPCB(H); 
            // 數據初始化
                DispInfo(H); // 初始化成功后的進程狀態
                SJP_Simulator(H); // 模擬時間片輪轉法
                system("pause");
            }

             

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

            typedef 
            struct PNode {  //PCB
                struct PNode *next; //指向下一個節點的指針
                char name[12];        //進程名
                int All_Time;        //要求運行時間
                int Wait_Time;        //等待時間
                float Res_Ratio;    //響應比    
                char state;            //狀態 Ready/End    
            }* Proc; //指向該PCB的指針

            int ProcNum; // 全局變量,用于用戶自己確定進程個數

            void ComputeRes(Proc &H) //計算響應比
            {
                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
            <<"輸入總進程個數: ";
                cin
            >>ProcNum; //進程總個數
                int Num=ProcNum;
                H
            =(Proc)malloc(sizeof(PNode));    
                H
            ->next=NULL;
                Proc p
            =H;
                cout
            <<"總進程個數默認為 "<<ProcNum<<" 個,請輸入相應信息\n\n";
                
                
            while (Num--) {
                    p
            =p->next=(Proc)malloc(sizeof(PNode));
                    cout
            <<"進程名 總運行時間 等待時間 :";
                    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<<"進程名:"<<p->name<<"\t總運行時間:"<<p->All_Time
                        
            <<"\t等待時間:"<<p->Wait_Time
                        
            <<"\t響應比:"<<p->Res_Ratio<<"\t狀態:"<<p->state<<endl;
                    p
            =p->next;
                }
            }

            void RelocateMax(Proc &H)  // 進程排序 (逆序算法) , 首節點是響應比最高節點
            {
                
            if(H->next==NULL || H->next->next==NULL)
                    
            return// 只剩一個節點或沒有節點時無需排序
                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) //高響應比算法模擬器
            {
                cout
            <<endl<<"-------------------START--------------------\n";    
                
            int flag=ProcNum; // 記錄剩余進程數
                 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; //將首節點刪除    
                        cout<<endl<<p->name<<" 進程已運行結束\n";
                        flag
            --;
                    }
                    
            else 
                    {
                        
                        DispInfo(H);ComputeRes(H);
                    }
                    RelocateMax(H);    
                }
                cout
            <<endl<<"--------------------END---------------------\n\n";
            }

            void main()
            {
                Proc H;
                InitProc(H); 
            // 數據初始化
                DispInfo(H); // 初始化成功后的進程狀態
                HRN_Simulator(H); // 模擬高響應比優先算法
                system("pause");
            }

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

            一、實驗內容

            利用高級語言模擬進程的時間片輪轉調度算法,響應比高者優先調度算法。

            二、實驗目的

            在采用多道程序設計的系統中,往往有若干個進程同時處于就緒狀態。當就緒進程個數大于處理器數時,就必須依照某種策略來決定哪些進程優先占用處理器。本實驗模擬在單處理器情況下的處理器調度,幫助學生加深了解處理器調度的工作。

            、實驗環境

            1PC微機。

            2Windows 操作系統。

            3C/C++/VB開發集成環境。

            四、實驗題目

            本實驗有兩個小題。

            第一題:設計一個按時間片輪轉法實現處理器調度的程序。

            算法設計思想:

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

            進程名

            指針

            要求運行時間

            已運行時間

            狀態

            其中,進程名——作為進程的標識,假設五個進程的進程名分別為Q1Q2Q3Q4Q5

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

            要求運行時間——假設進程需要運行的單位時間數。

            已運行時間——假設進程已經運行的單位時間數,初始值為“0”。

            狀態——有兩種狀態,“就緒”和“結束”,初始狀態都為“就緒”,用“R”表示。當一個進程運行結束后,它的狀態為“結束”,用“E”表示。

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

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

            標志單元

                     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) 處理器調度總是選擇標志單元指示的進程運行。由于本實驗是模擬處理器調度的功能,所以,對被選中的進程并不實際的啟動運行,而是執行:

            已運行時間+1

            來模擬進程的一次運行,表示進程已經運行過一個單位的時間。

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

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

            (6) 若“就緒”狀態的進程隊列不為空,則重復上面的(4)和(5)的步驟,直到所有的進程都成為“結束”狀態。

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

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

             

            第二題:設計一個按響應比高者優先調度算法實現進程調度的程序。

            算法設計思想:

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

            進程名

            指針

            要求運行時間

            等待時間

            響應比

            狀態

            其中,進程名——作為進程的標識,假設五個進程的進程名分別為P1P2P3P4P5

            指針——按優先數的大小把五個進程連成隊列,用指針指出下一個進程的進程控制塊的首地址,最后一個進程中的指針為“0”。

            要求運行時間——假設進程需要運行的單位時間數。

            等待時間——自最近一次調度運行至今等待的時間數,當進程被調度時等待時間清零。

            響應比——進程調度程序運行前計算每個進程的響應比,調度時總是選取響應比大的進程先執行,每次執行一個固定的時間片。

            狀態——可假設有兩種狀態,“就緒”狀態和“結束”狀態。五個進程的初始狀態都為“就緒”,用“R”表示,當一個進程運行結束后,它的狀態為“結束”,用“E”表示。

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

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

              隊首標志

                     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) 處理器調度總是選隊首進程運行。采用動態改變響應比的辦法,進程每運行一次重新計算各進程的響應比。由于本實驗是模擬處理器調度,所以,對被選中的進程并不實際的啟動運行,而是執行:要求運行時間-1、等待時間為0。其它進程等待時間+1,重新計算各進程的響應比,并從大到小排序。

            提醒注意的是:在實際的系統中,當一個進程被選中運行時,必須恢復進程的現場,讓它占有處理器運行,直到出現等待事件或運行結束。在這里省去了這些工作。

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

            (6) 若“就緒”狀態的進程隊列不為空,則重復上面(4)和(5)的步驟,直到所有進程都成為“結束”狀態。

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

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

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

            本學期學習了操作系統,實驗課共有三個大實驗,共有五個題目:時間片輪轉法、高響應比優先、首次適應算法、位示圖法、文件管理。每部分都會附上實驗指導書和代碼。每個實驗都有點問題,請大家指教。

            還有就是要提醒一下找到這里的學弟學妹,火哥可是知道我寫代碼的風格的,這些代碼他也都看過,如果要用在實驗作業上,請你們三思。

            如果你們原封不動的復制粘貼,那就太令我失望了,師大的學風要上來啊。

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

            僅列出標題
            共4頁: 1 2 3 4 
            香蕉久久夜色精品升级完成| 久久久久国产成人精品亚洲午夜| 久久亚洲视频| 久久精品成人免费观看97| 国产欧美久久一区二区| 久久国产亚洲精品麻豆| 精品久久久久久综合日本| 久久精品草草草| 品成人欧美大片久久国产欧美| 伊人久久综在合线亚洲2019| 久久99精品久久久久久水蜜桃 | 亚洲αv久久久噜噜噜噜噜| 综合久久给合久久狠狠狠97色| 狠狠色丁香久久婷婷综合蜜芽五月| 亚洲国产日韩欧美久久| 亚洲精品乱码久久久久久蜜桃不卡 | 久久国产一区二区| 国产精品免费久久久久影院| 精品久久久久久久久久久久久久久 | 国产精品免费看久久久香蕉| 亚洲国产天堂久久综合网站 | 狠狠精品久久久无码中文字幕| 欧美亚洲国产精品久久| 久久久久无码精品国产不卡| 久久精品国产亚洲一区二区| 午夜视频久久久久一区| 国内精品久久久久影院日本 | 久久亚洲国产欧洲精品一| 久久久久久久综合日本| 日韩AV无码久久一区二区| 国产免费久久久久久无码| 久久久国产亚洲精品| 久久亚洲欧美日本精品| 久久国产欧美日韩精品| 国产成人综合久久精品尤物| 亚洲人成网亚洲欧洲无码久久| 成人亚洲欧美久久久久| 午夜精品久久久久久99热| 亚洲欧美国产精品专区久久| 93精91精品国产综合久久香蕉| 亚洲国产精品无码久久久蜜芽|