24小时热门版块排行榜    

CyRhmU.jpeg
查看: 1003  |  回复: 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 的主题更新
信息提示
请填处理意见