24小时热门版块排行榜    

Znn3bq.jpeg
查看: 1134  |  回复: 5

lbws328

新虫 (小有名气)

[求助] c++/c 求助关于多维数组里改变一个区域值 已有2人参与

如何用c++ 把数组里一个区域内得6个相邻的1变成0, 大于6个相邻的1不变。看图片

求大神给代码或思路,不要太难水平有限。。。

c++/c 求助关于多维数组里改变一个区域值
原始数据


c++/c 求助关于多维数组里改变一个区域值-1
相邻6个1变为0
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

Mr__Right

专家顾问 (著名写手)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ...
感谢参与,应助指数 +1
jjdg: 金币+1, 感谢参与 2017-08-18 12:59:26
lbws328: 金币+150, 有帮助 2017-08-23 10:09:46
首先你要识数,那是一共9个,相邻的是8个。

然后,找到中心位置指标,

[i,j]

然后,根据指标增减的2^3=8种排列组合,全部赋值为0
文章乃身外之物,要多考虑编辑、审稿人和读者的感受。
2楼2017-08-18 11:00:47
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

imyourkobe

铁杆木虫 (著名写手)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ...
感谢参与,应助指数 +1
lbws328: 金币+150, 有帮助 2017-08-23 10:09:58
很容易的。用二维数组或容器来存储数据,写一个简单的算法判断六个连续的数据为1的个数累加。满足则修改。不满足继续判断下一组。
3楼2017-08-18 21:50:25
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

lbws328

新虫 (小有名气)

引用回帖:
2楼: Originally posted by Mr__Right at 2017-08-18 11:00:47
首先你要识数,那是一共9个,相邻的是8个。

然后,找到中心位置指标,



然后,根据指标增减的2^3=8种排列组合,全部赋值为0

抱歉是我没说明白,我想问得是这连续9个值 *(形状不规则有3*3 还有2*3 各种各样)
从第一个1开始计算为1 第二个1为2第三个1为3 换行然后是4,5,6,换行,7,8,9
这样 小于等于9的,前面1,2,3,4,5,6,7,8,9全为0,大于9的值不变,我现在是 小于等于9的,前面1,2,3,4,5,6,7,8,9全为0这里不知道怎么办,求解答
4楼2017-08-23 10:15:38
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

lbws328

新虫 (小有名气)

引用回帖:
3楼: Originally posted by imyourkobe at 2017-08-18 21:50:25
很容易的。用二维数组或容器来存储数据,写一个简单的算法判断六个连续的数据为1的个数累加。满足则修改。不满足继续判断下一组。

抱歉是我没说明白,我想问得是这连续9个值 *(形状不规则有3*3 还有2*3 各种各样)
从第一个1开始计算为1 第二个1为2第三个1为3 换行然后是4,5,6,换行,7,8,9
这样 小于等于9的,前面1,2,3,4,5,6,7,8,9全为0,大于9的值不变,我现在是 小于等于9的,前面1,2,3,4,5,6,7,8,9全为0这里不知道怎么办,
5楼2017-08-23 10:18:52
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

lbws328

新虫 (小有名气)

引用回帖:
2楼: Originally posted by Mr__Right at 2017-08-18 11:00:47
首先你要识数,那是一共9个,相邻的是8个。

然后,找到中心位置指标,



然后,根据指标增减的2^3=8种排列组合,全部赋值为0

CODE:
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>  
#include <iostream>  
#include <vector>
using namespace std;

struct Location {
        double fai;                       
        int *grain_number;
}num[1000000];

