Professional Documents
Culture Documents
实验目的:一维数组与二维数组熟悉和掌握一维数组和二维数组的说明、初始
化方法,数组元素访问方法熟悉和掌握一维数组和二维数组的说明、初始化方
法,数组元素访问方法
题目 1:取最大值
题目 2:循环左移
将一个数组中的数循环左移 n 位。
例如,如果数组中原来的数为:1 2 3 4 5
移动 1 位后变成:2 3 4 5 1
移动 2 位后变成:3 5 5 1 2
#include <iostream>
using namespace std;
void reverse(int *arr , int begin , int end) //逆置
{
int i , j;
for(i = begin , j = end ; i < j ; i++ , j--)
{
int t;
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
void function(int *arr , int n , int p) //循环左移
{
reverse(arr,0,p-1);
reverse(arr,p,n-1);
reverse(arr,0,n-1);
}
void display(int *arr , int n)
{
int i;
for(i = 0 ; i < n ; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main()
{
int arr_len,arr[100],i,j;
cin >> arr_len;
for (i = 0;i < arr_len;i++)
cin >> arr[i];
//int arr[5] = {1,2,3,4,5};
cin >> j;
function(arr,arr_len,j);
display(arr,arr_len);
return 0;
}
题目 3:凯撒加密算法
凯撒加密算法是古罗马凯撒大帝用来对军事情报进行加密的算法,它采用了替换方法对信
息中的每一个英文字符循环替换为字母表序列该字符后面第三个字符,对应关系如下:
明码字母表:ABCDEFGHIJKLMNOPQRSTUVWXYZ
密码字母表:DEFGHIJKLMNOPQRSTUVWXYZABC
明码字母表:abcdefghijklmnopqrstuvwxyz
密码字母表:defghijklmnopqrstuvwxyzabc
这个例子中位移量为 3;
#include<iostream>
#include<string.h>
#define N 100
using namespace std;
//凯撒密码
//加密公式 f(a)=(a+3) mod 26
//解密公式 f(a)=(a+23) mod 26
int psw(char *strI, int numB) //加密过程
{
for (int i = 0; i < strlen(strI); i++)
{
if ((!(strI[i] >= 'A' && strI[i] <= 'Z')) && (!(strI[i] >= 'a' && strI[i] <= 'z')))
{
cout << "输入字符串中存在非法字符!";
return 0;
}
}
for (int i = 0; i < strlen(strI); i++)
{
if (strI[i] >= 'A' && strI[i] <= 'Z')
{
strI[i] = ((strI[i] - 'A') + numB) % 26 + 'A';
}
else if (strI[i] >= 'a' && strI[i] <= 'z')
{
strI[i] = ((strI[i] - 'a') + numB) % 26 + 'a';
}
}
cout << "加密完成:" << strI << endl;
return 0;
}
int unpsw(char *strI, int numB) //解密
{
int num;
num = 26 - numB;
for (int i = 0; i < strlen(strI); i++)
{
if ((!(strI[i] >= 'A' && strI[i] <= 'Z')) && (!(strI[i] >= 'a' && strI[i] <= 'z')))
{
cout << "输入字符串中存在非法字符!";
return 0;
}
}
for (int i = 0; i<strlen(strI); i++)
{
}
cout << "解密完成:" << strI << endl;
return 0;
}
int main()
{
char str[N];
int model;
int numB;
cin >> model;
switch(model)
{
case 1: cin>>str;
cin>>numB;
psw(str,numB);
cout<<endl;
break;
case 2: cin>>str;
cin>>numB;
unpsw(str,numB);
cout<<endl;
break;
default:cout << "此程序不提供此项功能!" << endl;
}
return 0;
}
题目 4:数组合并
}
for (n = 0;n < c_len;n++) //遍历 c 数组
cout << c[n] << endl;
return 0;
}
题目 5:背包问题
为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设
你一共有资金 n 元,而市场有 m 种大米,每种大米都是袋装产品,其价格不等,并且只能
整袋购买。
请问:你用有限的资金最多能采购多少公斤粮食呢?
Input
输入数据第一行是两个整数 n 和 m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的
种类,然后是 m 行数据,每行包含 3 个数 p,h 和 c(1<=p<=20,1<=h<=200,1<=c<=20),分别
表示每袋的价格、每袋的重量以及对应种类大米的袋数。
Output
对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,
并且经费你可以不用完。每个实例的输出占一行。
输入:
8 2
2 100 4
4 100 2
输出:
400
#include<math.h>
#include<iostream>
using namespace std;
int f[105], V;
int main()
{
int t, i, n, cost, weight, amount;
//memset(f, 0, sizeof(f));
cin >> V >> n;
for (i = 1;i <= n;i++)
{
cin >> cost >> weight >> amount;
MultipelePack(cost, weight, amount);
}
cout << f[V] << endl;
return 0;
}