轉載自:https://blog.csdn.net/xia0liang/article/details/53157455
【程序20】
題目:一球從100米高度自由落下,每次落地後反跳回原高度的一半;再落下,求它在第10次落地時,共經過多少米?第10次反彈多高?
1.程序分析:見下面註釋
2.程序源代碼:
#include "stdio.h"
#include "stdlib.h"
//#include <windows.h>
int main()
{
//SetConsoleOutputCP(437);
float s=100.0,h=s/2;
int n;
for (n = 2; n <= 10; n++)
{
s = s + 2 * h;
h = h / 2;
}
printf("the total of route is %f\n", s);
printf("the tenth height is %f meter\n", h);
system("pause");
}
【程序21】
題目:猴子喫桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個,第二天早上又將剩下的桃子喫掉一半,又多吃了一個。以後每天早上都吃了前一天剩下的一半零一個。到第10天早上想再喫時,見只剩下一個桃子了。求第一天共摘了多少。
1.程序分析:採取逆向思維的方法,從後往前推斷。
2.程序源代碼:
#include "stdio.h"
#include "stdlib.h"
//#include <windows.h>
int main()
{
//SetConsoleOutputCP(437);
int day, p1, p2;
day = 9;
p2 = 1;
while (day > 0)
{
p1 = (p2 + 1) * 2;
p2 = p1;
day--;
}
printf("the total peaches' number is %d\n", p1);
system("pause");
}
【程序22】
題目:兩個乒乓球隊進行比賽,各出三人。甲隊爲a,b,c三人,乙隊爲x,y,z三人。已抽籤決定比賽名單。有人向隊員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單。
1.程序分析:
2.程序源代碼:
#include "stdio.h"
#include "stdlib.h"
//#include <windows.h>
int main()
{
//SetConsoleOutputCP(437);
char i, j, k;//i是a的對手,j是b的對手,k是c的對手
for (i = 'x'; i <= 'z'; i++)
{
for (j = 'x'; j <= 'z'; j++)
{
if(i!=j)
for (k = 'x'; k <= 'z'; k++)
{
if (i != k&&j != k)
if (i != 'x'&&k != 'x'&&k != 'z')
printf("order is a--%c\tb--%c\tc--%c\n", i, j, k);
}
}
}
system("pause");
}
【程序23】
題目:打印出如下圖案(菱形)
*
***
******
********
******
***
*
1.程序分析:先把圖形分成兩部分來看待,前四行一個規律,後三行一個規律,利用雙重for循環,第一層控制行,第二層控制列。
2.程序源代碼:
#include "stdio.h"
#include "stdlib.h"
//#include <windows.h>
int main()
{
//SetConsoleOutputCP(437);
printf("*\n");
printf("***\n");
printf("******\n");
printf("********\n");
printf("******\n");
printf("***\n");
printf("*\n");
system("pause");
}
下面這個代碼是生成一個菱形
#include "stdio.h"
#include "stdlib.h"
//#include <windows.h>
int main()
{
//SetConsoleOutputCP(437);
int i, j, k;
for (i = 0; i <= 3; i++)
{
for (j = 0; j <= 2 - i; j++)
printf(" ");
for (k = 0; k <= 2 * i; k++)
printf("*");
printf("\n");
}
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= i; j++)
printf(" ");
for (k = 0; k <= 4 - 2 * i; k++)
printf("*");
printf("\n");
}
system("pause");
}