首页
/
每日頭條
/
生活
/
c語言程序設計練習題及答案
c語言程序設計練習題及答案
更新时间:2024-05-16 16:50:00

c語言程序設計練習題及答案?/* 第一題:n為一個整數(小于10萬),它加上100後是一,現在小編就來說說關于c語言程序設計練習題及答案?下面内容希望能幫助到你,我們來一起看看吧!

c語言程序設計練習題及答案(C語言程序設計上機程序題)1

c語言程序設計練習題及答案

/* 第一題:n為一個整數(小于10萬),它加上100後是一

個完全平方數,再加上168又是一個完全平方數,請問該數是多少?

#include<iostream>

#include<cmath>

using namespace std;

int judge(int a)

{

int b=sqrt(a);

if(a==b*b)

return 1;

else

return 0;

}

void main()

{

int i,t;

for(i=-99;i<100000;i )

if

(judge(i 100)&&judge(i 168))

cout<<i<<endl;

}*/

/*第二題:求1000之内的完全數。

說明:完全數就是:除了它本身以外所有因子之和等于其

本身,例如:6=1 2 3。

#include<iostream>

using namespace std;

void main()

{

int a,b,sum=0;

for(a=1;a<=1000;a )

{

for(b=1;b<=a/2;b )

{

if(a%b==0)

sum=sum b;

}

if(sum==a)

cout<<a<<endl;

sum=0;

}

}

*/

/*第三題:求出 [900, 1000]間有偶數個因子(這裡因子不

含1和自身)的數有多少個,其中最小的一個?

#include<iostream>

using namespace std;

void main()

{

int a,b,c=0,d=0,min;

for(a=1000;a>=900;a--)

{

for(b=2;b<=a/2;b )

{

if(a%b==0)

c;

}

if(c%2==0)

{

d;

min=a;

}

}

cout<<"min="<<min<<"一共有:"<<d<<endl;

}

*/

/*第四題:編寫一個自定義函數:int f( int M, int N) ,f( )的功能是:

對給定的正整數M和N,找出滿足方程"7x 4y=M"的正整數解中x是偶數且y

是奇數的解,其中:x,y屬于[1,N]的範圍。

要求:若M和N不都是正整數,則結束函數并返回-1;隻有M和N都是正整數

時,才繼續求解操作,用二重循環進行求解:

(1)在函數中輸出滿足條件的正整數解x和y

(2)并且使函數值返回滿足條件的正整數解的組數

#include<iostream>

using namespace std;

int f(int M,int N)

{

int x,y,k=0;

if(M<0||N<0)

{cout<<"請輸入正整數";

return -1;}

{for(x=2;(M-7*x>0)&&N>=x;x =2)

{

for(y=1;N>y;y =2)

{

if(M-7*x-4*y==0)

{cout<<"x="<<x<<" y="<<y<<endl;

k;}

}

cout<<"k="<<k;

return k;}}

}

void main()

{

int M,N;

cout<<"請輸入整數M:";

cin>>M;

cout<<"請輸入整數N:";

cin>>N;

f(M,N);

}

*/

/*第五題:編寫一個自定義函數:int f( char x, int N) ,f( )的功

能是:對給定的字符c和整數N,用c代表的符号打印一個N行的圖案,每

行開頭沒有任何空格。比如,當c為"*"且N為5時,打印的圖案如本題圖

所示。且函數值返回1。

#include<iostream>

using namespace std;

int f(char x,int n)

{

int i,j;

for(i=1;i<=n;i )

{

for(j=1;j<=i;j )

cout<<x;

cout<<endl;

}

return 0;

}

int main()

{

f('*',5);

return 0;

}*/

/*第六題:編寫一個自定義函數:int f( int N) ,f( )的功能是:

對給定的正整數N,打印一個N行N列的由"A"、"B"、"C"以及空格" "符

号組成的方陣圖案。比如,當N為5時,打印的圖案如本題圖所示。且

函數值返回1。

#include<iostream>

using namespace std;

int f(int N)

{

int i,j;

char a[100][100];

for(i=0;i<=2*N-1;i )

{

for(j=0;j<=2*N-1;j )

{

if((j==0&&i%2==0)||(i==j&&i%2==0))

a[i][j]='A';

else if(j>i&&i%2==0&&j%2==0)

a[i][j]='B';

else if(j%2!=0||i%2!=0)

a[i][j]='\t';

else

a[i][j]='C';

}

cout<<a[i][j];

}

return 1;

}

void main()

{

f(5);

}

*/

