acm
给定一组数字,一组有9个数字,将这9个数字填写到3*3的九宫格内;使得横,竖,斜对角一条线上的三个数字之和相等;如果无解则打印无解;
#include <iostream>
#include <algorithm>
using namespace std; //a[9]为从小到大排好序的一维数组,b[3][3]为九宫格的二维数组
int jiuGongGe(int b[][3],int a[9])
{
int x = 0, y = 1;
b[x][y] = a[0]; //将最小的数字赋值给b[0][1],九宫格中第一行第二列的位置
for (int k = 1; k < 9; k++)
{
int xNew = x - 1;
int yNew = y + 1; //依次向右向上寻找,将下一个数字放在该位置
if (xNew < 0)
xNew = 2;
if (yNew > 2)
yNew = 0;
if (b[xNew][yNew] != 0){ //若该位置有数字了,则向下寻找,将下一个数字放在该位置
xNew = x + 1;
yNew = y;
}
b[xNew][yNew] = a[k];
x = xNew;
y = yNew;
}
int row1 = b[0][0] + b[0][1] + b[0][2]; //计算九宫格中每一行,列,斜对角线上的值
int row2 = b[1][0] + b[1][1] + b[1][2];
int row3 = b[2][0] + b[2][1] + b[2][2];
int col1 = b[0][0] + b[1][0] + b[2][0];
int col2 = b[0][1] + b[1][1] + b[2][1];
int col3 = b[0][2] + b[1][2] + b[2][2];
int dig1 = b[0][0] + b[1][1] + b[2][2];
int dig2 = b[2][0] + b[1][1] + b[0][2];
int flag = 0; //比较横竖斜方向上的的值是否相等
if (row1 == row2&&row1 == row3&&row1 == col1&&row1 == col2&&row1 == col3&&row1 == dig1&&row1 == dig2)
flag = 1;
return flag;
} int main()
{
int a[9];
for (int i = 0; i < 9; i++) //输入九个数字
cin >> a[i];
sort(a, a + 9); //对九个数字按照从小到大的顺序排序
for (int i = 0; i < 9; i++) //输出排好序的九个数字
cout << a[i] << " ";
cout << endl;
int b[3][3] = { 0 };
int flag = jiuGongGe(b, a);
if (flag) //若满足要求,则输入九宫格中的数组,否则输出无解
for (int i = 0; i < 3; ++i){
for (int j = 0; j < 3; ++j){
cout << b[i][j] << "\t";
}
cout << endl;
}
else
cout << "无解" << endl;
system("pause");
return 1;
}


