发现的总数组合使用三个数的整数

编程入门 行业动态 更新时间:2024-10-10 17:33:50
本文介绍了发现的总数组合使用三个数的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于一个给定的整数, N 的,我需要打印长度为3的所有列出了总和的 N 的。所述列表的成员必须为非负整数。打印所有这些名单后,必须再打印中发现,清单的数量。

For a given integer, n, I need to print all the lists of length 3 which sum to n. The members of the list must be non-negative integers. After printing all these lists, it must then print the number of lists that were found.

例如,如果的 N = 2 的:

  • 1 + 0 + 1 = 2
  • 1 + 1 + 0 = 2
  • 0 + 1 + 1 = 2
  • 2 + 0 + 0 = 2
  • 0 + 0 + 2 = 2
  • 0 + 2 + 0 = 2

下面是我为长度为2的列表,而不是长度3列出了程序:

Here is the program I did for lists of length 2 rather than lists of length 3:

#include <stdio.h> int main (void) { int total; int c1=0; int c2=0; int c3=0; int count; printf("Welcome friends and mainly enemies to the thingy. Only postive intergers!!!\n That's right just enter a number here:"); scanf ("%d",&total); printf ("\n\n\nWhy pick %d? Here is the list if combinations anyway,",total); for (count=0;count<=total;count++) { printf ("\n"); for (c1=count;c1==count;c1--) { printf("%d ",c1); } for (c2=total-count;c2<=total-count;c2++) { printf("%d",c2); } } printf ("\n\nThere are %d number combinations that total %d",count,total); }

的目标是从长度的两个列表扩展这对长度为3的列表。

The goal is to extend this from lists of length two to lists of length 3.

附加信息: 我只能用另外一个变量C3。

Additional Info: I can only use one other variable c3.

推荐答案

希望这有助于:

int c3 = 0; for (int c1 = 0; c1 <= total; c1++) { for (int c2 = total - c1; c2 >= 0; c2--){ printf("%d %d %d \n", c1, c2, total - c1 - c2); c3++; } } printf("there are %d ways to sum to %d", c3, total);

更多推荐

发现的总数组合使用三个数的整数

本文发布于:2023-11-29 09:55:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646062.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   整数   个数   总数   发现

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!