/*第七題:根據下列要求和提示,編寫一個函數f(N)判斷某數N是

否為"完全數",N由主函數給出;如果N是"完全數"則函數值返回1,

N不是"完全數"則函數值返回0。"完全數"定義如下:如果一個數恰

好等于它的因子之和(因子包括1,不包括數本身),則稱該數為

"完全數"。如:6的因子是1、2、3,而6=1 2 3,則6是個"完全數"。

#include<iostream>

using namespace std;

int f(int N);

void main()

{

int N;

cin>>N;

if(f(N)==1)

cout<<N<<"是完全數"<<endl;

else

cout<<N<<"不是完全數"<<endl;

}

int f(int N)

{

int a,sum=0;

for(a=1;a<=N/2;a )

{

if(N%a==0)

sum =a;

}

if(N==sum)

return 1;

else

return 0;

}

*/

/*第八題:編寫程序,求e的值,e≈1 1/1! 1/2! 1/3! 1/4! ...,

最後一項的值小于1e-6。

#include<iostream>

using namespace std;

int jiecheng(int N)

{

int i,c=1;

for(i=1;i<=N;i )

c=c*i;

return c;

}

void main()

{

double e=1.0;

int t;

for(t=1;(jiecheng(t))<=100000000;t )

e=e 1.0/jiecheng(t);

cout<<e<<endl;

}*/

/*第九題:有十進制數字a,b,c,d和e,求滿足式子:abcd*e=dcba

(a非0,e非0非1)的四位數中:

(1)共有多少個?

(2)最小的abcd;

(3)與之相對應的e。

#include<iostream>

using namespace std;

int f(int n)

{int m,sum=0;

while(n)

{

m=n;

sum=sum*10 m;

n=n/10;

}

return sum;

}

void main()

{ int i,j=0,k;

for(i=4999;i>=1000;i--)

{

if(f(i)/i*i==f(i)&&f(i)>i)

{

j ;

k=i;

}

}

cout<<j<<endl;

cout<<k<<endl;

cout<<f(k)/k;

}

*/

/*第十題:有一個7層燈塔,每層所點燈數都等于該層上一層

的兩

倍,燈的總數是381盞,求:塔底燈數?第幾層的燈數為48?

#include<iostream>

using namespace std;

int f(int n)

{

if(n==1)

return 1;

else

return 2*f(n-1);

}

void main()

{ int i,x,sum=0;

for(x=1;x<100;x )

{

for(i=1;i<=7; i)

{sum =x*f(i);}

if(sum!=381)

sum=0;

else

cout<<"x="<<x<<endl;

}

}

*/

/*第十一題:有3個紅球,5個白球,6個黑球,從中任意取出

8個球,

且其中必須有白球,請求出共有多少種取法?

#include<iostream>

using namespace std;

int main()

{

int x,y,sum=0;

for(x=1;x<=5;x )

{

for(y=0;y<=3;y )

{ if((8-x-y)<=6)

sum ;

}

}

cout<<sum;

return 0;

}

*/

/*第十二題:求數列1,3,3,3,5,5,5,5,5,7,7,7

,7,7,7,7……。求:

第40項的值;值為17的第1個數是數列中第幾項?

#include<iostream>

using namespace std;

void main()

{

int i,sum=0;

for(i=1;sum<=40;i =2)

sum =i;

i-=2;

cout<<i<<endl;

sum=0;

for(i=1;i<17;i =2)

sum =i;

cout<<sum 1<<endl;

}

*/

/*第十三題:計算p的近似值,直到最後一項的絕對值小于

10-8為止,近似公式為

#include<iostream>

using namespace std;

int f(int N)

{

if(N%2==0)

return (-1);

else

return 1;

}

void main()

{

int i,c=0;double p=0;

for(i=1;i<100000000;i =2)

{

c=c 1;

p =1.0/i*f(c);

}

cout<<p<<endl;

}

*/

//第十四題:用牛頓疊代法求方程3x3-4x2-5x 13=0在x=1

附近的根,要求精度為10-6。

/*

#include<iostream>

#include<iomanip>

using namespace std;

double y(double x)

{

double y;

y=3*x*x*x-4*x*x-5*x 13;

return y;

}

double k(double x)

{

double y;

y=9*x*x-8*x-5;

return y;

}

void main()

{

double x=1.0;

while(1)

{

int a=(x-y(x)/k(x))*1000000;

int b=x*1000000;

if(a==b)

{

cout<<setprecision(7)<<x;

break;

}

else

x=x-y(x)/k(x);

}

}

*/

