076 使用while 语句求n!
3! = 3 * 2 *1, 5! = 5 * 4 * 3 * 2 * 1
n! = n * (n - 1) * … 2 * 1,使用while语句求n!
输入n =0或1 时的阶乘不可少!
输出的总和结果要定义为单精度或双精度型,如果定义整型容易溢出。
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int i = 1, n;
float sum = 1;
printf("输入n阶\n");
scanf("%d",&n);
if(n == 0 || n == 1)
{
sum = 1;
printf("%f\n", sum);
return 0;
}
while(i <= n)
{
sum *= i;
i++;
}
printf("%f\n", sum);
printf("\n");
}
077 使用while 为用户提供菜单显示
为了使用户可以方便观察到菜单选项,要将其菜单进行输出,利用while将菜单进行循环输出,这样可以使用户清楚知道每一项所对应操作
在开始时,需要将用户输入值设为非0 值,否则一开始while循环无法执行。
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int select = 1;
while(select != 0)
{
printf("------------ MENU-----------\n");
printf("-----sell-------1---\n");
printf("-----buy-------2---\n");
printf("-----showproduct---3---\n");
printf("-----out-----0---\n");
scanf("%d", &select);
switch(select)
{
case 1:
printf("you are buying something into store\n");
break;
case 2:
printf("you are selling to consumer\n");
break;
case 3:
printf("checking the store\n");
break;
case 0:
printf("the program is out\n");
break;
default:
printf("you put a wrong selection\n");
break;
}
}
printf("\n");
}
078 一元钱兑换
如果要将整钱换成零钱,一元可以换1角,2角,或5角。问有几种换法?
要确定每个角最多数目,最后总额在乘上相应金额。
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int i, j, k;
for(i = 0; i <= 10; i++)
{
for(j = 0; j <= 5; j ++ )
{
for(k = 0; k <= 2; k ++)
{
if(i + j * 2 + k * 5 == 10)
{
printf("一角:%d个,贰角:%d个,五角:%d个\n",i, j, k);
}
}
}
}
printf("\n");
}
079 特殊等式
有等式: xyz + yzz =532,求x,y,z值。(xyz和yzz分别3位数)
需要注意x和y属于百位不可为0;
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int x, y, z;
for(x = 1; x <= 9; x++)
{
for(y = 1; y <= 9; y ++ )
{
for(z = 0; z <= 9; z ++)
{
if( x * 100 + y * 10 + z + y * 100 + z * 10 + z == 532)
{
printf("x = %d y = %d z = %d\n", x, y, z);
}
}
}
}
printf("\n");
}
080 打印乘法口诀表
-第二个for循环判断条件需要小于等于第一个for循环变量
-第二个for循环中的printf("\n"),可以不用加判断语句,可以将其放在第一个for中执行:
for(x = 1; x <= 9; x++)
{
for(y = 1; y <= x; y ++ )
printf(" %d * %d = %d “, x, y, x * y);
printf(”\n");
}
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int x, y;
for(x = 1; x <= 9; x++)
{
for(y = 1; y <= x; y ++ )
{
printf(" %d * %d = %d ", x, y, x * y);
if(y == x)
{
printf("\n");
}
}
}
printf("\n");
}
081 平方和运算的问题
任意给一个自然数k,不为0,计算其各位数字的平方和k1,再计算k1的各位数字的平方和k2…,重复此过程,最终将得到数字1或145,此时再做数的平方和运算,最终将得到数字1或145,编写程序验证此过程。
-应用数组a将自然数各位数字保存在其中,巧妙利用变量i知道数字k几位数。
-goto语句实现输入为0,代码重新跳回,
-n值还未达到判断值时,需要初始化 i 值
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
long a[10], n, i;
p: printf("please input a number: \n");
scanf("%d",&n);
if(n == 0)
{
goto p;
}
while(n != 1 && n != 145) //判断条件
{
printf("n = ld ->", n);
i = 1;
while(n > 0)
{
a[i++] = n % 10;
n /= 10;
}
n = 0;
i--;
while(i >= 1)
{
printf("%ld * %ld", a[i], a[i]);
if(i > 1)
{
printf("+");
}
n += a[i] * a[i];
i--;
}
printf("= %ld\n", n);
}
//getch();
printf("\n");
}
082 求从键盘中输入字符的个数
要求输出用户从键盘中输入字符个数。
利用get()函数来获取键盘输入字符。
利用字符串以‘\0’结束特点,来计算键盘的字符数。
//———————————————————————— 官方 ——
利用计算长度时指针代表数组
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
int length(char *p)
{
int len = 0;
while(*p != '\0')
{
p++;
len++;
}
return len;
}
main()
{
int leng;
char str[100];
gets(str);
leng = length(str);
printf("len = %d\n",leng);
printf("\n");
}
//--------- ME ------------
利用数组。
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
int length(char str1[])
{
int len = 0, i = 0;
while(str1[i] != '\0')
{
i++;
len++;
}
return len;
}
main()
{
int leng;
char str1[100];
gets(str1);
leng = length(str1);
printf("len = %d\n",leng);
printf("\n");
}
ⅰ、自定义函数
-
无参函数的定义形式
-
有参函数的定义形式
-
空函数
ⅱ、函数参数
形式参数和实际参数 -
在定义函数时函数名后面括号中变量名称为形式参数
-
在主函数中调用一个函数时,函数名后面括号中的参数为实际参数。
ⅲ、函数的返回值
- 函数的返回值通过函数中的return获得,
- 在定义函数时要指定函数值类型
- 函数类型决定了返回值类型
- 明确表示“不带回值”,可以用“void”
083 打印杨辉三角
打印出下列的杨辉三角,要求打出10行
第一,确定二维数组
第二,先将第一列和最后一列的赋值为1
第三,确定对角线和的值公式:a[i][j] = a[i - 1][j -1] + a[i - 1][j];
第四, 输出该数组。
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int i, j;
int a[11][11];
for(i = 1; i < 11; i++)
{
a[i][i] = 1;
a[i][1] = 1;
}
for(i = 3; i < 11; i++)
{
for(j = 2; j <= i; j++)
{
a[i][j] = a[i - 1][j -1] + a[i - 1][j];
}
}
for(i = 1; i < 11; i++)
{
for(j = 1; j <= i; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
printf("\n");
}
084 求总数问题
集邮爱好者把所有邮票放在3个集邮册里面,在A册内存放全部的2/10,在B册放不知道全部的七分之几,在C册放303张,问这个集邮者总数是多少?以及每册几张?
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int sum = 0, x, a, b, c;
for(x = 1; x < 7; x ++)
{
if((10605 % (28- 5 * x) )== 0)
{
sum = 10605 / (28 - 5 * x);
printf("sum = %d\n", sum);
a = 2 * sum/ 10;
b = x * sum/ 7 ;
c = 303;
printf("a册 = %d\n", a);
printf("b册 = %d\n", b);
printf("c册 = %d\n", c);
}
}
printf("\n");
}
085 彩球问题
在一个袋子里装三色球,红色3,白色3,黑色6,问袋子这取出8个情况。
以三数和为8,命名红球和白球,黑球数以(8 - i - j) <= 6来判断
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int i, j, count;
puts("the result is :\n");
printf("time red ball white ball black ball\n");
count = 1;
for(i = 0; i <= 3; i++)
{
for(j = 0; j <= 3; j++)
{
if((8 - i - j) <= 6)
{
printf(" %3d%8d%9d%10d\n", count++, i, j, 8-i-j);
}
}
}
printf("\n");
}
用枚举法列举,判断条件,球和加起来为8
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int r, w, b;
for(r = 0; r <=3; r++)
{
for(w = 0; w <=3; w++)
{
for(b = 0; b <=6; b++)
{
if(r + w + b == 8)
{
printf("red =%d, white=%d black=%d\n", r, w, b);
}
}
}
}
printf("\n");
}
086 新同学年龄
班里来了新同学,同学问年龄时,他说:我的年龄的平方是3位数,立方是4位数,4次方是6位数,3次方和4次方正好用遍0-9这10个数,猜我年龄多大?
1、先确定该同学年龄的大致范围,在18-21(17的四次方是5位数,22的3次方是5位数)
2、将年龄的3次方和4次方的值分别保存在数组s中,方便判断其数值在0-9范围内,
3、判断数组s中的数值0-9各自次数,后(判断有无重复,或数字未出现)即须次数为1,且判断次数是第9次打印该年龄。
———————————————— 官方——————————————
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
long x = 18, a[10] = {0}, s[10] = {0}, i, n3, n4;
do
{
n3 = x * x * x;
for(i = 3 ; i >= 0; i--)
{
a[i] = n3 % 10;
a3 /= 10;
}
n4 = x * x * x * x;
for(i = 9 ; i >= 4; i--)
{
a[i] = n4 % 10;
a4 /= 10;
}
for(i = 0 ; i <= 9; i++) //保存数字存在次数
{
s[a[i]]++;
}
for(i = 0; i < 10; i++)
{
if(a[i] == 1)
{
if(i == 9)
{
printf("%d", x);
}
}
else
{
break;
}
}
x++;
}while(x < 22);
printf("\n");
}
———————————————————————— ME _______________
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
long x = 18, a[10], s[10], i, j, n2, n3, n4;
while(x <= 21)
{
n3 = x * x * x;
n4 = x * x * x * x;
for(i = 0 ; i < 4; i++)
{
s[i] = n3 % 10;
n3 = n3 / 10;
}
for(i = 4 ; i < 10; i++)
{
s[i] = n4 % 10;
n4 = n4 / 10;
}
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
if(s[i] == j)
{
a[i]++;
}
}
}
for(i = 0; i < 10; i++)
{
if(a[i] == 1)
{
if(i == 9)
{
printf("%d", x);
}
}
}
x++;
}
printf("\n");
}
087 灯塔数量
一层8层灯塔,每层灯数是上一层的2倍,共计765个灯。求最上层和最下层灯数。
1、假设第一层灯是x,先设为1,然后用死循环逐渐增加x的值,在灯总数sum为765时,打印跳出,
2、计算总数用for循环来计算
3、在每次更新第一层灯塔数的时候,要记得初始化sum=0,以及,最高层的灯数量为需要除2.
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int x = 1, i, y, sum = 0;
while(1)
{
y = x;
sum = 0;
for(i = 1; i <= 8; i++)
{
sum += y;
y = y * 2;
}
if(sum == 765)
{
printf("第一层: %d\n", x);
printf("第8层: %d", y / 2);
break;
}
x++;
}
printf("\n");
088 计算1的平方+2的平方+…+10的平方
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int i, sum = 0;
for(i = 1; i <= 10; i++)
{
sum += i * i;
}
printf("sum = %d", sum);
printf("\n");
}
089 循环显示随机数
用for随机显示10个随机数,其中产生随机数要使用到srand函数和rand函数。
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int i;
time_t t;
/* 初始化随机数发生器 */
srand((unsigned) time(&t));
/*
srand(i + 2);//也可以调用该于伪随机数生成算法播种。
*/
/* 输出 0 到 50 之间的 5 个随机数 */
for( i = 0 ; i < 10 ; i++ )
{
printf("%d\n", rand() % 50);
}
printf("\n");
}
090 卖西瓜
一农夫卖西瓜, 一共1020个,第一天卖一半多2个,第二天卖掉剩下的一半多2个,按此规律,这个农夫几天全部卖完?
判断条件sum > 0;
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int sum = 1020, day = 0;
while(sum > 0)
{
sum = sum - (sum /2 + 2);
day++;
}
printf("day = %d\n", day);
printf("\n");
}
091 银行存款问题
假设银行当前整存零取5年期的年利润为2.5 %,现在某人手里有一笔钱,预计在今后的5年当中每年年底取出1000,到底5年的时候刚好取完,计算在最开始存钱的时候要存多少钱?
1、由题可知, 最后一年为1000 ,则第四年为1000/ (1 + 0.025),用倒推法
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
float money, x = 1000;
int i;
for(i = 5; i >= 1; i--)
{
money = x / (1 + 0.025);
x = money + 1000;
}
printf("money = %f",money);
printf("\n");
}
092 统计不及格人数
假设一个班有20 人,输入某科考试成绩,然后统计该班不及格人数。
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
main()
{
int s, i, sum = 0;
for(i = 0; i < 20; i++)
{
scanf("%d", &s);
if(s < 60)
{
sum++;
}
}
printf("%d\n", sum);
printf("\n");
}
093 猜数字游戏
猜数字游戏具体要求如下:开始时应输入要猜的数字的位数,这样让算机可以根据输入的位数随机分配-一个符合要求的数据,计算机输出guess后便可以输入数字,注意数字间需用空格或回车符加以区分,计算机会根据输入信息给出相应的提示信息:
A表示位置与数字均正确的个数,
B表示位置不正确但数字正确的个数,
这样便可以根据提示信息进行下次输入,直到正确为止,这时会根据输入的次数给出相应的评作。
大致思路:
1、利用系统时钟作为随机数的种子,将其保存在数组a中;
2、在获取键盘输入的值保存在数组b中;
3、数组a与数组b的值进行比较,分别统计位置与数字均正确的个数与位置不正确但数字正确的个数
4,统计比较的次数,来进行排名。
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<dos.h>
void guess(int n)
{
int acount, bcount, i, j, k = 0, flag, a[10], b[10];
do
{
flag = 0;
srand((unsigned)time(NULL));
for(i = 0; i < n; i++)
{
a[i] = rand() % 10;
}
for(i = 0; i < n - 1; i++)
{
for(j = i + 1; j < n; j++)
{
if(a[i] == a[j]) //判断随机数中是否有相同值
{
flag = 1;
break;
}
}
}
}while(flag == 1);
do
{
k++; //记录猜数字的次数
acount = 0; //位置与数字均正确 个数
bcount = 0; // 位置不正确,数字正确个数
printf("guess:");
for(i = 0; i< n; i++) //输入值
{
scanf("%d", &b[i]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(a[i] == b[j])
{
acount++;
break;
}
if(a[i] == b[j] && i!=j)
{
bcount++;
break;
}
}
}
printf("clue on :%d A %d B \n\n", acount, bcount);
if(acount == n)
{
if(k == 1)
{
printf("you are the toprnost rung of fortune is ladder !!\n\n");
}
else if(k <= 5)
{
printf("you are genius!\n\n");
}
else if(k <= 10)
{
printf("you are cleaver!!\n\n");
}
else
{
printf("you need try hard!\n\n");
}
break;
}
}while(1);
}
main()
{
int i, n;
while(1)
{
printf("1, start game?(1:y /2: n)\n");
printf("2. Rule\n");
printf("3.exit\n");
printf("please choose:\n");
scanf("%d", &i);
switch(i)
{
case 1:
printf("please input n:\n");
scanf("%d", &n);
guess(n);
break;
case 2:
printf("\t\t The Rules of the games\n");
printf("step 1: input the number of digits\n");
printf("step 2: input the number the number ,separated by a space between two number \n");
printf("step 3: A represent location and data are correct\n");
printf("\t Brepresent location is correct but data is wrong !\n");
break;
case 3:
exit(0);
default:
break;
}
}
printf("\n");
}