You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
514 lines
10 KiB
514 lines
10 KiB
11 months ago
|
实验1.1半数集问题
|
||
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
int a[1005]={0}; //存放已计算的数据
|
||
|
int b[1005]={0};
|
||
|
int halfset(int n){
|
||
|
int sum = 1;
|
||
|
if(a[n]>0) //如果f(n)已经得出,就不必再重复计算
|
||
|
return a[n];
|
||
|
for(int i=1;i<=n/2;i++){
|
||
|
sum += halfset(i);
|
||
|
}
|
||
|
a[n]=sum;
|
||
|
return sum;
|
||
|
}
|
||
|
int main()
|
||
|
{
|
||
|
int n = 0,i = 0;
|
||
|
cin>>n;
|
||
|
while(n!=0){
|
||
|
b[i++] = n;
|
||
|
cin>>n;
|
||
|
}
|
||
|
for(int j=0;j<i;j++){
|
||
|
int result=halfset(b[j]);
|
||
|
cout << result <<endl;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
实验1.2双色hanoi塔问题
|
||
|
#include <bits/stdc++.h>
|
||
|
#include<cstdio>
|
||
|
#include<cstring>
|
||
|
void mov(int n,char a,char b)
|
||
|
{
|
||
|
printf("%d %c %c\n",n,a,b);
|
||
|
}
|
||
|
void dg(int n,char a, char c, char b)
|
||
|
{
|
||
|
if(n==1) mov(n,a,b);
|
||
|
else
|
||
|
{
|
||
|
dg(n-1,a,b,c);
|
||
|
mov(n,a,b);
|
||
|
dg(n-1,c,a,b);
|
||
|
}
|
||
|
}
|
||
|
int main()
|
||
|
{
|
||
|
char a='A',b='B',c='C';
|
||
|
int n;
|
||
|
while(scanf("%d",&n)!=EOF)
|
||
|
dg(n,a,c,b);
|
||
|
return 0;
|
||
|
}
|
||
|
实验2.1我要去西藏
|
||
|
#include<bits/stdc++.h>
|
||
|
#include<algorithm>
|
||
|
using namespace std;
|
||
|
int a[200][200];
|
||
|
int cost[200];
|
||
|
int city,result;
|
||
|
int travel(){
|
||
|
cost[city-2] = a[city-2][city-1];
|
||
|
for(int i = city-3;i>=0;i--){
|
||
|
result = a[i][city-1];
|
||
|
for(int j = i+1;j<city-1;j++){
|
||
|
result = min(a[i][j]+cost[j],result);
|
||
|
}
|
||
|
cost[i] = result;
|
||
|
}
|
||
|
return cost[0];
|
||
|
}
|
||
|
int main(){
|
||
|
cin>>city;
|
||
|
for(int i = 0;i<city-1;i++){
|
||
|
for(int j = i+1;j<city;j++){
|
||
|
cin>>a[i][j];
|
||
|
}
|
||
|
}
|
||
|
result = travel();
|
||
|
cout<<result;
|
||
|
return 0;
|
||
|
}
|
||
|
实验2.2云天明项链
|
||
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
const int N = 1e3 + 10;
|
||
|
#define Ma_x 99999
|
||
|
#define Mi_x 0
|
||
|
#define min(a,b) a<b?a:b
|
||
|
#define max(a,b) a>b?a:b
|
||
|
int n,w[N],dp[N][N],dq[N][N];
|
||
|
int sum(int i,int t){
|
||
|
int k,s=0,k1;
|
||
|
for(k=i;k<i+t;k++){
|
||
|
k1=k%n;
|
||
|
if(k1==0) k1=n;
|
||
|
s=s+w[k1];
|
||
|
}
|
||
|
return s;
|
||
|
}
|
||
|
int main(){
|
||
|
int i,t,k;
|
||
|
scanf("%d",&n);
|
||
|
for(i=1;i<=n;i++){
|
||
|
scanf("%d",&w[i]);
|
||
|
dp[i][1]=0;
|
||
|
}
|
||
|
for(t=2;t<=n;t++){
|
||
|
for(i=1;i<=n;i++){
|
||
|
dp[i][t]=Ma_x;
|
||
|
dq[i][t]=Mi_x;
|
||
|
for(k=1;k<t;k++){
|
||
|
dp[i][t]=min(dp[i][t],dp[i][k]+dp[(i+k-1)%n+1][t-k]+sum(i,t)); dq[i][t]=max(dq[i][t],dq[i][k]+dq[(i+k-1)%n+1][t-k]+sum(i,t));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
int mini=Ma_x;
|
||
|
int maxi=Mi_x;
|
||
|
for(i=1;i<=n;i++){
|
||
|
mini=min(mini,dp[i][n]);
|
||
|
maxi=max(maxi,dq[i][n]);
|
||
|
}
|
||
|
printf("%d\n",mini);
|
||
|
printf("%d ",maxi);
|
||
|
}
|
||
|
实验2.3扩展距离
|
||
|
#include <bits/stdc++.h>
|
||
|
#include<algorithm>
|
||
|
using namespace std;
|
||
|
string a,b;
|
||
|
int k;
|
||
|
int value[10000][10000];
|
||
|
int main(){
|
||
|
cin>>a>>b>>k;
|
||
|
int alen=a.size();
|
||
|
int blen=b.size();
|
||
|
for(int i=0;i<=alen;i++){
|
||
|
value[i][0]=k*i;
|
||
|
}
|
||
|
for(int i=1;i<=blen;i++){
|
||
|
value[0][i]=k*i;
|
||
|
}
|
||
|
for(int i=1;i<=alen;i++){
|
||
|
for(int j=1;j<=blen;j++){
|
||
|
value[i][j]=min(min(value[i-1][j]+k,value[i][j-1]+k),value[i-1][j-1]+abs(a[i-1]-b[j-1]));
|
||
|
}
|
||
|
}
|
||
|
cout<<value[alen][blen];
|
||
|
}
|
||
|
实验2.4双胞胎探宝
|
||
|
#include<bits/stdc++.h>
|
||
|
#include<iostream>
|
||
|
using namespace std;
|
||
|
int n;
|
||
|
int m[15][15];
|
||
|
int f[25][15][15];
|
||
|
int main() {
|
||
|
cin >> n;
|
||
|
int x, y, z;
|
||
|
while (cin >> x >> y >> z) {
|
||
|
if (!x && !y && !z) break;
|
||
|
m[x][y] = z;
|
||
|
}
|
||
|
for (int k = 2; k <= 2 * n; k ++ )
|
||
|
for (int i = 1; i <= n; i ++ )
|
||
|
for (int j = 1; j <= n; j ++ ) {
|
||
|
int a = k - i, b = k - j;
|
||
|
if (a >= 1 && a <= n && b >= 1 && b <= n) {
|
||
|
int t = max(max(f[k - 1][i - 1][j], f[k - 1][i][j]), max(f[k - 1][i][j - 1], f[k - 1][i - 1][j - 1]));
|
||
|
if (i == j) f[k][i][j] = t + m[i][a];
|
||
|
else f[k][i][j] = t + m[i][a] + m[j][b];
|
||
|
}
|
||
|
}
|
||
|
cout << f[2 * n][n][n];
|
||
|
return 0;
|
||
|
}
|
||
|
实验2.5双胞胎做题
|
||
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
int f[1000][1000];
|
||
|
int a[10000],b[10000];
|
||
|
int n,sum,result=9999;
|
||
|
|
||
|
void resulta(){
|
||
|
memset(f, 0, sizeof(f));
|
||
|
for(int i=1;i<=n;i++){
|
||
|
|
||
|
for(int j=0;j<=sum;j++){
|
||
|
if(j<a[i]){
|
||
|
f[i][j]=f[i-1][j]+b[i];
|
||
|
}
|
||
|
else{
|
||
|
f[i][j]=min(f[i-1][j-a[i]],f[i-1][j]+b[i]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
for(int j=0;j<=sum;j++)
|
||
|
{
|
||
|
int t=max(f[n][j],j);
|
||
|
if(result>t)
|
||
|
result=t;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main(){
|
||
|
cin>>n;
|
||
|
for(int i=1;i<=n;i++){
|
||
|
cin>>a[i];
|
||
|
sum=sum+a[i];
|
||
|
}
|
||
|
for(int i=1;i<=n;i++){
|
||
|
cin>>b[i];
|
||
|
}
|
||
|
resulta();
|
||
|
cout<<result;
|
||
|
}
|
||
|
实验2.6少打比赛多训练
|
||
|
#include <bits/stdc++.h>
|
||
|
|
||
|
using namespace std;
|
||
|
typedef long long ll;
|
||
|
ll dp[100002],n,m,s[100002]={0};
|
||
|
map<ll,int>T;
|
||
|
struct node{
|
||
|
ll u;ll t;
|
||
|
}a[100002];
|
||
|
int main()
|
||
|
{
|
||
|
cin>>n>>m;
|
||
|
for(int i=0;i<m;i++){
|
||
|
scanf("%lld%lld",&a[i].u,&a[i].t);
|
||
|
T[a[i].u*100000+(s[a[i].u]++)]=a[i].t;//记录以a[i].u开始的每一个维持时间
|
||
|
}
|
||
|
memset(dp,0x3f,sizeof(dp));
|
||
|
dp[n+1]=0;
|
||
|
for(int i=n;i>=1;i--){
|
||
|
if(s[i]==0){dp[i]=dp[i+1];}
|
||
|
else{
|
||
|
for(int j=0;j<s[i];j++)
|
||
|
dp[i]=min(dp[i+T[i*100000+j]]+T[i*100000+j],dp[i]);
|
||
|
}
|
||
|
}
|
||
|
cout<<dp[1]<<endl;
|
||
|
return 0;
|
||
|
}
|
||
|
实验3.1教室安排问题
|
||
|
#include<bits/stdc++.h>
|
||
|
#include <iostream>
|
||
|
#include <algorithm>
|
||
|
|
||
|
using namespace std;
|
||
|
struct huichang
|
||
|
{
|
||
|
int s;
|
||
|
int f;
|
||
|
};
|
||
|
huichang t[10000+10];
|
||
|
int n;
|
||
|
bool cmp(huichang a, huichang b) {
|
||
|
return a.s < b.s;
|
||
|
}
|
||
|
int meeting() {
|
||
|
int count_chang = 0;
|
||
|
int *a = new int[n + 1]; //该数组于保存每个会场的结束时间
|
||
|
for (int i = 0; i < n+1; i++) {
|
||
|
a[i] = 0;
|
||
|
}
|
||
|
for (int i = 1; i <= n; i++) {
|
||
|
for (int j = 1; j <= n; j++) {
|
||
|
if (a[j] <= t[i].s) {
|
||
|
a[j] = t[i].f;
|
||
|
if (j >= count_chang + 1) count_chang++;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
delete[]a;
|
||
|
return count_chang;
|
||
|
}
|
||
|
int main() {
|
||
|
cin >> n;
|
||
|
for (int i = 1; i <= n; i++) {
|
||
|
cin >> t[i].s >> t[i].f;
|
||
|
}
|
||
|
sort(t+1,t+n+1,cmp);
|
||
|
cout << meeting() << endl;
|
||
|
return 0;
|
||
|
}
|
||
|
实验3.2磁带最优存储问题
|
||
|
#include <bits/stdc++.h>
|
||
|
#include<iostream>
|
||
|
#include<queue>
|
||
|
#include<fstream>
|
||
|
using namespace std;
|
||
|
struct Tape
|
||
|
{
|
||
|
int len; //长度
|
||
|
int pr; //概率
|
||
|
double tr;
|
||
|
friend bool operator< (Tape a,Tape b) {return a.tr>b.tr;}
|
||
|
Tape(int l=0,int p=0):len(l),pr(p),tr(l*p){};
|
||
|
};
|
||
|
priority_queue<Tape>tape;
|
||
|
double minTimeCost(int n,priority_queue<Tape>&tape,long long sum){
|
||
|
double res=0,acc=0;
|
||
|
while(!tape.empty())
|
||
|
{
|
||
|
acc+=tape.top().tr/sum;//计算tr累加值
|
||
|
res+=acc;
|
||
|
tape.pop();
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
|
int main()
|
||
|
{
|
||
|
int n,a,b;
|
||
|
long long sum=0;
|
||
|
cin>>n;
|
||
|
for(int i = 0;i<n;i++){
|
||
|
cin>>a>>b;
|
||
|
tape.push(Tape(a,b));
|
||
|
sum+=b;//计算概率和
|
||
|
}
|
||
|
double res=minTimeCost(n,tape,sum);
|
||
|
cout<<res;
|
||
|
return 0;
|
||
|
} }
|
||
|
double res=minTimeCost(n,tape,sum);
|
||
|
cout<<res;
|
||
|
return 0;
|
||
|
}
|
||
|
作业2.1烟台基地宿舍分配
|
||
|
#define _CRT_SECURE_NO_WARNINGS 1
|
||
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
typedef long long ll;
|
||
|
typedef pair<int, int> PII;
|
||
|
const int N = 1e5 + 10;
|
||
|
ll n;
|
||
|
ll dp[110][110];
|
||
|
int main()
|
||
|
{
|
||
|
ios::sync_with_stdio(0);
|
||
|
cin.tie(0);
|
||
|
cin >> n;
|
||
|
memset(dp, 0, sizeof dp);
|
||
|
dp[1][1] = 1;// i个人分j个宿舍
|
||
|
for (int i = 0; i <= n; i++) {
|
||
|
dp[i][0] = 0;
|
||
|
dp[i][1] = 1;
|
||
|
dp[0][i] = 0;
|
||
|
}
|
||
|
ll sum = 0;
|
||
|
for (int i = 1; i <= n; i++) {
|
||
|
for (int j = 1; j <= i; j++) {
|
||
|
if(dp[i][j]==0) dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] * j;
|
||
|
}
|
||
|
}
|
||
|
for (int i = 1; i <= n; i++) {
|
||
|
sum += dp[n][i];
|
||
|
}
|
||
|
cout << sum;
|
||
|
return 0; //17
|
||
|
}
|
||
|
作业2.2烟台基地宿舍分配2
|
||
|
#define _CRT_SECURE_NO_WARNINGS 1
|
||
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
typedef long long ll;
|
||
|
typedef pair<int, int> PII;
|
||
|
const int N = 1e5 + 10;
|
||
|
ll n,m;
|
||
|
ll dp[110][110];
|
||
|
int main()
|
||
|
{
|
||
|
ios::sync_with_stdio(0);
|
||
|
cin.tie(0);
|
||
|
cin >> n>>m;
|
||
|
memset(dp, 0, sizeof dp);
|
||
|
dp[1][1] = 1;// i个人分j个宿舍
|
||
|
for (int i = 0; i <= n; i++) {
|
||
|
dp[i][0] = 0;
|
||
|
dp[i][1] = 1;
|
||
|
dp[0][i] = 0;
|
||
|
}
|
||
|
ll sum = 0;
|
||
|
for (int i = 1; i <= n; i++) {
|
||
|
for (int j = 1; j <= i; j++) {
|
||
|
if(dp[i][j]==0) dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] * j;
|
||
|
}
|
||
|
}
|
||
|
cout << dp[n][m];
|
||
|
return 0;
|
||
|
}
|
||
|
作业3.1金字塔寻宝
|
||
|
#include <bits/stdc++.h>
|
||
|
#include<iostream>
|
||
|
using namespace std;
|
||
|
const int N=105;
|
||
|
int n,*num,*lastNum,ary1[N],ary2[N];
|
||
|
void dp()
|
||
|
{
|
||
|
num=ary1,lastNum=ary2;
|
||
|
for(int i=1;i<=n;i++){
|
||
|
for(int j=1;j<=i;j++){
|
||
|
cin>>num[j];
|
||
|
num[j]=max(lastNum[j],lastNum[j-1])+num[j];
|
||
|
}
|
||
|
swap(num,lastNum);
|
||
|
}
|
||
|
}
|
||
|
int maxValue()
|
||
|
{
|
||
|
int max=lastNum[1];
|
||
|
for(int i=2;i<=n;i++)
|
||
|
if(lastNum[i]>max) max=lastNum[i];
|
||
|
return max;
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
cin>>n;dp();
|
||
|
cout<<maxValue()<<endl;
|
||
|
return 0;
|
||
|
}
|
||
|
作业3.2数字游戏
|
||
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
void getnumber(string s);
|
||
|
void getresult();
|
||
|
const int N=1010;
|
||
|
int f[N][N],num[N][N],a[N];
|
||
|
int d[N][N];
|
||
|
int ans[N];
|
||
|
int cnt;
|
||
|
int n,k;
|
||
|
int main(){
|
||
|
string s;
|
||
|
int c,e;
|
||
|
cin>>n>>k;
|
||
|
cin>>s;
|
||
|
k--;
|
||
|
getnumber(s);
|
||
|
getresult();
|
||
|
cout<<f[n][k];
|
||
|
return 0;
|
||
|
}
|
||
|
void getnumber(string s){
|
||
|
for(int i=1;i<=s.size();i++){
|
||
|
a[i]=s[i-1]-'0';
|
||
|
}
|
||
|
for(int i=1;i<=n;i++){
|
||
|
for(int j=i;j<=n;j++){
|
||
|
num[i][j]=num[i][j-1]*10+a[j];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
void getresult(){
|
||
|
for(int i=1;i<=n;i++){
|
||
|
f[i][0]=num[1][i]; //处理边界
|
||
|
}
|
||
|
for(int i=1;i<=n;i++){ //枚举处理多少个数字
|
||
|
for(int j=1;j<=k;j++){ //枚举插入多少个乘号
|
||
|
for(int t=1;t<i;t++){ //枚举最后乘号的乘号插入哪里(将前多少个数字划为一类)
|
||
|
f[i][j]=max(f[i][j],f[t][j-1]*num[t+1][i]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
作业4.1摸金校尉
|
||
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
const int N=10000;
|
||
|
int servicetime[N];
|
||
|
int line[N];
|
||
|
int main(){
|
||
|
int n,s,sum,num;
|
||
|
cin>>n>>s;
|
||
|
for(int i=0;i<n;i++){
|
||
|
cin>>servicetime[i];
|
||
|
}
|
||
|
sort(servicetime,servicetime+n);
|
||
|
sum=servicetime[0]+s;
|
||
|
num=1;
|
||
|
for(int i=0;i<n;i++){
|
||
|
if(sum<servicetime[i]){
|
||
|
num+=1;
|
||
|
sum=servicetime[i]+s;
|
||
|
}
|
||
|
}
|
||
|
cout<<num;
|
||
|
}
|
||
|
作业4.2平均等待时间
|
||
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
const int N=10000;
|
||
|
int servicetime[N];
|
||
|
int line[N];
|
||
|
int main(){
|
||
|
int n,s,sum;
|
||
|
cin>>n>>s;
|
||
|
for(int i=0;i<n;i++){
|
||
|
cin>>servicetime[i];
|
||
|
}
|
||
|
sort(servicetime,servicetime+n);
|
||
|
for(int i=0;i<n;i++){
|
||
|
line[i%s]+=servicetime[i];
|
||
|
sum+=line[i%s];
|
||
|
}
|
||
|
cout<<(sum*1.0/n);
|
||
|
}
|