1.在屏幕上输出以下图案:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char arr1[] = " ";//字符串后自动带有"\0",数组从0开始计数的
char arr2[] = "*********";//打印出菱形,此数组长度一定为奇数
char arr3[] = " ";
char ch = '*';
int left1, right1;
int left = 0; //stelen函数可以输出字符串长度
int right = strlen(arr1) - 1;//字符串长度减去1,刚好是数组最后一个值的下标
int middle = right/ 2; //最后一个值的下标除以2,是最中间的数的下标
for (left1 = 0, right1 = 0; left1 <=middle;left1++,right1++)
{
arr1[middle - left1] = ch;//向左打印
arr1[middle + right1] = ch;//向右打印
printf("%s\n", arr1);
} //第一个for打印上半部分,上半部分打印*
for (left = 0; left <= right; left++, right--)
{
arr1[left] = arr3[left];
arr1[right] = arr3[right];
printf("%s\n", arr1);
} //第二个for打印下半部分,下半部分打印空格
system("pause");
}
2.求出0~999之间的所有“水仙花数”并输出。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int deng(int x,int y)
{
int num;
num = pow(x, y); //函数定义,pow用来及x的次方
return num;
}
int main()
{
int x, y, z;
int number = 0;
for (number = 100; number < 1000; number++)
{
x = number / 100;
y = (number - x * deng(10, 2)) / 10;
z = (number - x * deng(10, 2) - y * deng(10, 1));
if (number == deng(x, 3) + deng(y, 3) + deng(z, 3)) //函数的调用
{
printf("%d\n", number);
}
}
system("pause");
}
3.求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字,
例如:2+22+222+2222+22222
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int ISsn(int a)
{
int sn = 0;
int count = 0;
int sum = 0;
for (count = 0; count <= 4; count++)
{
sn =sn + a * pow(10, count);
sum = sn + sum;
}
return sum;
}
int main()
{
int a;
int sn;
printf("请输入a:");
scanf("%d", &a);
sn = ISsn(a);
printf("Sn = %d\n",sn);
system("pause");
return 0;
}