给定一组数字,一组有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. Spring Enable* 注解

    Spring提供了一系列以Enable开头的注解,这些注解本质上是激活Spring的某些管理功能.比如,EnableWebMvc. 这个注解引入了MVC框架在Spring 应用中需要用到的所有bean ...

  2. MySQL常用运算符:算术运算符、比较运算符、逻辑运算符

    (一)  算术运算符 注意: 在除法运算和模运算中,如果除数为0,将是非法除数,返回结果为NULL. div运算符主要是求两个数相除的商 (二)  比较运算符:比较运算符的运算结果为1(条件为真),0 ...

  3. 补齐-Django之Model操作

    http://www.cnblogs.com/wupeiqi/articles/6216618.html

  4. cpu概念

    cpu的主频=外频x倍频 cpu的主频不能完全决定cpu的性能,只是cpu性能的一个参数 cpu的外频是cpu的基准频率,它决定着整个主板的运行速度,超频超的是cpu的外频 IPC:cpu每一个时钟周 ...

  5. 2019.04.13 python基础

    第一节    主要讲python背景  没什么要注意的  了解记住概念就好 python官网  python.org  自带shell  可以运行python代码 在IDLE中怎么运行代码 新建文本  ...

  6. Android adt-bundle 开发环境的搭建_Linuxs

    本文完全是拷贝的: https://www.jb51.net/article/87957.htm  的文章, 有需要请看原文, 拷贝仅用于学习记录. 本文与<利用adt-bundle轻松搭建An ...

  7. C#设计模式(3)——工厂方法模式(转)

    C#设计模式(3)——工厂方法模式   一.引言 在简单工厂模式中讲到简单工厂模式的缺点,有一点是——简单工厂模式系统难以扩展,一旦添加新产品就不得不修改简单工厂方法,这样就会造成简单工厂的实现逻辑过 ...

  8. Eclipse安装fatjar(不用自己下载fatjar包)

    .安装Eclipse-jee-luna-SR2-win32-x86_64版本的插件支持 方法如下: Help -> Install New Software... -> Work with ...

  9. sitecore 数字化营销-path funnel

    路径分析器是一个应用程序,允许您查看联系人在浏览网站时所采用的各种路径.您可以查看联系人在转换目标并与广告系列互动时所采用的路径,让您深入了解哪些路径为每次转化提供最佳参与价值,以及哪些路径效率较低且 ...

  10. git的基本用法

    作业要求来自https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 一:以下是git的基本使用方法: 1:首先先进行账号注册. 2:然 ...