程序填空题(二)
1.尼科彻斯定理
this program is to verify theorem of nicoqish.that is the cube of any integer can be represented as the sum of some continue odd numbers.for example, 8^3=512=57+59+61+63+65+67+69+71.
#include <iostream>
using namespace std;
int main()
{
int n,a,i;
while(1)
{
cout<<"please input a integer to verify(0 to quit): ";
cin>>n;
if(n==0) __________; // (1)
// 输出等差数列,首项为a*a-a+1,公差为2,项数为n
a=n*n-n+1;
cout<<n<<"*"<<n<<"*"<<n<<"="<<n*n*n<<"="<<a;
for (i=1; __________;i++) // (2)
cout<<"+"<<__________; // (3)
cout<<endl;
}
return 0;
}
2.角谷猜想
this program is to verify jiaogu guess.that is given any natural number, if it is an even, divides 2, if it is an odd, multiple 3 and add 1, the result continues to be calculated analogously. after some times, the result is always 1.
#include <iostream>
using namespace std;
int main()
{
int n,a,i,cnt;
while(1)
{
cout<<"please input a integer to verify(0 to quit): ";
cin>>n;
if(n==0) __________break; // (1)
cnt=0;
cout<<" ------ results of verification: ------------\n";
do{
if(__________) // (2)
{
n=n*3+1;
cout<<"step no."<<++cnt<<":"<<(n-1)/3<<"*3+1="<<n<<endl;
}
else
{
n/=2;
cout<<"step no."<<++cnt<<":"<<2*n<<"/2="<<n<<endl;
}
} while(__________); // (3)
cout<<endl;
}
return 0;
}
3.四方定理
this program is to verify theorem of four squares.that is all natural numbers can be represented as sum of no more than 4 squares of the numbers.e.g., 123=7*7+7*7+4*4+3*3.
#include <iostream>
using namespace std;
int main()
{
int i,j,k,l,number;
while(1)
{
cout<<"please input a number to verify(0 to quit): ";
cin>>number;
if(number==0) __________; // (1)
cout<<" ------ results of verification: ------------\n";
for(i=1;i<=number/2;i++)
for(j=0;j<=i;j++)
for(k=0;k<=j;k++)
for(l=0;l<=k;l++)
if(__________) // (2)
{
cout<<number<<"="<<i<<"*"<<i<<"+"<<j<<"*"<<j<<"+"<<k<<"*"<<k<<"+"<<l<<"*"<<l<<endl;
goto exit;
}
exit: cout<<" ---------------------------------------------\n";
}
return 0;
}
4.亲密数
this is a program to find friendly numbers pair.which means the sum of integer a's all factors (except a) equals to the sum of integer b's all factors (except b)< e.g. sum of integer 220's all factors are:1+2+4+5+10+11+20+22+44+55+110=284,and sum of integer 284's all factors are:1+2+4+71+142=220>
#include <iostream>
using namespace std;
int main()
{
int a,i,b,n,m;
cout<<"please input the scale you want to find n: ";
cin>>n;
cout<<"\n there are following friendly--numbers pair smaller than "<<n<<endl;
for(a=1;a<n;a++)
{
for(__________;i<=a/2;i++) // (1)
if(!(a%i)) b+=i;
for(__________;i<=b/2;i++) // (2)
if(!(b%i)) m+=i;
if(__________&&a<b) // (3)
cout<<a<<".."<<b<<" ";
}
cout<<endl;
return 0;
}
5.自守数
this program will find the automorphic numbers.the defination of a automorphic number is: the mantissa of a natural number's square equals to itself. e.g., 5^2=25, 76^2=5776, 9376^2=87909376.
#include <iostream>
using namespace std;
int main()
{
int mul,number,k,kk;
for(number=0;number<10000;number++)
{
for(mul=number,k=1; __________;k*=10); // (1)
kk=k*10;
mul=number*number%kk;
if(__________) // (2)
cout<<number<<" ";
}
cout<<endl;
return 0;
}
6.特殊的四位数
this program will find the four figures which have the characteristic as follows: abcd=(ab+cd)^2. e.g., 3025=(30+25)*(30+25).
#include <iostream>
using namespace std;
int main() {
int n,a,b;
for(n=1000;n<10000;n++)
{
________________; // (1)
________________; // (2)
if((a+b)*(a+b)==n)
cout<<n<<" ";
}
cout<<endl;
return 0;
}
7.求方程cos(x)-x=0的根
this program is to find the real root of function cos(x)-x=0.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float x0,x1=0.0;
while(1)
{
________________; // (1)
________________; // (2)
if(fabs(x0-x1)<1e-6)
break;
}
cout<<"the real root is "<<x1<<endl;
return 0;
}
8.特殊的3位数
this program is to find the perfect square numbers. which have 3 digits, and 2 of them are the same. e.g.100, 121, 144, 225, 400, 441, 484, 676, 900.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int i,n,a,b,c;
for (i=10;i<=sqrt(1000);i++)
{
_______________; // (1)
a=n/100;
_______________; // (2)
c=n%10;
if (a==b || a==c || b==c)
cout<<n<<" ";
}
cout<<endl;
return 0;
}
9.三重回文数
this program is to find the palindrome numbers. whose square and cubic are also palindrome numbers.
#include <iostream>
using namespace std;
bool ispalindrome(int n)
{
int x,y=0;
x=n;
while (x!=0)
{
_______________; // (1)
_______________; // (2)
}
return y==n;
}
int main()
{
int m;
for(m=11;m<1000;m++)
{
if(ispalindrome(m) && ispalindrome(m*m) && ispalindrome(m*m*m))
{
cout<<m<<" "<<m*m<<" "<<m*m*m<<endl;
}
}
return 0;
}
10.six and nine
this program is to find the numbers of six and nine, which satisfy the formula six+six+six=nine+nine, where s,i,x,n,e stand for digits between 0 and 9.
#include <iostream>
using namespace std;
int main()
{
int i,x,a,b,c,d;
for(i=668;i<=999; _____________) // (1)
{
x=3*i/2;
a=i/10%10; b=x/100%10; c=x/1000; d=x%100/10;
if(____________________) // (2)
{
cout<<i<<" "<<x<<endl;
}
}
return 0;
}
或
#include <iostream>
using namespace std;
int main()
{
int s,i,x,n,e,six,nine;
for(s=1;s<10;s++)
for(i=0;i<10;i++)
for(x=0;x<10;x++)
for(n=1;n<10;n++)
for(e=0;e<10;e++)
{
six=_____________; // (3)
nine=_____________; // (4)
if(_____________) // (5)
cout<<six<<" "<<nine<<endl;
}
return 0;
}
11.马克思手稿中的数学题
this program is to solve an interesting math question in karl marx's manuscript.the problem is as follows: 30 persons spent 50 yuan in a restaurant, amony them, each man spent 3 yuan, each woman spent 2 yuan, and each child spent 1 yuan. the question is how many men, women and children are there?
#include <iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"men \twomen \tchildren"<<endl;
for(x=0;x<=10;x++)
{
________________; // (1)
________________; // (2)
if(__________________________) // (3)
cout<<x<<"\t"<<y<<"\t"<<z<<endl;
}
return 0;
}
12.百钱百鸡问题
this program is to solve problem of hundred yuan hundred fowls.which is presented by zhang qiujiang, a chinese ancient mathematician, in his work bible of calculation: 5 yuan can buy 1 cock,3 yuan can buy 1 hen, 1 yuan buy 3 chickens, now one has 100 yuan to buy 100 fowls, the question is how many cocks, hens, chickens to buy?
#include <iostream>
using namespace std;
int main()
{
int x,y,z;
for(x=0; _____________;x++) // 外层循环控制鸡翁数x (1)
for(y=0; _____________;y++) // 内层循环控制鸡母数y (2)
{
z=100-x-y;
if(_____________) // (3)
cout<<"cock="<<x<<", hen="<<y<<", chicken="<<z<<endl;
}
return 0;
}
13.三色球问题
this program is to solve problem of three color ball.the problem is as follows: there are 12 balls in the pocket.amony them, 3 balls are red,3 balls are white and 6 balls are black. now take out any 8 balls from the pocket,how many color combinations are there?
#include <iostream>
using namespace std;
int main()
{
int i,j,cnt=0;
cout<<"red \twhite \tblack"<<endl;
for(i=0; _____________;i++) // (1)
for(j=0; _____________;j++) // (2)
if(_____________) // (3)
cout<<++cnt<<" : "<<i<<"\t"<<j<<"\t"<<8-i-j<<endl
return 0;
}
14.配对新郎和新娘
this program is to solve problem of bridegroom and bride.the problem is as follows: someone goes to 3 couples lovers'wedding. the bridegrooms are a,b,c and the brides are x,y,z. he wants to know who marries who and asks them. a says he will marry to x, x says her fiance is c, c says he will marry to z. the man knows that they are all kidding. what they said is not true. so try to find who will marry to who?
#include <iostream>
using namespace std;
int main()
{
int x,y,z;
for(x=1;x<=3;x++) // 穷举x的全部可能配偶
for(y=1;y<=3;y++) // 穷举y的全部可能配偶
for(z=1;z<=3;z++) // 穷举z的全部可能配偶
if(x!=1&&x!=3&&z!=3&&x!=y&&x!=z&&y!=z)
{
cout<<" x will marry to "<<char(_____________)<<endl; // (1)
cout<<" y will marry to "<<char(_____________)<<endl; // (1)
cout<<" z will marry to "<<char(_____________)<<endl; // (1)
}
return 0;
}
15.邮票组合
this program is to solve problem of stamp combination.the problem is as follows. john has 4 stamps with value of 3 cents and 3 stamps with value of 5 cents. use one or more of these stamps, how many kinds of postages can john provide?
#include <iostream>
using namespace std;
int main()
{
int i,j,s,n=0,a[28]={0};
for(i=0;i<=4;i++)
for(j=0;j<=3;j++)
{
____________; // (1)
if (____________) { a[s]=1; n++; } // (2)
}
cout<<"there are "<<n-1<< " kinds of postages:\n";
for (i=1;i<=27;i++)
if (____________) cout<<i<<" "; // (3)
cout<<endl;
return 0;
}
参考答案:
1.(1)break (2)i<n (3)a+i*2
2.(1)break (2)n%2==1 (3)n!=1
3.(1)break (2)number==i*i+j*j+k*k+l*l
4.(1)b=0,i=1 (2)m=0,i=1 (3)m==a
5.(1)(mul/=10)>0 (2)number==mul
6.(1)a=n/100 (2)b=n%100
7.(1)x0=x1 (2)x1=cos(x0)
8.(1)n=i*i (2)b=n/10%10
9.(1)y=y*10+x%10 (2)x=x/10
10.(1)i=i+2 (2)a==b && c==d (3)s*100+i*10+x
(4)n*1000+i*100+n*10+e (5)3*six==2*nine
11.(1)y=20-2*x (2)z=30-x-y (3)3*x+2*y+z==50
12.(1)x<=20 (2)y<=33 (3)z%3==0&&5*x+3*y+z/3==100
13.(1)i<=3 (2)j<=3 (3)(8-i-j)<=6
14.(1)'a'+x-1 (2)'a'+y-1 (3)'a'+z-1
15.(1)s=i*3+j*5 (2)a[s]==0 (3)a[i]==1