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 阅读(306) | 评论 (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 阅读(555) | 评论 (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 阅读(1158) | 评论 (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 阅读(1829) | 评论 (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 阅读(1100) | 评论 (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 阅读(1556) | 评论 (0)编辑 收藏

     摘要: 一、实验内容 利用高级语言,实现存储分配算法,开发一个存储管理的模拟程序,对内存空间的管理和分配。内存空间的管理可采用固定分区管理方式,可变分区管理方式,页式存储管理,段式存储管理等方案。 二、实验目的 一个好的计算机系统不仅要有一个足够容量的、存取速度高的、稳定可靠的主存储器,而且要能合理地分配和使用这些存储空间。当用户提出申请存储器空间时,存储管理必须根据申请者的要求,按一定的策略分析主...  阅读全文

posted @ 2010-08-17 13:00 Brian 阅读(2152) | 评论 (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 阅读(2178) | 评论 (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 阅读(1864) | 评论 (0)编辑 收藏

本学期学习了操作系统,实验课共有三个大实验,共有五个题目:时间片轮转法、高响应比优先、首次适应算法、位示图法、文件管理。每部分都会附上实验指导书和代码。每个实验都有点问题,请大家指教。

还有就是要提醒一下找到这里的学弟学妹,火哥可是知道我写代码的风格的,这些代码他也都看过,如果要用在实验作业上,请你们三思。

如果你们原封不动的复制粘贴,那就太令我失望了,师大的学风要上来啊。

posted @ 2010-08-17 12:56 Brian 阅读(1044) | 评论 (0)编辑 收藏

仅列出标题
共4页: 1 2 3 4