1.在屏幕上输出菱形:
#include<stdio.h>
/*
打印菱形,类似
*
***
*
*/
int main() {
for (int i = 1; i <= 7; i++) {
for (int j = 6; j >= i; j--) {
printf(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
for (int i = 6; i >= 1; i--) {
for (int j = i; j <= 6; j++) {
printf(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
输出0 - 999之间的水仙花数
#include<stdio.h>
/*
100 - 999 的水仙花数
*/
int main() {
//int temp = 0;
//for (int i = 0; i <= 999; i++) {
// temp = i;
//
// if (temp / 100 != 0 && temp / 1000 == 0) {
// if (i == (temp % 10) * (temp % 10) * (temp % 10) + (temp / 100) * (temp / 100) * (temp / 100) + (temp % 100 / 10) * (temp % 100 / 10) * (temp % 100 / 10)) {
// printf("%d\n", i);
// }
// }
//}
int a = 0, b = 0, c = 0;
for (int i = 100; i <= 999; i++) {
a = i % 10;
b = i % 100 / 10;
c = i / 100;
if (i == a * a * a + b * b * b + c * c * c) {
printf("%d\n", i);
}
}
return 0;
}
求Sn = 2 + 22 + 222 + 2222 + 22222
#include<stdio.h>
/*
求2 + 22 + 222 + 2222 + 22222
*/
int main() {
int num = 0;
int sum = 0;
printf("Please input a number :");
scanf("%d", &num);
int temp = num;
for (int i = 0; i < 5; i++) {
printf("%d+", temp);
sum += temp;//总值
temp = temp * 10 + num;//每一项的值
}
printf("\b=%d\n", sum);
return 0;
}