1.
#include <stdio.h>
int rather(int,int);
int main(){
int m
,n
,min
;
printf("enter two numbers:");
scanf("%d %d",&m
,&n
);
min
= rather(n
,m
-n
);
long long int fact
=1,facc
=1;
for(int i
=1;i
<=min
;i
++){
fact
*=i
;
facc
*=(m
-i
+1);
}
printf("%d",facc
/fact
);
return 0;
}
int rather(int a
,int b
){
int flag
;
if (b
<=a
){
flag
=b
;
}else{
flag
=a
;
}
return flag
;
}
第一次,按老师要求,化简公式,取long long int 型,可以使m达到15.
#include <stdio.h>
double factorial(int);
int main(){
double m
,n
;
double result
;
printf("enter two numbers:");
scanf("%lf %lf",&m
,&n
);
result
= factorial(m
)*1.0/(factorial(n
)*factorial(m
-n
));
printf("%.lf",result
);
return 0;
}
double factorial(int k
){
double fact
=1;
for(int i
=1;i
<=k
;i
++){
fact
*=i
;
}
return fact
;
}
第二次,直接把变量定义为double,可以算到m=170次方,算是极大地提升。
如果把第一种公式的化简应用到第二种里面,应该范围可以更大。
#include <stdio.h>
double rather(double,double);
int main(){
long double m
,n
,min
;
printf("enter two numbers:");
scanf("%lf %lf",&m
,&n
);
min
= rather(n
,m
-n
);
double fact
=1,facc
=1;
for(int i
=1;i
<=min
;i
++){
fact
*=i
;
facc
*=(m
-i
+1);
}
printf("%.lf",facc
/fact
);
return 0;
}
double rather(double a
,double b
){
int flag
;
if (b
<=a
){
flag
=b
;
}else{
flag
=a
;
}
return flag
;
}
既化简了公式,又取变量为double类型,可以计算达到m=266,n=133,位数达到79位。
2.
(2)
#include <stdio.h>
int check(int,int,int);
int main(){
int x
,y
,n
;
printf("please enter three numbers:");
scanf("%d %d %d",&x
,&y
,&n
);
printf("%d",check(x
,y
,n
));
return 0;
}
int check(int x
,int y
,int n
){
int flag
=0;
if ((x
>=0&&x
<n
)&&(y
>=0&&y
<n
)){
flag
=1;
}
return flag
;
}
(5)
#include <stdio.h>
int num_digits(int);
int main(){
int num
;
printf("please enter a number:");
scanf("%d",&num
);
printf("the number has %d digits.",num_digits(num
));
return 0;
}
int num_digits(int x
){
int cnt
=0;
while (x
>0){
x
/=10;
cnt
++;
}
return cnt
;
}
(6)
#include <stdio.h>
int digit(int, int);
int main(){
int n
,k
;
printf("enter two numbers:");
scanf("%d %d",&n
,&k
);
printf("第%d位数字是%d",k
,digit(n
,k
));
return 0;
}
int digit(int n
,int k
){
int cnt
=1;
int flag
=0;
while (n
>0){
if(cnt
==k
){
flag
= n
%10;
break;
}
n
/=10;
cnt
++;
}
return flag
;
}
3.
(5)
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int pinzi(char[]);
int main(){
int i
,j
;
char word
[50];
printf("Enter a word:");
scanf("%s",&word
);
printf("Scrabble value:%d",pinzi(word
));
return 0;
}
int pinzi(char word
[50]){
int i
,len
,value
=0;
len
= strlen(word
);
for(i
=0;i
<len
;i
++){
char ch
=toupper(word
[i
]);
if (ch
=='A'||ch
=='E'||ch
=='I'||ch
=='L'||ch
=='N'||ch
=='O'||ch
=='R'||ch
=='S'||ch
=='T'||ch
=='U')
value
++;
else if(ch
=='D'||ch
=='G')
value
+=2;
else if(ch
=='B'||ch
=='C'||ch
=='M'||ch
=='P')
value
+=3;
else if(ch
=='F'||ch
=='H'||ch
=='V'||ch
=='W'||ch
=='Y')
value
+=4;
else if(ch
=='K')
value
+=5;
else if(ch
=='J'||ch
=='X')
value
+=8;
else if(ch
=='Q'||ch
=='Z')
value
+=10;
}
return value
;
}
(14)
#include <stdio.h>
#include <math.h>
double square(double);
int main(){
double x
;
printf("Enter a positive number:");
scanf("%lf",&x
);
printf("%lf",square(x
));
return 0;
}
double square(double x
){
double y
=1;
double m
,t
;
do{
m
=y
;
t
=x
/y
;
y
=(y
+x
/y
)/2;
}while(fabs(m
-y
)>=10e-5 *y
);
return y
;
}
4.
(2)
#include <stdio.h>
#include <math.h>
double dist(double,double);
double angel(double,double);
int main(){
double x
,y
;
printf("please enter two numbers:");
scanf("%lf %lf",&x
,&y
);
printf("the distance is %.2lf\n",dist(x
,y
));
printf("the angel is %.2lf\n",angel(x
,y
));
return 0;
}
double dist(double x
,double y
){
return sqrt(x
*x
+y
*y
);
}
double angel(double x
,double y
){
return atan(y
/x
);
}
5.
(5)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
int flip(int);
void percentages(int,int);
int main(){
int heads
;
int numTosses
= 1000;
double sumperheads
=0,sumpertails
=0;
srand(time(NULL));
for(int i
=0;i
<20;i
++){
heads
= flip(numTosses
);
percentages(numTosses
,heads
);
sumperheads
+=(float)heads
/numTosses
*100.0;
sumpertails
+=(float)(numTosses
-heads
)/numTosses
*100.0;
}
printf("\n\nsumHeads came up %6.2f percent of the time.\n",sumperheads
/20.0);
printf("sumTails came up %6.2f percent of the time.\n",sumpertails
/20.0);
return 0;
}
int flip(int numTimes
){
int randValue
;
int heads
=0;
int i
;
for(i
=1;i
<=numTimes
;i
++){
randValue
=1+rand()%100;
if (randValue
>50)
heads
++;
}
return heads
;
}
void percentages(int numTosses
,int heads
){
int tails
;
float perheads
;
if(numTosses
==0){
printf("There were no tosses, so no percentages can be calculated.\n");
}else {
tails
=numTosses
-heads
;
printf("Number of coin tosses:%d\n",numTosses
);
printf(" Heads:%d Tails:%d\n",heads
,tails
);
perheads
=(float)heads
/numTosses
*100.0;
printf("Heads came up %6.2f percent of the time.\n",perheads
);
printf("Tails came up %6.2f percent of the time.\n",100-perheads
);
}
}
(6)
(a)
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
int seed
,number
;
srand(time(NULL));
seed
=1+rand()%100;
printf("the seed is %d\n",seed
);
int cnt
=7;
while (cnt
>0){
printf("enter a number:");
scanf("%d",&number
);
cnt
--;
if (number
==seed
){
printf("Hooray,you have won!");
break;
}else {
if (number
>seed
){
printf("Wrong Number,Try Again\n");
printf("the number is too big!,you has %d chances\n",cnt
);
}else{
printf("Wrong Number,Try Again\n");
printf("the number is too small!,you have %d chances\n",cnt
);
}
}
}
if (cnt
==0){
printf("Sorry,you lose\n");
printf("the correct number is %d\n",seed
);
}
return 0;
}
(b)
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void game(void);
int main(){
game();
char choice
;
while (1) {
rewind(stdin);
printf("enter a letter:");
scanf("%c",&choice
);
if (choice
=='y'||choice
=='Y'){
game();
}
else{
break;
}
}
return 0;
}
void game(){
int seed
,number
;
srand(time(NULL));
seed
=1+rand()%100;
printf("the seed is %d\n",seed
);
int cnt
=7;
while (cnt
>0){
printf("enter a number:");
scanf("%d",&number
);
cnt
--;
if (number
==seed
){
printf("Hooray,you have won!");
break;
}else {
if (number
>seed
){
printf("Wrong Number,Try Again\n");
printf("the number is too big!,you has %d chances\n",cnt
);
}else{
printf("Wrong Number,Try Again\n");
printf("the number is too small!,you have %d chances\n",cnt
);
}
}
}
if (cnt
==0){
printf("Sorry,you lose\n");
printf("the correct number is %d\n",seed
);
}
printf("Would you like to play again(y/n)?\n");
}
第二次用函数来实现,需要注意的细节还是比较多的。
(7)
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void shuzu(int,int count
[10]);
int main(){
int i
=0,j
=0;
int count
[10]={0};
shuzu(i
,count
);
while(j
<10){
printf("the %d is %d percent.\n",j
+1,count
[j
]);
j
++;
}
return 0;
}
void shuzu(int i
,int count
[]){
int seed
;
srand(time(NULL));
while(i
<100){
seed
=1+rand()%10;
count
[seed
-1]++;
i
++;
}
}
当函数需要数组作为参数的时候,在这道题里面写成int count[]是对的,写成int count[10]也是对的,写成int *count也是对的。
但是写成int count确实错的。
(8)
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int game1(int,int,int);
int game2(int,int,int);
int game3(int,int,int);
int main(){
int shai1
,shai2
;
int game
;
int money
=50;
srand(time(NULL));
while (money
>0){
printf("please select a game:");
scanf("%d",&game
);
printf("please roll two dice\n");
shai1
=1+rand()%6;
shai2
=1+rand()%6;
switch (game
){
case 1:
money
= game1(shai1
,shai2
,money
);
break;
case 2:
money
= game2(shai1
,shai2
,money
);
break;
case 3:
money
= game3(shai1
,shai2
,money
);
break;
}
}
return 0;
}
int game1(int shai1
,int shai2
,int money
){
if ((shai1
+shai2
==7)||(shai1
+shai2
==11)){
printf("the shai1=%d,the shai2=%d\n",shai1
,shai2
);
printf("you won!\n");
}else{
money
-=(shai1
+shai2
);
printf("the shai1=%d,the shai2=%d\n",shai1
,shai2
);
printf("you lose,the money has %d.\n",money
);
}
return money
;
}
int game2(int shai1
,int shai2
,int money
){
if ((shai1
+shai2
)%2==0){
printf("the shai1=%d,the shai2=%d\n",shai1
,shai2
);
printf("you won!\n");
}else{
money
-=(shai1
*shai2
);
printf("the shai1=%d,the shai2=%d\n",shai1
,shai2
);
printf("you lose,the money has %d.\n",money
);
}
return money
;
}
int game3(int shai1
,int shai2
,int money
){
if ((shai1
>shai2
) && (shai1
>4)){
printf("the shai1=%d,the shai2=%d\n",shai1
,shai2
);
printf("you won!\n");
}else{
money
/=shai1
;
printf("the shai1=%d,the shai2=%d\n",shai1
,shai2
);
printf("you lose,the money has %d.\n",money
);
}
return money
;
}
6.
(12)
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
char let0
,let
;
int i
;
srand(time(NULL));
while(1){
let0
=let
;
let
='a'+rand()%26;
i
+=2;
if ((let0
=='a'&&let
=='t')||(let0
=='i'&&let
=='s')||(let0
=='h'&&let
=='e')||(let0
=='w'&&let
=='e')||(let0
=='u'&&let
=='p')||(let0
=='o'&&let
=='n')){
break;
}
printf("all numbers have %d\n",let
);
}
return 0;
}
7.
(13)
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
int i
,j
=0;
srand(time(NULL));
for(i
=500;i
>0;i
--){
int num
,dist
=0;
while(1){
num
=1+rand()%2;
if(num
>1){
dist
+=2;
}else{
dist
-=1;
}
if (dist
>=10){
j
++;
break;
}else if (dist
==0){
break;
}
}
}
printf("the percent is %lf",j
/500.0);
}
8.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
float gaussrand_NORMAL() {
static float V1
, V2
, S
;
static int phase
= 0;
float X
;
if (phase
== 0) {
do {
float U1
= (float) rand() / RAND_MAX
;
float U2
= (float) rand() / RAND_MAX
;
V1
= 2 * U1
- 1;
V2
= 2 * U2
- 1;
S
= V1
* V1
+ V2
* V2
;
} while (S
>= 1 || S
== 0);
X
= V1
* sqrt(-2 * log(S
) / S
);
} else
X
= V2
* sqrt(-2 * log(S
) / S
);
phase
= 1 - phase
;
return X
;
}
float gaussrand(float mean
, float stdc
) {
return mean
+ gaussrand_NORMAL() * stdc
;
}
int main()
{
float mean
= 0;
float stdc
= 1;
float data
= 0;
for(int i
=0; i
<100; ++i
){
data
= gaussrand(0, 1);
printf("%f\t", data
);
}
}
9.
随机数生成器程序
double randomExponential(double lambda
)
{
double pv
= 0.0;
pv
= (double)(rand()%100)/100;
while(pv
== 0)
{
pv
= (double)(rand() % 100)/100;
}
pv
= (-1 / lambda
)*log(1-pv
);
return pv
;
}