青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

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 閱讀(336) | 評論 (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 閱讀(588) | 評論 (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 閱讀(1205) | 評論 (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 閱讀(1895) | 評論 (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 閱讀(1195) | 評論 (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 閱讀(1609) | 評論 (0)編輯 收藏

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

posted @ 2010-08-17 13:00 Brian 閱讀(2220) | 評論 (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 閱讀(2229) | 評論 (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 閱讀(1925) | 評論 (0)編輯 收藏

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

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

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

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

僅列出標題
共4頁: 1 2 3 4 
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲综合色激情五月| 亚洲二区在线| 99这里只有精品| 欧美成人高清视频| 一本色道精品久久一区二区三区| 蜜臀av国产精品久久久久| 国产精品蜜臀在线观看| 亚洲欧美另类在线| 亚洲欧美日韩成人高清在线一区| 国产毛片一区| 久久先锋资源| 欧美大片免费看| 一区二区三区欧美| 在线亚洲欧美专区二区| 国产九区一区在线| 久久久久久高潮国产精品视| 欧美在线黄色| 亚洲国产人成综合网站| 亚洲区一区二| 国产精品久久久久久久久久久久久| 亚洲午夜国产成人av电影男同| 一本久久精品一区二区| 国产欧美精品va在线观看| 久久人人爽国产| 欧美激情网站在线观看| 亚洲一区二区三区乱码aⅴ| 亚洲欧美日本另类| 中文日韩欧美| 国产欧美日韩精品丝袜高跟鞋| 久久久999国产| 欧美激情第三页| 亚洲欧美色一区| 久久影音先锋| 亚洲欧美成人在线| 另类亚洲自拍| 亚洲欧美一区二区激情| 久久精品中文字幕一区| 亚洲午夜久久久| 久久精品久久99精品久久| 亚洲福利电影| 在线视频精品一区| 国产精品久久国产精麻豆99网站| 午夜欧美视频| 久久xxxx精品视频| 亚洲五月婷婷| 麻豆亚洲精品| 久久久久久亚洲精品不卡4k岛国| 欧美激情影院| 美女成人午夜| 国产欧美精品一区二区色综合 | 永久免费精品影视网站| 99精品免费| 国产亚洲精品一区二555| 亚洲伦理一区| 国产日韩欧美麻豆| 亚洲成人在线视频播放| 国产情侣久久| 中日韩美女免费视频网址在线观看| 在线精品亚洲| 久久精品国产欧美亚洲人人爽 | 欧美精品aa| 欧美激情1区2区| 黄色一区三区| 亚洲欧洲av一区二区| 亚洲图片你懂的| 欧美成人午夜激情| 欧美国产视频在线| 亚洲电影一级黄| 久久久久久久久久看片| 久久婷婷国产综合尤物精品| 国产色产综合色产在线视频| 99综合精品| 亚洲一区二区三区精品在线观看 | 久久亚洲欧美| 国产亚洲一区二区精品| 欧美一区二区三区精品| 欧美怡红院视频| 国产亚洲成av人在线观看导航 | 亚洲国产高清一区| 亚洲精品国产品国语在线app| 蜜桃精品久久久久久久免费影院| 久久综合色影院| 国产亚洲制服色| 久久黄色网页| 欧美中文字幕在线| 国内成人精品2018免费看| 久久久久高清| 亚洲第一偷拍| 宅男噜噜噜66国产日韩在线观看| 欧美日韩国产色站一区二区三区| 亚洲免费精彩视频| 亚洲欧美国产视频| 国产偷国产偷亚洲高清97cao| 久久九九免费视频| 亚洲国产高清aⅴ视频| 中文一区字幕| 国产视频精品va久久久久久| 久久免费少妇高潮久久精品99| 欧美高清hd18日本| 99天天综合性| 欧美精品国产| 欧美一区网站| 亚洲国产精品t66y| 午夜精品免费| 一区二区在线视频播放| 欧美日本一道本在线视频| 在线亚洲成人| 免费成人av在线| 亚洲先锋成人| 在线电影国产精品| 欧美三级免费| 欧美在线观看视频一区二区| 亚洲国产精品久久久久久女王| 亚洲一区一卡| 亚洲福利视频专区| 国产精品视频xxxx| 欧美国产日韩一区| 午夜欧美不卡精品aaaaa| 久久欧美肥婆一二区| av成人免费在线| 很黄很黄激情成人| 国产精品videosex极品| 免费高清在线一区| 欧美一区二区三区的| 夜夜嗨av一区二区三区四季av| 久久亚洲综合网| 亚洲欧美日本伦理| 亚洲国产精品va在看黑人| 国产精品视频久久| 欧美精品一区二区三区一线天视频 | 麻豆成人小视频| 午夜日韩av| 日韩午夜视频在线观看| 欧美一区二区三区啪啪| 亚洲天堂成人在线视频| 亚洲欧洲在线视频| 经典三级久久| 国产日韩欧美制服另类| 国产精品白丝av嫩草影院| 欧美国产日韩在线| 免费久久99精品国产自在现线| 欧美一区二区啪啪| 午夜精品理论片| 亚洲欧美在线一区二区| 中文日韩在线| 在线亚洲自拍| av成人福利| avtt综合网| 亚洲高清自拍| 欧美国产成人在线| 欧美阿v一级看视频| 久久嫩草精品久久久久| 久久久999成人| 久久久久综合网| 久久久久欧美| 久久青草福利网站| 麻豆av福利av久久av| 免费欧美日韩国产三级电影| 裸体歌舞表演一区二区| 欧美高清在线精品一区| 欧美在线视频一区| 亚洲综合第一页| 久久综合色8888| 久久综合色婷婷| 欧美手机在线| 极品中文字幕一区| 正在播放欧美一区| 久久精品亚洲一区二区三区浴池| 欧美国产成人在线| 亚洲午夜伦理| 嫩草国产精品入口| 国产欧美成人| 亚洲美洲欧洲综合国产一区| 香蕉av福利精品导航| 欧美激情在线狂野欧美精品| 一区二区三区欧美在线| 久久免费一区| 国产精品一级二级三级| 亚洲精选一区| 久久久蜜桃一区二区人| 亚洲美女中文字幕| 久久精品国产999大香线蕉| 欧美日韩国产精品自在自线| 国产亚洲精品aa| 一本色道久久88亚洲综合88| 久久久噜噜噜久久| 制服丝袜激情欧洲亚洲| 欧美成人午夜| 黄色精品一区| 欧美一级黄色网| 99热免费精品| 欧美不卡视频一区| 国产综合色一区二区三区| 亚洲图片在线| 最近中文字幕日韩精品| 久久久久久久综合日本| 国产欧美精品日韩精品| 亚洲一区在线观看免费观看电影高清| 欧美高清视频一二三区| 久久久久久国产精品mv|