#define seed 40000 //粒子个数
bool main(){
        int grain_num[seed], correction_num[seed], grain_size[seed],
                divided_grain_count[seed], div_area,
                grain_count, grain_sum, divided_grain_sum, cut_off,
                grain_size_distribution[seed], max_grain_size, div_distribution;

        double boundary_line, boundary_line1, boundary_line2;
        int count, count2, meshx, meshy, meshX, meshY;
        char        fname[100];
        FILE        *datin0;
        //        初期化
        grain_count = 0, count2 = 0, grain_sum = 0, divided_grain_sum = 0, count = 0;
        max_grain_size = 0;
        for (int i = 0; i < seed; i++){
                grain_num[i] = i;
                correction_num[i] = i;
                grain_size[i] = -1;
                divided_grain_count[i] = -1;
                grain_size_distribution[i] = 0;
        }
        // 指定文件夹和保存
        char*  file_name("C:\\testp.loc");
        ofstream fout1("save1.xls");
        ofstream fout2("save2.xls");
        ofstream fout3("save3.xls");
        ofstream fout4("save4.xls");
        ofstream fout5("save5.xls");

        boundary_line = 0.4;        // 判断数值大小
        cut_off = 0;        // cut_off以下的不计算

        //        边界设定
        meshx = 1200;
        meshy = 150;
        meshX = meshx + 2; meshY = meshy + 2;

        // 打开文件夹
        std::ifstream fin;
        fin.open(file_name, std::ios::in);
        // 文件夹确认
        if (!fin){
                std::cout << " file open error! " << "\n";
                return false;
        }
        if (!fout1){
                cerr << "open error!" << endl;
                exit(1);
        }
        if (!fout2){
                cerr << "open error!" << endl;
                exit(1);
        }
        if (!fout3){
                cerr << "open error!" << endl;
                exit(1);
        }
        if (!fout4){
                cerr << "open error!" << endl;
                exit(1);
        }
        if (!fout5){
                cerr << "open error!" << endl;
                exit(1);
        }
        //读入粒子
        for (int i = 0; i<meshY; i++){
                for (int j = 0; j<meshX; j++){
                        if (i <= 0 || i >= meshY - 1 || j <= 0 || j >= meshX - 1){
                                num[count].fai = 0;
                                count++;
                                continue;

                        }
                        else{
                                fin >> num[count].fai;

                                count++;


                        }
                }
        }

        //        判断数值大小大于0.4等于1小于0.4等于0
                count = 0;
        for (int i = 0; i<meshY; i++){
                for (int j = 0; j<meshX; j++){
                        if (num[count].fai < boundary_line)
                        {
                                num[count].fai = 0;
                        }
                        else{
                                num[count].fai = 1;
                        }
                        fout1 << num[count].fai << " ";
                        count++;
                }
                fout1 << endl;
        }
        // grain_number
        count = 0;
        for (int i = 0; i<meshY; i++){
                for (int j = 0; j<meshX; j++){
                        if (i == 0 || i == meshY - 1 || j == 0 || j == meshX - 1){
                                num[count].grain_number = &grain_num[0];
                                count++;
                                continue;
                        }
                        else{
                                if (j == 1){
                                        grain_count++;
                                }
                                num[count].grain_number = &grain_num[grain_count];

                                if (i > 1 && num[count - meshX].fai == 1. && num[count].fai == 1.){
                                        grain_num[grain_count] = *num[count - meshX].grain_number;
                                }
                                if (num[count].fai == 0){
                                        num[count].grain_number = &grain_num[0];
                                        if (num[count + 1].fai == 1 && j>2 && j<meshX - 2){
                                                grain_count++;
                                        }
                                }
                                count++;
                        }
                        fout2 << num[count].grain_number << " "; //号码赋值
                        //cout << grain_count << " ";
                }
                fout2 << endl;
        }
        count = 0;
        for (int i = 0; i<10; i++){
                for (int j = 0; j<meshX; j++){

                        if (i == 0 || i == meshY - 1 || j == 0 || j == meshX - 1){
                                count++;
                                continue;
                        }
                        else{
                                if (num[count - meshX].fai == 1 && num[count].fai == 1){            //上下值为1 有phase
                                        if (*num[count].grain_number < *num[count - meshX].grain_number){    //如果下面番号小于上面的番号
                                                correction_num[*num[count - meshX].grain_number] = *num[count].grain_number;//把*num[count].grain_number的值赋给correction_num[*num[count - meshX].grain_number]
                                        }
                                        else{
                                                correction_num[*num[count].grain_number] = *num[count - meshX].grain_number;//否则*num[count - meshX].grain_number赋给correction_num[*num[count].grain_number]
                                        }
                                }
                                count++;
                        }
                        fout3 << *num[count].grain_number << " ";
                }
                fout3 << endl;
        }
        // 计算grain_size粒径大小
        count = 0;
        for (int i = 0; i<meshY; i++){
                for (int j = 0; j<meshX; j++){
                        if (i == 0 || i == meshY - 1 || j == 0 || j == meshX - 1){
                                count++;
                                continue;
                        }
                        else{
                                grain_size[*num[count].grain_number] += 1;
                                count++;
                        }
                        fout4 << grain_size[*num[count].grain_number] << " ";
                }
                fout4 << endl;
        }
        //这里插入粒径长度小于9的小于等于0 大于9的值不变



        // grain_sum 计算个数
        count = 1;
        while (count<seed){
                if (grain_size[count] > cut_off){
                        grain_sum++;

                        if (grain_size[count] > max_grain_size){
                                max_grain_size = grain_size[count];
                                std::cout << "max = " << max_grain_size << "\n";
                        }
                }
                count++;
        }
}

