0:
#include <stdio.h>
#define FIRST_KG 23
#define NEXT_KG 14
int main()
{
int weight, cost;
printf("公斤 —— 花费(元):\n");
for (weight = 1, cost = FIRST_KG; weight <= 20; weight++, cost += NEXT_KG)
{
printf("%3d —— %3d\n", weight, cost);
}
return 0;
}
1:
#include <stdio.h>
int main()
{
int num = 0;
long sum = 0L; // 0L表示类型为long的0
int status;
do
{
printf("请输入合法的数字:");
sum = sum + num;
status = scanf("%d", &num);
} while(status == 1);
printf("结果是:%ld\n", sum);
return 0;
}
2:
#include <stdio.h>
int main()
{
float num = 0;
double sum = 0;
int status;
do
{
printf("请输入合法的数字:");
do
{
sum = sum + num;
status = scanf("%f", &num);
} while (getchar() != '\n' && status == 1);
} while(status == 1);
printf("结果是:%.2lf\n", sum);
return 0;
}
3:
左上:
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= 9; j++)
{
if (i <= j)
{
printf("%d*%d=%-2d ", i, j, i * j);
}
}
putchar('\n');
}
return 0;
}
右上:
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= 9; j++)
{
if (j < i)
{
printf(" ");
}
else
{
printf("%d*%d=%-2d ", i, j, i * j);
}
}
putchar('\n');
}
return 0;
}
右下:
#include <stdio.h>
int main()
{
int i, j, k;
for (i = 1; i <= 9; i++)
{
for (k = 1; k <= 9-i; k++)
{
printf(" ");
}
for (j = 1; j <= i; j++)
{
printf("%d*%d=%-2d ", i, j, i * j);
}
putchar('\n');
}
return 0;
}