주어진 합계를 얻기 위해 더할 수 있는 요소를 인쇄합니다.

PHPz
풀어 주다: 2023-09-07 22:09:15
앞으로
1138명이 탐색했습니다.

주어진 합계를 얻기 위해 더할 수 있는 요소를 인쇄합니다.

사용자가 입력하려는 요소 수를 입력한 다음, 주어진 요소 목록에서 사용자가 계산하려는 총 값을 입력하세요.

Input : N=5
   Enter any 5 values : 3 1 6 5 7
   Enter sum you want to check : 10
Output : 3 1 6
로그인 후 복사

Algorithm

START
STEP1-> Take values from the user
STEP2-> Take the sum a user want to check in the set.
STEP3-> For i = 0; i < n; i++
STEP4-> Check If sum - *(ptr+i) >= 0 then,
   STEP4.1-> sum -= *(ptr+i);
   STEP4.2-> Print the value of *(ptr+i)
END If
END For
STOP
로그인 후 복사

Example

#include <stdio.h>
int main(int argc, char const *argv[]){
   int *ptr, n, i, sum;
   printf("Enter number of digits you want to enter");
   scanf("%d", &n);
   ptr = (int*)malloc(sizeof(int)*n); //Dynamically allocating the memory of int
   type
   printf("Enter %d elements", n);
   for(i = 0; i < n; i++) {
      scanf("%d", (ptr+i)); //Inputting the value in dynamically
      //allocated array
   }
   printf("Enter the sum you want to check");
   scanf("%d", &sum);
   for ( i = 0; i < n; i++) {
      if(sum - *(ptr+i) >= 0) { //Checking the values which can be added to form the sum
         X
         sum -= *(ptr+i); //Updating the value of sum
         printf("%d ", *(ptr+i)); //Printing the Values which can be summed up to form sum
      }
   }
   return 0;
}
로그인 후 복사

Output

위 프로그램을 실행하면 다음과 같은 출력이 생성됩니다

Enter number of digits you want to enter
5
Enter 5 elements
3
1
6
5
7
Enter the sum you want to check
10
3 1 6
로그인 후 복사

위 내용은 주어진 합계를 얻기 위해 더할 수 있는 요소를 인쇄합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:tutorialspoint.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!