
给定一个整数n,任务是打印仅由0和1组成的数字,并且它们的总和等于整数n。
仅包含0和1的数字是1、10 , 11 所以我们必须打印所有可以相加得到等于 n 的数字。
就像,我们输入 n = 31 那么答案可以是 10+10+11 或 10+10 +10+1
Input: 31 Output:10 10 10 1
int findNumbers(int n)
START
STEP 1: DECLARE AND ASSIGN VARAIBALES m = n % 10, a = n
STEP 2: LOOP WHILE a>0
IF a/10 > 0 && a > 20 THEN,
SUBTARCT 10 FROM a AND STORE BACK IT IN a
PRINT "10 "
ELSE IF a-11 == 0 THEN,
SUBTRACT 11 FROM a AND STORE
BACK IN a
PRINT "11 "
ELSE
PRINT "1 "
DECREMENT a BY 1
END IF
END LOOP
STOP#include <stdio.h>
// Function to count the numbers
int findNumbers(int n){
int m = n % 10, a = n;
while(a>0){
if( a/10 > 0 && a > 20 ){
a = a-10;
printf("10 ");
}
else if(a-11 == 0 ){
a = a-11;
printf("11 ");
}
else{
printf("1 ");
a--;
}
}
}
// Driver code
int main(){
int N = 35;
findNumbers(N);
return 0;
}如果我们运行上面的程序,它将生成以下输出:
10 10 1 1 1 1 11
以上就是在C程序中,打印只包含数字0和1的数,使它们的和为N的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号