vb编写程序,计算s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)

vb编写程序,计算s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n),第1张

Private Sub Command1_Click()

Dim n As Integer

Dim sum As Double

Dim Tsum As Integer

Tsum = 0

sum = 0

n = InputBox("请输入数字N")

For i = 1 To n

    Tsum = Tsum + i

    sum = sum + Tsum

Next i

Print sum

End Sub

/

snake

/

#include <stdioh>

#include <stdlibh>

#include <stringh>

#define DEBUG 0

#define printpos() \

printf("File: %s\tLine: %d\n", __FILE__, __LINE__); fflush(stdout);

#define CALLOC(ARRAY, NUM, TYPE) \

ARRAY = (TYPE) calloc(NUM, sizeof(TYPE)); \

if (ARRAY == NULL) { \

printf("File: %s, Line: %d: ", __FILE__, __LINE__); \

printf("Allocating memory failed\n"); \

exit(0); \

}

#define REALLOC(ARRAY, NUM, TYPE) \

ARRAY = (TYPE) realloc(ARRAY, (NUM)sizeof(TYPE)); \

if (ARRAY == NULL) { \

printf("File: %s, Line: %d: ", __FILE__, __LINE__); \

printf("Allocating memory failed\n"); \

exit(0); \

}

const int START = -1;

const int HOME = -2;

#if DEBUG

int m=4, n=4;

int a[4][4] = {{7, 0, 4, 18}, {4, 0, 1, 1}, {15, 7, 11, -1}, {0, 12, -2, 0}};

#else

int m=0, n=0;

int a=NULL;

#endif

struct pos {

int x;

int y;

};

typedef struct pos pos;

struct node {

pos p;

int mv;

int n;

};

typedef struct node node;

const pos mv[4] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };

/

get m, n, a and check them

/

int setup()

{

int nstart=0, nhome=0;

int i, j;

#if DEBUG

#else

//get the dimension of the matrix and allocate memory

printf("Please input the number of rows of the matrix: ");

scanf("%d", &m);

if (m<=0) {

printf("Number of rows must be greater than 0\n");

exit(0);

}

a = (int) calloc(m, sizeof(int));

if (a == NULL) {

printf("Allocate memory failed\n");

exit(1);

}

printf("Please input the number of columns of the matrix: ");

scanf("%d", &n);

if (n<=0) {

printf("Number of columns must be greater than 0\n");

exit(0);

}

for (i=0; i<m; i++) {

a[i] = (int) calloc(n, sizeof(int));

if (a[i] == NULL) {

printf("Allocate memory failed\n");

exit(1);

}

}

//get the matrix

printf("Please input the matrix, entities seperated by blank:\n");

for (i=0; i<m; i++) {

for (j=0; j<n; j++) {

scanf("%d", &a[i][j]);

}

}

#endif

//check the matrix

for (i=0; i<m; i++) {

for (j=0; j<n; j++) {

if (a[i][j] == START) {

nstart++;

if (nstart > 1) {

printf("More than 1 starting point\n");

exit(0);

}

} else if (a[i][j] == HOME) {

nhome++;

if (nhome > 1) {

printf("More than 1 home point\n");

exit(0);

}

} else if (a[i][j] < 0) {

printf("a[%d][%d] = %d has no meaning\n", i, j, a[i][j]);

exit(0);

}

}

}

if (nstart == 0) {

printf("No starting point\n");

exit(0);

}

if (nhome == 0) {

printf("No home point\n");

exit(0);

}

//output the matrix

printf("The matrix (%d X %d):\n", m, n);

for (i=0; i<m; i++) {

for (j=0; j<n; j++) {

printf("%d\t", a[i][j]);

}

printf("\n");

}

return 0;

}

int solve(node optpath)