#include <string.h>
#include <iostream>
using namespace std; #define N 100 void tuse(int *p, int i, int j, int k) //将数组a[0][0]的地址传给指针p,其位置为i,j,连通区域的标号k
{ //判断为小岛的点赋值为2,并进入该点上下左右的点,递归的进行扩展,将连通在一起的点,都赋值为k
*(p + i*N + j) = k;
if (*(p + i*N + j - ) == )//左边
tuse(p, i, j - , k);
if (*(p + (i - )*N + j) == )//上面
tuse(p, i - , j, k);
if (*(p + i*N + j + ) == )//右边
tuse(p, i, j + , k);
if (*(p + (i + )*N + j) == )//下面
tuse(p, i + , j, k);
return;
} void computeArea(int *p, int br, int *p1, int *p2) //计算第一大区域的面积与第二大区域的面积,返回给指针p1,p2
{
for (int i = ; i < br; i++)
for (int j = ; j < br; j++)
{
if (*(p + i*N + j) == )
(*p1)++;
if (*(p + i*N + j) == )
(*p2)++;
}
} int main()
{
int br, i, j, num = ;
int row[][];
int col[][];
int k = , area1 = , area2 = ; //第一大岛的值全部为2
int a[][] = { { , , , , , },
{ , , , , , },
{ , , , , , },
{ , , , , , },
{ , , , , , },
{ , , , , , } };
memset(row, -, sizeof(row));
memset(col, -, sizeof(col));//compared with number 0
br = ;
for (i = ; i<br; i++) //判断每行中最左边的1和最右边的1的标号row[i][0],row[i][1],每列中最上边的1和最下边的1的标号col[i][0],col[i][1]
{
for (j = ; j<br; j++)
{
//scanf("%d", &a[i][j]);
if (a[i][j] == )
{
if (row[i][] == -)
row[i][] = j;
if (col[j][] == -)
col[j][] = i;
row[i][] = j;
col[j][] = i;
}
}
}
for (i = ; i<br; i++)
{
for (j = ; j<br; j++)
{
if (a[i][j] == )
{
if (j>row[i][] && j<row[i][] && i>col[j][] && i<col[j][]) //如果该点左边,右边,上边,下边有1,则判断该点为岛
{
tuse(&a[][], i, j, k); //进入该点,递归,将该点的连通区域都标记为k
k++;
}
}
}
}
for (i = ; i < br; i++)
{
for (j = ; j < br; j++) //输出更改标记后的矩阵a
cout << a[i][j] << " ";
cout << endl;
}
computeArea(&a[][], br, &area1, &area2);
if (area2 != ) //如果有第二大小岛,则输出area2的面积
cout << area2 << endl;
else
cout << area1 << endl; //否则,输出最大小岛的面积area1
system("pause");
return ;
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; int maxProfit(vector<int> &prices) {
int len = prices.size();
if (len == )
return ;
vector<int> pre(len);
vector<int> post(len);
int minPrice = prices[];
for (int i = ; i<len; i++){ //计算第i点之前的最大利润pre
minPrice = min(minPrice, prices[i]);
pre[i] = max(pre[i - ], prices[i] - minPrice);
}
int maxPrice = prices[len - ];
for (int i = len - ; i >= ; i--){ //计算第i点之后的最大利润post
maxPrice = max(maxPrice, prices[i]);
post[i] = max(post[i + ], maxPrice - prices[i]);
}
int maxProfit = ;
for (int i = ; i<len; i++){ //计算第i点的,pre[i]与post[i]之和的最大值,赋值给maxProfit
maxProfit = max(maxProfit, pre[i] + post[i]);
}
return maxProfit;
} int main()
{
vector<int> array;
vector <int>::iterator Iter;
int num;
cout << "please input a number" << endl;
cin >> num;
while (num != ) //循环输入array[i]中的值,直到输入0停止
{
array.push_back(num);
cin >> num;
}
int maxp = maxProfit(array);
cout << "最大利润为:"<<maxp << endl;
system("pause");
return ;
}

acm的更多相关文章
- SCNU ACM 2016新生赛决赛 解题报告
新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...
- SCNU ACM 2016新生赛初赛 解题报告
新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...
- acm结束了
最后一场比赛打完了.之前为了记录一些题目,开了这个博客,现在结束了acm,这个博客之后也不再更新了. 大家继续加油!
- 关于ACM的总结
看了不少大神的退役帖,今天终于要本弱装一波逼祭奠一下我关于ACM的回忆. 从大二上开始接触到大三下结束,接近两年的时间,对于大神们来说两年的确算不上时间,然而对于本弱来说就是大学的一半时光.大一的懵懂 ...
- 第一届山东省ACM——Phone Number(java)
Description We know that if a phone number A is another phone number B’s prefix, B is not able to be ...
- 第一届山东省ACM——Balloons(java)
Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...
- ACM之鸡血篇
一匹黑马的诞生 故事还要从南京现场赛讲起,话说这次现场赛,各路ACM英雄豪杰齐聚南京,为争取亚洲总舵南京分舵舵主之职位,都使出了看 家本领,其中有最有实力的有京城两大帮清华帮,北大帮,南郡三大派上交派 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- acm 1002 算法设计
最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是 ...
- ACM进阶计划
ACM进阶计划ACM队不是为了一场比赛而存在的,为的是队员的整体提高.大学期间,ACM队队员必须要学好的课程有:lC/C++两种语言l高等数学l线性代数l数据结构l离散数学l数据库原理l操作系统原理l ...
随机推荐
- Linux服务器上搭建web项目环境
一.下载并安装jdk 去官网下载linux系统上jdk的安装包jdk-8u181-linux-x64.tar.gz,在Linux的/usr目录下新建文件夹java,可以使用命令:cd /usr ...
- winfrom弹出窗口用timer控件控制倒计时20秒后关闭
功能描述: 因为在程序退出时需要确认是否是误操作,所以加了密码输入的子窗体,子窗体在20秒内会自动关闭 代码如下: private int count; private void Form2_Load ...
- Android hdpi ldpi mdpi xhdpi xxhdpi适配详解
设计稿计算: x/2.5=1080/3x=900y/2.5=1920/3y=1600 http://blog.csdn.net/lantiankongmo/article/details/505491 ...
- mixpanel umeng talkingdata
市面上可以比较容易的接触到的实时大数据用户行为分析系统有很多,比如国外有著名的Mixpanel.Localytics.Google,国内有TalkingData.这些公司都提供基于云的大数据分析系统, ...
- varnish缓存系统基础知识
缓存系统类型 1.页面缓存/pageCache 缓存静态资源(html js css image) 例如:varnish squid 2.数据缓存/dataCache 缓存应 ...
- idea 无法找到或加载主类
- 53.CSS---CSS水平垂直居中常见方法总结
CSS水平垂直居中常见方法总结 1.元素水平居中 当然最好使的是: margin: 0 auto; 居中不好使的原因: 1.元素没有设置宽度,没有宽度怎么居中嘛! 2.设置了宽度依然不好使,你设置的是 ...
- 小程序canvas生成海报保存至手机相册
小程序canvas画图保存至手机相册 (1)可直接展示生成的海报 .因手机分辨率不同可能导致生成的海报会有细微差别,这里隐藏canvas海报,页面正常设置海报样式保存时保存隐藏的canvas海报 (2 ...
- Delphi中类的运行期TypeInfo信息结构说明(转载)
Delphi中类的运行期TypeInfo信息结构说明作者:刘啸CnPack开发组 http://www.cnpack.org关键字:RTTI, TypeInfo, TypeData, PropInfo ...
- m2e-wtp的作用
描述 Maven3下的项目结构,target目录下会有一个m2e-wtp文件夹,删除掉会自动生成,有什么作用呢? wtp解释 WTP:Web Tools Project Maven集成WTP The ...
