给定一组数字,一组有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的更多相关文章

  1. SCNU ACM 2016新生赛决赛 解题报告

    新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...

  2. SCNU ACM 2016新生赛初赛 解题报告

    新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...

  3. acm结束了

    最后一场比赛打完了.之前为了记录一些题目,开了这个博客,现在结束了acm,这个博客之后也不再更新了. 大家继续加油!

  4. 关于ACM的总结

    看了不少大神的退役帖,今天终于要本弱装一波逼祭奠一下我关于ACM的回忆. 从大二上开始接触到大三下结束,接近两年的时间,对于大神们来说两年的确算不上时间,然而对于本弱来说就是大学的一半时光.大一的懵懂 ...

  5. 第一届山东省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 ...

  6. 第一届山东省ACM——Balloons(java)

    Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...

  7. ACM之鸡血篇

    一匹黑马的诞生 故事还要从南京现场赛讲起,话说这次现场赛,各路ACM英雄豪杰齐聚南京,为争取亚洲总舵南京分舵舵主之职位,都使出了看 家本领,其中有最有实力的有京城两大帮清华帮,北大帮,南郡三大派上交派 ...

  8. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  9. acm 1002 算法设计

    最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是 ...

  10. ACM进阶计划

    ACM进阶计划ACM队不是为了一场比赛而存在的,为的是队员的整体提高.大学期间,ACM队队员必须要学好的课程有:lC/C++两种语言l高等数学l线性代数l数据结构l离散数学l数据库原理l操作系统原理l ...

随机推荐

  1. Codeforces 1136C - Nastya Is Transposing Matrices

    题目链接:https://codeforces.com/problemset/problem/1136/C 题意: 给出 $n \times m$ 的矩阵 $A,B$,你可以对其中任意某个 $k \t ...

  2. Interllij IDEA中启动web项目

    1.在IDEA中打开你的Web应用,点击一下绿色三角形左边的框框,然后在弹出框上选择Edit Configurations,会弹出一个配置面板. 2.在弹出的面板中我们点击Defaults,然后找到T ...

  3. SPSS提示“列表中不允许存在字符串变量”的解决方法

    点击 查看 菜单->变量->将需要修改为数字类型的列属性改为数字,还可以更改小数位数

  4. .net ML机器学习中遇见错误记录

    避免入坑: 1.错误提示 numClasses must be at least   2 大概是训练模型的数据分类必须是两种,如下错误: 正确数据集如下:

  5. Linux常用总结

    CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服务进行管理.systemd兼容SysV和Linux标准 ...

  6. Echarts-图表根据值的不同展示成不同的颜色

    series : [        {            name:'直接访问',            type:'bar',            barWidth: '60%',      ...

  7. 【UML】NO.71.EBook.9.UML.4.002-【PowerDesigner 16 从入门到精通】- RQM

    1.0.0 Summary Tittle:[UML]NO.71.EBook.9.UML.4.002-[PowerDesigner 16 从入门到精通]-  RQM Style:DesignPatter ...

  8. gitlab的ssh key有2个

    Gitlab添加SSH key可以pull不能push的问题 最后解决的是 使用http去clone pull  提交 没用ssh.就是需要输入密码

  9. Treap仿set 模板

    Treap仿set 模板 蓝书232 &代码: #include <cstdio> #include <bitset> #include <iostream> ...

  10. python模块化学习(一)

    import time #获取cpu的时间: #获取本地时间: #获取标准时间格式: #获取时间戳: #print(time.clock()) #这个在3即将被舍弃 print(time.proces ...