/*第十五題:編寫一個自定義函數:int f( int M, int N)

,函數f的功能是:對給定的正整數M和N,

找出滿足方程"5x 6y=M"的正整數解中x和y都是偶數的

解。要求:若M和N不都是正整數,

則結束函數并返回-1;隻有M和N都是正整數時,且

x,y∈[1,N], 才繼續求解操作,

并用二重循環進行求解:(1)在函數中輸出滿足條件的

正整數解x和y,

(2)使函數值返回滿足條件的正整數解的組數。

#include<iostream>

using namespace std;

int f(int M,int N)

{

int x,y,k=0;

if(M<0||N<0)

{

cout<<"請輸入正整數";

return -1;

}

{

for(x=2;(M-5*x>0)&&N>=x;x =2)

{

for(y=2;N>y;y =2)

{

if(M-5*x-6*y==0)

{

cout<<"x="<<x<<" y="<<y<<endl;

k;

}

}

}

cout<<"k="<<k;

return k;

}

}

void main()

{

int M,N;

cout<<"請輸入整數M:";

cin>>M;

cout<<"請輸入整數N:";

cin>>N;

f(M,N);

}

*/

/*第十六題:求s=2 22 222 ... 222...2的和,即第n

個加數是一個n位的整數,

且各數位上的數字均為2,例如當n=4時,s=2 22

222 2222。

要求設計一個函數int f(int n)求每一個加數,在main()中用累

加求和方式求出總和。

#include<iostream>

using namespace std;

int f(int N)

{

int i,p=0;

for(i=1;i<=N;i )

p=p*10 2;

return p;

}

int main()

{

int s=0,i,n;

cout<<"input n:";

cin>>n;

for(i=1;i<=n;i )

s=s f(i);

cout<<"2 22 222''''''="<<s<<endl;

return 0;

}

*/

//第十七題:所有能被13整除又能被17整除且

末位不是偶數的三位數有幾個?最大的一個?

//如何處理多個數中輸出最大的

/*

#include<iostream>

using namespace std;

int main()

{

int i=100,j=0,a;

for(;i<=999;i )

{

if(i==0&&i==0&&i%2!=0)

{ a=i;

j ;}

}

cout<<a<<" "<<j<<endl;

return 0;

}

*/

//第十八題:基本解決,有兩個疑問,為何不能執行

,為何寫1.0

/*

#include<iostream>

using namespace std;

double f(int n)

{

if(n==1)

return 1.0;

else if(n==2)

return 2.0;

else

return f(n-1) f(n-2);

}

void main()

{

double sum=0;

int i;

for(i=1;i<=20;i )

sum =f(i)/f(i 1);

cout<<sum<<endl;

cout<<f(i)/f(i 1);

}*/

*/

/*第二十題:從鍵盤上輸入一個正整數,判别它是否為

一回文數。如:123321

#include<iostream>

using namespace std;

void main()

{

int a,b,c=0,d;

cout<<"請輸入正整數"<<endl;

cin>>d;

b=d;

while(b)

{

a=b;

c=c*10 a;

b=b/10;

}

if(d==c)

cout<<"yes";

else

cout<<"NO";

}

*/

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
洗臉巾怎麼用
洗臉巾怎麼用
1、用法一:代替毛巾,用作洗臉具體做法為:全臉在豐富的泡沫洗面奶充分清洗之後,取一張洗臉巾打濕,輕輕...
2024-05-16
蝦片做法是什麼
蝦片做法是什麼
1、先把油加鍋裡,注意不要太多,别太少了。主要是你一次不會炸太多的,适中就好。2、把油燒熱,可以先放一個試試,放進去就開始炸開花了那就可以炸了,要不就在等會。3、開始放蝦片了,一次少放點,掌握不好的放個5.6片都可以了,不要太多!因為炸得很快的,幾秒龍蝦片變大,變色,就炸開了,這個時候用筷子翻一下。...
2024-05-16
1986年2月28陽曆是
1986年2月28陽曆是
1、1986年2月28陽曆是1986年4月6日,公元1986年,公曆平年,共365天,53周。農曆丙...
2024-05-16
姓楊女孩名字
姓楊女孩名字
1、楊绮绮、楊珊菲、楊熙芳、楊琳芳2、楊娴楚、楊愫瑜、楊桐瑛、楊心雅3、楊莉桐、楊雯楚、楊素嘉、楊華...
2024-05-16
廣饒縣美食
廣饒縣美食
廣饒縣美食有廣饒肴驢肉、虎頭雞、黃河口鼈、丁莊風味排骨、廣饒草莓、石村麻辣魚等。1、廣饒肴驢肉:世代相傳,一直保持很好的聲譽,繼承了古老的傳統工藝,肴制的驢肉深香馥郁、脍炙人口。2、虎頭雞:虎頭雞,又叫松雞,當地人習慣稱“炸雞”。最早起源于山東省廣饒縣大碼頭一帶,是大碼頭鎮名吃之一,已有400多年的曆史。虎頭雞其色金黃,其味濃郁香醇,口感酥爽鮮嫩,其制作包括炸雞塊和焖炖兩部分,炸制後易長時間保存,
2024-05-16
Copyright 2023-2024 - www.tftnews.com All Rights Reserved