19.1 students together to buy snacks, a total cost of 100 yuan, of which each college students spend 5 yuan, each high school students spend 3 yuan,//3 pupils spend 1 yuan, ask big, the number of pupils in the allocation of a total of how many different solutions//(Remove a class of students to zero solution)? This is the program I wrote, the answer is 3/* #include <stdio.h>int main () {int i,j,k;int count=0;for (i=1;i<20;i++) {for (j=1;j<33;j++ {for (k=3;k<300;k+=3) {if (5*i+3*j+k/3==100&&i+j+k==100) {printf ("University student%d, middle school%d, primary school%d", i,j,k); count++; printf ("\ n"); }}}}printf ("Altogether%d \ n", count); return 0;} *///This is the answer to the program, the answer is 6 kinds of #include<stdio.h>int main () {int a,b,c;for (a=1;a<=20;a++) for (b=1;b<=33;b++) {c=100- A-b;if ((5*A+3*B+C/3) ==100) printf ("%d,%d,%d\n", A,b,c);} return 0;} Please compare these two ways carefully,
20.s1=1,s2=1+3,s3=1+3+5,s4=1+3+5+7,......,sn=1+3+5+7......+ (2n-1),//n is a positive integer. The maximum value of n is programmed to calculate the value of S1+s2+s3+s4+.....+sn <20000. This is what I wrote, the answer is 38/* #include <stdio.h>int main () {int s[100]={0,1};//the array defined here is s[100],100 is a size I assume, I think, Within 100 certainly can arrive the answer, oneself experience int i,sum=0;for (i=2;i<100;i++) {s[i]=s[i-1]+2*i-1;} for (i=1;i<100;i++) {sum+=s[i];if (sum>=20000) {printf ("N Maximum value is%d\n", i-1); break;}} return 0;} *///This is the answer to the code, written with a function call, the answer is also 38#include<stdio.h>int func (int n) {int i,sum=0;for (i=1;i<=n;i++) sum=sum+2*i-1; return (sum);} int main () {int Sum=0,i=1;while (sum<20000) {sum=sum+func (i); if (sum>=20000) break;i++;} printf ("N Maximum value is%d\n", i-1); return 0;}
This article from "Peng Brother's Blog" blog, declined reproduced!
C Language Example series-------Day