{

pos dest; //destinating point

node curpath = NULL; //current path

node sol = NULL;

int nsol = 0;

int nsteps; //number of steps

int i, j;

int curmv = -1;

int sucmv = 0; //sucessfully moved

int sum;

int maxsum=0;

//setup starting point

for (i=0; i<m; i++) {

for (j=0; j<n; j++) {

if (a[i][j] == START) {

destx = i;

desty = j;

break;

}

}

}

nsteps = 0;

CALLOC(curpath, nsteps+1, node);

curpath[0]px = destx;

curpath[0]py = desty;

curpath[0]mv = -1;

a[destx][desty] = 0;

curmv = 0;

while (1) {

for (sucmv=0, curmv=curpath[nsteps]mv+1; curmv<4; curmv++) {

destx = curpath[nsteps]px + mv[curmv]x;

desty = curpath[nsteps]py + mv[curmv]y;

if (destx < 0 || destx >= m || desty < 0 || desty >= n) {

curpath[nsteps]mv = curmv;

continue;

}

if (a[destx][desty] == 0) {

curpath[nsteps]mv = curmv;

continue;

}

nsteps++;

REALLOC(curpath, nsteps+1, node);

curpath[nsteps]px = destx;

curpath[nsteps]py = desty;

curpath[nsteps-1]mv = curmv;

curpath[nsteps]mv = -1;

curpath[nsteps]n = a[destx][desty];

a[destx][desty] = 0;

sucmv = 1;

break;

}

if (sucmv) {

if (curpath[nsteps]n == HOME) {

nsol++;

REALLOC(sol, nsol, node);

CALLOC(sol[nsol-1], nsteps+1, node);

memcpy(sol[nsol-1], curpath, (nsteps+1)sizeof(node));

//back

a[curpath[nsteps]px][curpath[nsteps]py] = curpath[nsteps]n;

nsteps--;

if (nsteps == -1 && curpath[0]mv == 3) break;

REALLOC(curpath, nsteps+1, node);

} else {

continue;

}

} else {

a[curpath[nsteps]px][curpath[nsteps]py] = curpath[nsteps]n;

nsteps--;

if (nsteps == -1 && curpath[0]mv == 3) break;

REALLOC(curpath, nsteps+1, node);

}

}

//printf("number of solutions: %d\n", nsol);

for (maxsum=0, i=0; i<nsol; i++) {

//printf("Solution %d \n", i);

//printf("\tPath: ");

sum = -1HOME;

for (j=0; ; j++) {

//printf("(%d, %d)\t", sol[i][j]px, sol[i][j]py);

sum += sol[i][j]n;

if (sol[i][j]mv == -1) break;

}

//printf("\n\tSum of apples: %d\n", sum);

if (sum>maxsum) {

maxsum = sum;

optpath = sol[i];

}

}

return 0;

}

int output(node path)

{

int i=0, sum=0;

printf("Path: ");

sum = -1HOME;

for (i=0; ; i++) {

printf("(%d, %d)\t", path[i]px, path[i]py);

sum += path[i]n;

if (path[i]mv == -1) break;

}

printf("\nSum of apples: %d\n", sum);

return 0;

}

int main()

{

node path=NULL;

setup();

solve(&path);

output(path);

return 0;

}

编译、链接、运行程序,输入与输出如下:

:!gcc -Wall tmpc -o tmp

:! /tmp

Please input the number of rows of the matrix: 5

Please input the number of columns of the matrix: 5

Please input the matrix, entities seperated by blank:

1 7 9 7 0

-2 8 10 8 7

0 10 8 2 -1

4 3 0 7 0 9

1 2 5 1 0 7

The matrix (5 X 5):

1 7 9 7 0

-2 8 10 8 7

0 10 8 2 -1

4 3 0 7 0

9 1 2 5 1

Path: (2, 4) (1, 4) (1, 3) (0, 3) (0, 2) (1, 2) (2, 2) (2, 3) (3, 3) (4, 3) (4, 2) (4, 1) (4, 0) (3, 0) (3, 1) (2, 1) (1, 1) (0, 1) (0, 0) (1, 0)

Sum of apples: 108

:!gcc -Wall tmpc -o tmp

:! /tmp

Please input the number of rows of the matrix: 4

Please input the number of columns of the matrix: 4

Please input the matrix, entities seperated by blank:

7 0 4 18

4 0 1 1

15 7 11 -1

0 12 -2 0

The matrix (4 X 4):

7 0 4 18

4 0 1 1

15 7 11 -1

0 12 -2 0

Path: (2, 3) (1, 3) (0, 3) (0, 2) (1, 2) (2, 2) (2, 1) (3, 1) (3, 2)

Sum of apples: 54

#include <stringh>

#include <ctypeh>

void addNum(void)