6楼2017-08-23 10:19:55
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 lbws328 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[硕博家园] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 1rx34o113h 2026-05-23 3/150 2026-05-24 17:41 by 0i3mu4vkjz
[基金申请] 评审有感 +16 popular289 2026-05-18 27/1350 2026-05-24 17:34 by hhs666
[教师之家] 论文撤稿了 +4 bjvtcliu 2026-05-24 7/350 2026-05-24 17:29 by bjvtcliu
[硕博家园] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +4 hvkbtfonbv 2026-05-23 4/200 2026-05-24 17:21 by 75ui6h7z2t
[博后之家] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 hvkbtfonbv 2026-05-23 3/150 2026-05-24 17:10 by 75ui6h7z2t
[考博] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 hvkbtfonbv 2026-05-23 3/150 2026-05-24 17:01 by 75ui6h7z2t
[考研] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 a2tycdlnq1 2026-05-23 5/250 2026-05-24 16:21 by hhx1yx9evi
[论文投稿] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 a2tycdlnq1 2026-05-23 4/200 2026-05-24 16:16 by hhx1yx9evi
[基金申请] 河北省自然科学基金 +6 Peterchao 2026-05-18 9/450 2026-05-24 16:02 by 130067131
[硕博家园] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +4 pmo95bazuy 2026-05-23 8/400 2026-05-24 15:56 by 1uy1ht2y9r
[教师之家] 某211大学教师把个人教师官方主页改成:我跑了我跑了我跑了!官宣跑路! +4 zju2000 2026-05-21 5/250 2026-05-24 09:35 by songwz
[考博] 26/27申博自荐 10+4 ZXW0202 2026-05-22 9/450 2026-05-24 08:47 by bjvtcliu
[基金申请] 青B发送上会通知了吗 +5 chemBioBro 2026-05-22 7/350 2026-05-23 12:35 by zhuifengzhy
[考博] 博士申请 +3 焦晓明 2026-05-21 3/150 2026-05-23 11:26 by mlc840311
[论文投稿] 投稿求助,期刊 +4 希冀,有书读 2026-05-20 8/400 2026-05-22 10:16 by 希冀,有书读
[文学芳草园] 献血感触 +7 呀呀好傻 2026-05-19 13/650 2026-05-21 20:15 by 呀呀好傻
[基金申请] 国自然评分 +4 无名者登山 2026-05-20 5/250 2026-05-21 16:35 by swuq
[基金申请] 提交了我也来说说感想 +9 fummck 2026-05-20 10/500 2026-05-21 14:17 by draco1987
[有机交流] 反应很差,大量原料没有反应 5+3 Mr.Zot 2026-05-19 8/400 2026-05-20 22:19 by Equinoxhua
[考博] 如果工作了想读博,可以边工作边读全日制嘛? 30+3 铁达火车 2026-05-18 5/250 2026-05-20 09:33 by tfang
信息提示
请填处理意见