题目1:
A以10%的单利息投资了100美元,B以每年5%复合利息投资了100美元
。编写一程序,计算需要多少年B的投资总额才会超过A的投资总额度,并且
显示出到那个时刻两个人的各自的资产总额。
#define MONEY_A 100
#define MONEY_B 100
static void test1(void)
{
//初始化A,B的金额为100
float A = MONEY_A , B = MONEY_B;
int num = 0;//计算需要多少年B可以超过A
while(1)
{
A += MONEY_A*0.1;
B += B*0.05;
num++;
if( B > A)
{
printf("在第%d年,B超过A的资产总额\n", num );
printf("这个时候A的资产为%f美元\n", A );
printf("这个时候B的资产为%f美元\n", B );
break;
}
}
}
题目2:
从终端读入数据,直到输入0值作为止。并且计算出其中的偶数的个数及平均值和奇数的个数及平均值
static void test2(void)
{
//初始化奇数与偶数的个数与总数
int odd = 0,even = 0;
float sum_odd = 0,sum_even = 0;
printf("请输入N个正整数:>\n");
while(1)
{
int num;
int i = scanf("%d", &num);
if( num == 0 || i == 0)
{
break;
}
if(num%2 == 1)//奇数
{
odd++;
sum_odd += num;
}
else//偶数
{
even++;
sum_even += num;
}
}
printf("接收了%d个奇数,%d个偶数\n", odd , even );
printf("其中奇数的平均值 = %f\n",sum_odd/odd);
printf("其中偶数的平均值 = %f\n",sum_even/even);
}
题目3:
从终端输入若干字符,对其中的元音字母进行统计
static void test3(void)
{
int sum = 0;//记录累计的元音个数
printf("请输入若干字符,以0结尾\n");
while(1)
{
char i = 0;
int x = scanf("%c", &i );
//这里是根据ASCII码表,选出对应的元音字母码号
if( i == 65 || i == 69 || i == 73 || i == 79 || i == 85)
{
sum++;
}
else if( i == 48 )
{
break;
}
}
printf("其中元音个数为%d个\n",sum);
}
题目4:
写出fibonacci数列的前40项(不能用数组实现)
1,1,2,3,5,8,13,21。。。
#define MAX 40 //需要打印出前40项,90是longlong的上限
static void test4(void)
{
int sum = 0;//累计项数
unsigned long long front = 0,later = 0;
for( int i = 0 ; i<MAX ; i++ )
{
if( later == 0)
{
later++;
printf("%lld ", later );
}
else if( front == 0)
{
front++;
printf("%lld ", front );
}
else
{
int num = front;
front += later;
later = num;
printf("%llu ", front );
}
if( ( i + 1 ) % 5 == 0)
printf("\n");
}
printf("\n");
}
题目5:
输出九九乘法表
//更改宏可以输出任意XX乘法表
#define X_MAX 9
#define Y_MAX 9
static void test5(void)
{
for ( int x = 1; x < X_MAX + 2; x++ )
{
for( int y = 1; y < Y_MAX - x + 2; y++ )
{
printf("%d x %d = %d\t", x , y , x*y );
}
printf("\n");
}
}
题目6:
百钱买白鸡:公鸡 * 1 = 5
母鸡 * 1 = 3
小鸡 * 3 = 1
百钱买百鸡,公鸡,母鸡,小鸡各几只
#define MONEY 100//金钱
#define QUANTITY 100//数量
static void test6(void)
{
printf("百钱百鸡\n");
unsigned int male = 0,female = 0,young = 0;//鸡的数量
for( male = 0 ; male <= QUANTITY / 1 && male <= MONEY / 5 ; male++ )
{
for( female = 0 ; female <= QUANTITY / 1 && female <= MONEY / 3 ; female++ )
{
for( young = 3 ; young <= QUANTITY / 1 && young <= MONEY / 1 ; young += 3 )
{
if( male*5 + female*3 + young / 3 == 100 )//百钱
{
if( male + female + young == 100 )//百鸡
{
printf("公鸡 = %d\t母鸡 = %d\t小鸡 = %d\n", male , female , young );
}
}
}
}
}
}
题目7:
求出1000以内的水仙花数:
各位的立方+十位的立方+百万的立方 = 本身
#define DAFFODIL 1000
static void test7(void)
{
int ts = 0 ,re = 0 ,sum = 0;
for( int i = 100 ; i < DAFFODIL ; i++ )
{
ts = i;//获取缓存数
while(1)
{
re = ts % 10;
ts = ts / 10;
sum += re*re*re;
if( ts == 0 )
{
if( sum == i )
{
printf("%d\n",i);
}
sum = 0;//累计数清零
break;
}
}
}
}
题目8:
求出1000以内的所以质数:2,3,5,7,11(只能被1和本身整除)
#define PRIME 1000
static void test8(void)
{
int sum = 0;
for( int i = 2 ; i < PRIME ; i++ )
{
sum = 0;
for( int j = 1 ; j <= i ; j++ )
{
if( i % j == 0 )
{
sum++;
}
if( sum > 2 )
{
break;
}
}
if( sum == 2 )
{
printf("%d\n",i);
}
}
}
题目9:
在终端上实现如下输出:
ABCDEF
BCDEF
CDEF
DEF
EF
F
//修改宏可以任意字母开始,打印任意数量的字母
#define ASCII_A 65
#define ASCII_QUANTITY 6
static void test9(void)
{
for( int i = ASCII_A ; i < ASCII_A + ASCII_QUANTITY ; i++ )
{
for( int j = i ; j < ASCII_A + ASCII_QUANTITY ; j++ )
{
printf("%c",j);
}
printf("\n");
}
}
题目10:
输出钻石型
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int row = 15, half, i, j;
//打印钻石上半部分
half = row / 2 + 1;
for(i = 1; i <= half; i++)
{
//打印前导空格
for(j = 1; j <= half - i; j++)
{
printf(" ");
}
//打印星号
for(j = 1; j <= 2 * i - 1; j++)
{
printf("*");
}
//换行
printf("\n");
}
//打印钻石下半部分
for(i = 1; i < half; i++)
{
//打印前导空格
for(j = 1; j<= i; j++)
{
printf(" ");
}
//打印星号
for(j = 1; j <= 2 * (half - i - 1) + 1; j++)
{
printf("*");
}
//换行
printf("\n");
}
return 0;
}