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 ...
随机推荐
- Feign 重试解析
Spring cloud Feign 在restful 调用失败后,会进行重试.在没有到达指定重试次数,会一直重试. @Override public Object invoke(Object[] a ...
- 20175303 2018-2019-2 《Java程序设计》第2周学习总结
•总体的说,第2周主要学习了课本第二章第三章的内容,这周的学习简直比上一周的简单而且容易理解得多.上周的各种安装,注册,眼花缭乱,很早就开始弄,但各种出错,到最后都想放弃了,好在有同学的帮助,帮我正确 ...
- python练习题-day11
1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件), 要求:登录成功一次,后续的函数都无需再输入用户名和密码 flag=False def wrapper(fun): def inn ...
- 【托业】【跨栏阅读】错题集-REVIEW1
05 06 REVIEW 1
- CookieUitl
import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.h ...
- Excel Vlookup使用
VLookup用途 作用:关联两张表数据显示 如A表有字段id, name, B表有字段id, sex, 要把两张表合并成id, name, sex 操作步骤 1,在A表后面新增列sex 2, 插入公 ...
- nghttp2 和nginx的实践
主要参考https://bg2bkk.github.io/post/HTTP2%E7%9A%84%E5%AE%9E%E8%B7%B5%E8%BF%87%E7%A8%8B/,和https://fangp ...
- 第四章 jQuery节点操作
1.DOM操作分为三类:(1)DOM Core:任何一种支持DOM的编程语言都可以使用用它如:getElementById()(2)HTML-DOM:用于处理HTML文档,如document,form ...
- NOIP2009(codevs1173)最优贸易
题目大意:给你一张有n个点m条边的有向图,每个点有一个权值,求一条1到n的路径,使得这条路径上存在两个点且他们的权值差最大. 思路:用dis[i]]记录从1到i的路径中所能得到两点间权值差的最大值,然 ...
- SLF4J bindings
see: https://www.slf4j.org/manual.html
