【项目1:利用循环求和】
方法1
<pre name="code" class="cpp">#include<iostream.h>
void main()
{
int a(0),b(0),c(0);
while(b<1000)
{c=c+1;
b=2*c;
a=a+b;
}
cout<<a;
}
方法2
#include<iostream.h>
void main()
{
int c,a(0),b(0);
for(c=0;c<500;c++,b=2*c,a=a+b);
cout<<a;
}
方法3
#include<iostream.h>
void main()
{
int a(0),b(0),c(0);
do
{
c=c+1;
b=2*c;
a=a+b;
}
while(b<1000);
cout<<a;
}
【项目3:乘法口诀表】编程序,输出一个乘法口诀表,形如
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
……
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
for(a=1;a<=9;++a){
for(b=1;b<=a;++b){
c=a*b;
cout<<a<<"×"<<b<<"="<<c<<" ";
}
cout<<endl;
}
return 0;
}
项目3 ( 1 )百钱百鸡问题
#include <iostream>
int main()
{
int a,b,c,count=0;
for(a=1;a<=20;a++)
for(b=1;b<=33;b++)
for(c=1;c<=100;c++)
if(5*a+3*b+c/3==100&&a+b+c==100)
{
++count;
cout<<count<<": "<<a<<", "<<b<<", "<<c<<endl;
}
return 0;
}