给定一个整数n,任务是打印仅由0和1组成的数字,并且它们的总和等于整数n。
仅包含0和1的数字是1、10 , 11 所以我们必须打印所有可以相加得到等于 n 的数字。
就像,我们输入 n = 31 那么答案可以是 10+10+11 或 10+10 +10+1
示例input: 31output:10 10 10 1
算法int findnumbers(int n)startstep 1: declare and assign varaibales m = n % 10, a = nstep 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 ifend loopstop
示例#include <stdio.h>// function to count the numbersint 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 codeint main(){ int n = 35; findnumbers(n); return 0;}
输出如果我们运行上面的程序,它将生成以下输出:
10 10 1 1 1 1 11
以上就是在c程序中,打印只包含数字0和1的数,使它们的和为n的详细内容。