{

#define NUMBUFFERSIZE 81

    int cntNum1 = 0, cntNum2 = 0, cntMax = 0,cntMix = 0;

    int index = 0,addUp = 0,cntSum = 0;

    int i = 0;

    char num1[NUMBUFFERSIZE] = { 0 }, num2[NUMBUFFERSIZE] = {0};

    char sum[NUMBUFFERSIZE] = { 0 };

    if (NULL == fgets(num1, NUMBUFFERSIZE, stdin))

        return;

    if (NULL == fgets(num2, NUMBUFFERSIZE, stdin))

        return;

    cntNum1 = strlen(num1) - 1;//去除'\n'

    cntNum2 = strlen(num2) - 1;//去除'\n'

    i = 0;

    while (i < cntNum1)

    {

        //

        if (!isdigit(num1[i]))

        {

            printf("num1[%d] = %c isn't a decimal-digit", i, num1[i]);

            return;

        }

            

        num1[i] -= 48;

        ++i;

    }

    i = 0;

    while (i < cntNum2)

    {

        //

        if (!isdigit(num2[i]))

        {

            printf("num2[%d] = %c isn't a decimal-digit", i, num2[i]);

            return;

        }

        num2[i] -= 48;

        ++i;

    }

    if (cntNum1 > cntNum2)

    {

        cntMax = cntNum1;

        cntMix = cntNum2;

        index = 1;

    }

    else

    {

        cntMax = cntNum2;

        cntMix = cntNum1;

        index = 2;

    }

    cntSum = cntMax;

    sum[cntSum] = '\n';

    sum[cntSum + 1] = '\0';

    addUp = 0;

    while (cntMix > 0)

    {

        --cntMax, --cntMix;

        if (1 == index)

        {

            sum[cntMax] = num1[cntMax] - num2[cntMix] + addUp;

            if (sum[cntMax] < 0)

            {

                sum[cntMax] += 10;

                addUp = -1;

            }

            else

            {

                addUp = 0;

            }

        }

        else

        {

            sum[cntMax] = num1[cntMix] - num2[cntMax] + addUp;

            if (sum[cntMax] < 0)

            {

                sum[cntMax] += 10;

                addUp = -1;

            }

            else

            {

                addUp = 0;

            }

        }

    }

    while (cntMax > 0)

    {

        --cntMax;

        if (1 == index)

        {

            sum[cntMax] = num1[cntMax] + addUp;

            if (sum[cntMax] < 0)

            {

                sum[cntMax] += 10;

                addUp = -1;

            }

            else

            {

                addUp = 0;

            }

        }

        else

        {

            sum[cntMax] = num2[cntMax] + addUp;

            if (sum[cntMax] < 0)

            {

                sum[cntMax] += 10;

                addUp = -1;

            }

            else

            {

                addUp = 0;

            }

        }

        

    }

    

    i = 0;

    while (i < cntSum)

    {

        //

        sum[i] += 48;

        ++i;

    }

    fputs((const char )sum, stdout);

}

这个是我以前无聊的时候编的,虽然和你的题目有点不一样,但是基本的功能都有,你拿去参考一下吧!

由于编的时候还没有学结构体,所以这里没有使用结构体……

#define N 6

/共有6个同学,在这里根据实际学生人数修改学生数/

#define M 4

/有3门课/

main()

{int score[N][M],i,j,sum[N]={0};

float average[M],sum1,sum2=00;

for(i=0;i<N;i++)

score[i][0]=i+1; /编学号/

for(j=0;j<N;j++)

{printf("Please input num %d's score!\n",j+1);

for(i=1;i<M;i++)

scanf("%d",&score[j][i]); /根据学号分别输入各位学生各科的成绩 /

}

for(i=0;i<N;i++)

for(j=1;j<M;j++)

sum[i]+=score[i][j]; /求每个人的成绩总分/

for(i=1;i<M;i++)

{sum1=00;

for(j=0;j<N;j++)

{sum1+=score[j][i];

sum2+=sum[j];

}

average[i-1]=sum1/N;

average[M-1]=sum2/N/(M-1); /求每门课及总分的平均分 /

}

printf("Num\tChinese\tEnglish\tMaths\tSum\n");

for(i=0;i<N;i++)

{for(j=0;j<M;j++)

printf("%d\t",score[i][j]);

printf("%d\n",sum[i]);

}

printf("\n");

printf("Average:");

for(i=0;i<M;i++)

printf("%3f\t",average[i]);

getch();

}

以上就是关于vb编写程序,计算s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)全部的内容,包括:vb编写程序,计算s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)、求助,用C语言并用回溯算法编写的有关蛇吃苹果走迷宫的程序,题目如下:、编写程序实现两个超长正整数(每个最长80位数字)的减法运算。c语言,求帮忙看看代码哪错了。。。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9439181.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存