2-1
int main()
{
printf("Programming in C is fun!");
return 0;
}
2-3
int main()
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < i; ++j)
{
printf(" ");
}
for (int j = 0; j < 4 - i; ++j)
{
printf("*");
if (j < 3 - i)
printf(" ");
}
printf("\n");
}
return 0;
}
2-4
int main()
{
printf("fahr = %d, celsius = %d", 150, 5 * (150 - 32) / 9);
return 0;
}
2-6
int main()
{
printf("height = %.2f\n", (float)(45));
return 0;
}
2-8
int main()
{
int input = 0;
scanf("%d", &input);
printf("Celsius = %d\n", 5 * (input - 32) / 9);
return 0;
}
2-9
int main()
{
int a = 0, b = 0;
scanf("%d %d", &a, &b);
printf("%d + %d = %d\n", a, b, a + b);
printf("%d - %d = %d\n", a, b, a - b);
printf("%d * %d = %d\n", a, b, a * b);
printf("%d / %d\ = %d", a, b, a / b);
return 0;
}
2-10
int main()
{
float x = 0.0f;
scanf("%f", &x);
if (x == 0)
printf("f(0.0) = 0.0\n");
else
printf("f(%.1f) = %.1f\n", x, 1.0 / x);
return 0;
}
2-11
int main()
{
float x = 0.0f;
scanf("%f", &x);
float f = 0.0f;
if (x >= 0)
f = sqrt(x);
else
f = pow(x + 1, 2) + 2 * x + 1.0 / x;
printf("f(%.2f) = %.2f\n", x, f);
return 0;
}
2-12
int main()
{
int lower = 0, upper = 0;
scanf("%d %d", &lower, &upper);
if (lower <= upper && upper <= 100)
{
printf("fahr celsius\n");
int tmp = lower;
while (tmp <= upper)
{
printf("%d%6.1f\n", tmp, 5 * (tmp - 32) / 9.0);
tmp += 2;
}
}
else
printf("Invalid.\n");
return 0;
}
2-13
int main()
{
int n = 0;
scanf("%d", &n);
double sum = 0.0;
for (int i = 1; i <= n; ++i)
{
sum += 1.0 / i;
}
printf("sum = %.6f\n", sum);
return 0;
}
2-14
int main()
{
int n = 0;
scanf("%d", &n);
double sum = 0.0;
for (int i = 1; i <= n; ++i)
{
sum += 1.0 / (2 * i - 1);
}
printf("sum = %.6f\n", sum);
return 0;
}
2-15
int main()
{
int n = 0;
scanf("%d", &n);
double sum = 0.0;
int flag = 1;
for (int i = 1; i <= n; ++i)
{
sum += flag * 1.0 / (3 * i - 2);
flag *= -1;
}
printf("sum = %.3f\n", sum);
return 0;
}
2-17
int main()
{
int n = 0;
scanf("%d", &n);
for (int i = 0; i <= n; ++i)
{
printf("pow(3,%d) = %d\n", i, (int)pow(3, i));
}
return 0;
}
2-18
double Fac(int n)
{
double fac = 1.0;
for (int i = 2; i <= n; ++i)
{
fac *= i;
}
return fac;
}
int main()
{
int m = 0, n = 0;
scanf("%d %d", &m, &n);
printf("result = %d\n", (int)(Fac(n) / (Fac(m) * Fac(n - m))));
return 0;
}
2-1
int main()
{
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
printf("Sum = %d; Average = %.1f\n", a + b + c + d, (a + b + c + d) / 4.0);
}
2-2
int main()
{
int i = 0;
scanf("%d", &i);
float total = 0.0f;
if (i <= 50 && i >= 0)
total = i * 0.53;
else if (i > 50)
total = 50 * 0.53 + (i - 50) * 0.58;
else
printf("Invalid Value!\n");
if (i >= 0)
printf("cost = %.2f\n", total);
return 0;
}
2-3
int main()
{
int m, n;
scanf("%d %d", &m, &n);
double sum = 0.0;
for (int i = m; i <= n; ++i)
{
sum += pow(i, 2) + 1.0 / i;
}
printf("sum = %.6f\n", sum);
return 0;
}
2-4
int main()
{
int n = 0;
scanf("%d", &n);
double sum = 0.0;
int flag = 1;
for (int i = 1; i <= n; ++i)
{
sum += flag * (1.0 * i)/ (2 * i - 1);
flag *= -1;
}
printf("%.3f\n", sum);
return 0;
}
2-5
int main()
{
int n = 0;
scanf("%d", &n);
double sum = 0.0;
for (int i = 1; i <= n; ++i)
{
sum += sqrt(i);
}
printf("sum = %.2f\n", sum);
return 0;
}
2-6
int Fac(int n)
{
int ret = 1;
for (int i = 1; i <= n; ++i)
ret *= i;
return ret;
}
int main()
{
int n = 0;
scanf("%d", &n);
int sum = 0;
for (int i = 1; i <= n; ++i)
{
sum += Fac(i);
}
printf("%d\n", sum);
return 0;
}
3-2
int main()
{
int n = 0;
scanf("%d", &n);
int ret = 0;
if (n < 0)
ret = -1;
else if (n == 0)
ret = 0;
else
ret = 1;
printf("sign(%d) = %d\n", n, ret);
return 0;
}
3-3
int main()
{
int n = 0;
scanf("%d", &n);
float sum = 0.0f;
int count = 0;
int input = 0;
for (int i = 0; i < n; ++i)
{
scanf("%d", &input);
sum += input;
if (input >= 60)
++count;
}
if (n != 0)
printf("average = %.1f\ncount = %d\n", sum / n, count);
else
printf("average = 0.0\ncount = 0\n");
return 0;
}
3-4
int main()
{
int letter = 0, blank = 0, digit = 0, other = 0;
char tmp = 0;
for (int i = 0; i < 10; ++i)
{
tmp = getchar();
if ((tmp >= 'a' && tmp <= 'z')
|| (tmp >= 'A' && tmp <= 'Z'))
++letter;
else if (tmp == ' ' || tmp == '\n')
++blank;
else if (tmp >= '0' && tmp <= '9')
++digit;
else
++other;
}
printf("letter = %d, blank = %d, digit = %d, other = %d\n", letter, blank, digit, other);
return 0;
}
3-5
int main()
{
int n = 0;
scanf("%d", &n);
if (n <= 2000 || n >= 2101)
printf("Invalid year!\n");
else
{
int count = 0;
for (int i = 2001; i <= n; ++i)
{
if (((i % 4 == 0) && (i % 100 != 0)) || (i % 400 == 0))
{
printf("%d\n", i);
++count;
}
}
if (count == 0)
printf("None\n");
}
return 0;
}
3-7
int main()
{
int score = 0;
scanf("%d", &score);
if (score >= 90)
printf("A\n");
else if (score >= 80 && score < 90)
printf("B\n");
else if (score >= 70 && score < 80)
printf("C\n");
else if (score >= 60 && score < 70)
printf("D\n");
else
printf("E\n");
}
3-8
int main()
{
printf("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit\n");
int count = 0;
int index = 0;
while (count < 5)
{
scanf("%d", &index);
if (index == 0)
break;
if (index == 1)
printf("price = 3.00\n");
else if (index == 2)
printf("price = 2.50\n");
else if (index == 3)
printf("price = 4.10\n");
else if (index == 4)
printf("price = 10.20\n");
else
printf("price = 0.00\n");
++count;
}
return 0;
}
3-1
int main()
{
int arr[3] = { 0 };
int sum = 0;
for (int i = 0; i < 3; ++i)
{
scanf("%d", &arr[i]);
sum += arr[i];
}
int min = arr[0], mid = arr[0], max = arr[0];
for (int i = 1; i < 3; ++i)
{
if (arr[i] > max)
max = arr[i];
if (arr[i] < min)
min = arr[i];
}
printf("%d->%d->%d\n", min, sum - min - max, max);
return 0;
}
3-2
int main()
{
float speed, limit;
scanf("%f %f", &speed, &limit);
if ((speed / limit) > 1.1 && (speed / limit) < 1.5)
printf("Exceed %.0f%%. Ticket %d\n", (speed - limit)/ limit * 100, 200);
else if ((speed / limit) >= 1.5)
printf("Exceed %.0f%%. License Revoked\n", (speed - limit)/ limit * 100);
else
printf("OK\n");
return 0;
}
3-3
#include <stdio.h>
int main()
{
float mile = 0.0f, fare = 0.0f;
int second = 0;
scanf("%f %d", &mile, &second);
if (mile <= 3)
fare = 10;
else if (mile > 3 && mile <= 10)
fare = 10 + (mile - 3) * 2;
else
fare = 10 + 7 * 2 + (mile - 10) * 3;
if (second >= 5)
fare += second / 5 * 2;
printf("%.0f\n", fare);
return 0;
}
3-4
int main()
{
int rank[5] = { 0 }, N = 0, input = 0;
scanf("%d", &N);
for (int i = 0; i < N; ++i)
{
scanf("%d", &input);
if (input >= 90)
++rank[0];
else if (input >= 80 && input < 90)
++rank[1];
else if (input >= 70 && input < 80)
++rank[2];
else if (input >= 60 && input < 70)
++rank[3];
else
++rank[4];
}
printf("%d %d %d %d %d", rank[0], rank[1], rank[2], rank[3], rank[4]);
return 0;
}
3-5
typedef struct Coordinate {
double x;
double y;
}coor;
int main() {
coor c1, c2, c3;
scanf("%lf %lf %lf %lf %lf %lf", &c1.x, &c1.y,\
&c2.x, &c2.y,\
&c3.x, &c3.y);
double sideL1 = sqrt(pow(c1.x - c2.x, 2) + pow(c1.y - c2.y, 2));
double sideL2 = sqrt(pow(c1.x - c3.x, 2) + pow(c1.y - c3.y, 2));
double sideL3 = sqrt(pow(c2.x - c3.x, 2) + pow(c2.y - c3.y, 2));
if (sideL1 + sideL2 <= sideL3 ||
sideL1 + sideL3 <= sideL2 ||
sideL2 + sideL3 <= sideL1)
printf("Impossible\n");
else {
double circumF = sideL1 + sideL2 + sideL3;
double p = circumF / 2.0;
printf("L = %.2f, A = %.2f\n", circumF,\
sqrt(p * (p - sideL1) * (p - sideL2) * (p - sideL3)));
}
return 0;
}
4-3
int getNum(double eps)
{
int i = 1;
while (1.0 / (3 * i - 2) > eps)
++i;
return i;
}
int main() {
double eps = 0.0f, sum = 0.0f;
scanf("%lf", &eps);
int n = getNum(eps);
int flag = 1;
for (int i = 0; i < n; ++i) {
sum += flag * 1.0 / (3 * i + 1);
flag = -flag;
}
printf("sum = %.6f\n", sum);
return 0;
}
4-6
int main() {
int num = 0, count = 0, i = 1, guessN;
scanf("%d %d", &num, &count);
while (i <= count) {
scanf("%d", &guessN);
if (guessN < 0) {
printf("Game Over\n");
break;
}
if (guessN < num) {
printf("Too small\n");
}
else if (guessN > num) {
printf("Too big\n");
}
else {
if (i == 1)
printf("Bingo!\n");
else if (i <= 3)
printf("Lucky You!\n");
else
printf("Good Guess!\n");
break;
}
++i;
}
if (i > count)
printf("Game Over\n");
return 0;
}
4-7
int main() {
double sum = 1.0;
int n = 0;
double fac = 1.0;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
fac *= i;
sum += 1.0 / fac;
}
printf("%.8f\n", sum);
return 0;
}
4-10
int main() {
int n = 0, num = 0, minimum = 0xffff;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &num);
if (num < minimum)
minimum = num;
}
printf("min = %d\n", minimum);
return 0;
}
4-11
int isPrimeN(int num, int* count) {
if (num == 1) {
return 0;
}
else if (num == 2) {
++*count;
return num;
}
else {
if (num % 2 == 0)
return 0;
for (int i = 3; i <= (int)sqrt(num); ++i) {
if (num % i == 0)
return 0;
}
++(*count);
return num;
}
}
int main() {
int left, right, sum = 0, count = 0;
scanf("%d %d", &left, &right);
for (int i = left; i <= right; ++i) {
sum += isPrimeN(i, &count);
}
printf("%d %d\n", count, sum);
return 0;
}
4-1
int main() {
int num = 0, sum = 0;
while (1) {
scanf("%d", &num);
if (num <= 0)
break;
if (num & 0x0001)
sum += num;
}
printf("%d\n", sum);
return 0;
}
4-2
int main() {
float input = 0.0f, sum = 0.0f;
double fac = 1.0;
scanf("%f", &input);
int k = 0;
while (1){
if (pow(input, k) / fac >= 0.00001){
sum += pow(input, k) / fac;
++k;
fac *= k;
}
else {
++k;
sum += pow(input, k) / fac;
break;
}
}
printf("%.4f\n", sum);
return 0;
}
4-3
int main() {
int n = 0;
scanf("%d", &n);
double sum = 2.0;
double den = 2.0, mol = 3.0;
for (int i = 2; i <= n; ++i) {
sum += (mol * 1.0) / den;
mol = mol + den;
den = mol -den;
}
printf("%.2f\n", sum);
return 0;
}
4-4
int main() {
int a, n, sum = 0, tmp = 1;
scanf("%d %d", &a, &n);
for (int i = 1; i <= n; ++i) {
sum += tmp * a;
tmp = tmp * 10 + 1;
}
printf("s = %d\n", sum);
return 0;
}
4-5
int main() {
int input = 0, count = 0;
scanf("%d", &input);
for (int i = 20; i >= 1; --i) {
for (int j = 50; j >= 1; --j) {
for (int k = 100; k >= 1; --k) {
if (5 * i + 2 * j + k < input)
break;
else if (5 * i + 2 * j + k == input) {
++count;
printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n", i, j, k, i + j + k);
break;
}
}
}
}
printf("count = %d\n", count);
return 0;
}
4-6
int pow (int m, int n) {
int i, ret;
ret = 1;
for (i = 0; i < n; i++)
ret *= m;
return ret;
}
int main() {
int n = 0;
scanf("%d", &n);
int digit = (int)pow(10, n - 1);
for (int i = digit; i < digit * 10 - 1; ++i) {
int tmp = 0, num = i;
for (int i = 0; i < n; ++i) {
tmp += pow(num % 10, n);
num /= 10;
}
if (tmp == i)
printf("%d\n", i);
}
return 0;
}
4-7
int main() {
int m, n;
scanf("%d %d", &m, &n);
int resA, resB;
/*fmax defined in <math.h>*/
for (int i = 1; i <= fmax(m, n); ++i) {
if (m % i == 0 && n % i == 0) resA = i;
}
resB = m * n / resA;
printf("%d %d\n", resA, resB);
return 0;
}
4-8
int main() {
float h = 0.0f;
int n;
scanf("%f %d", &h, &n);
if (n == 0) {
printf("0.0 0.0\n");
return 0;
}
float distance = 0.0f, bounce_height = 0.0f;
for (int i = 0; i < n; ++i) {
distance += h;
h /= 2;
distance += h;
}
distance -= h;
bounce_height = h;
printf("%.1f %.1f\n", distance, bounce_height);
return 0;
}
4-9
/*
* 思路: 先打上半部分, 在打下半部分; 每个部分先打空格空格, 再大星号空格
*/
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n / 2; ++i) {
for (int j = 0; j < n / 2 - i; ++j) { printf(" "); }
for (int j = 0; j < 2 * i + 1; ++j) { printf("* "); }
printf("\n");
}
for (int i = 0; i < n / 2 + 1; ++i) {
for (int j = 0; j < i; ++j) { printf(" "); }
for (int j = 0; j < n - i * 2; ++j) { printf("* "); }
printf("\n");
}
return 0;
}
4-10
int main() {
int n;
scanf("%d", &n);
int res = 1;
for (int i = 0; i < n - 1; ++i) {
res = res * 2 + 2;
}
printf("%d\n", res);
return 0;
}
4-11
int main() {
int N = 0;
scanf("%d", &N);
int a = 1, b = 1, pairs = 1, times = 0;
while (pairs < N) {
pairs = a + b;
a = b;
b = pairs;
times += 1;
}
if (N <= 1) times = 1;
else times += 2;
printf("%d\n", times);
}
6-3
#include <stddef.h> // size_t
int main() {
char str[100] = { 0 };
gets(str);
int dis = 0;
for (size_t i = 0; i < strlen(str); ++i) {
if (isalpha(str[i])) {
if (isupper(str[i])) {
dis = (str[i] - 'A' + 1) % 26;
str[i] = 'a' + dis;
} else {
dis = (str[i] - 'a' + 1) % 26;
str[i] = 'A' + dis;
}
}
}
printf("%s\n", str);
return 0;
}
6-8
typedef unsigned int size_t;
int main() {
char str[1000] = { 0 };
gets(str);
for (size_t i = 0; i < strlen(str); ++i) {
if ((i == 0 && isalpha(str[0])) || (isalpha(str[i]) && str[i - 1] == ' ')) {
str[i] = toupper(str[i]);
}
}
printf("%s\n", str);
}
7-2
#include <limits.h> // INT_MIN
int main() {
int n; scanf("%d", &n);
int val, max_val = INT_MIN, max_idx = -1;
for (int i = 0; i < n; ++i) {
scanf("%d", &val);
if (val > max_val) {
max_val = val;
max_idx = i;
}
}
printf("%d %d\n", max_val, max_idx);
return 0;
}
7-3
int main() {
int n;
scanf("%d", &n);
int arr[n];
int val = 0;
for (int i = n - 1; i >= 0; --i) {
scanf("%d", &val);
arr[i] = val;
}
for (int i = 0; i < n; ++i) {
if (i < n - 1) { printf("%d ", arr[i]); }
else printf("%d", arr[i]);
}
return 0;
}
7-1
#define INT_MIN -2147483648
void swap(int *left, int *right) {
int tmp = *left;
*left = *right;
*right = tmp;
}
int main() {
int n = 0;
scanf("%d", &n);
int arr[10] = { INT_MIN };
int count = n;
while (count--) {
scanf("%d", &arr[count]);
}
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - 1 -i; ++j) {
if (arr[j] < arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
for (int i = 0; i < n; ++i) {
if (i != n - 1) {
printf("%d ", arr[i]);
}
else {
printf("%d", arr[i]);
}
}
return 0;
}
7-9
int is_leap_year(int year) {
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
return 1;
}
else {
return 0;
}
}
int main() {
int day_of_year[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
int year = 0, month = 0, day = 0;
scanf("%d/%d/%d", &year, &month, &day);
int total_day = 0;
total_day += day_of_year[month - 1];
total_day += day;
if (is_leap_year(year) && (month >= 3)) ++total_day;
printf("%d", total_day);
return 0;
}
7-10
int main() {
char str[81] = { 0 };
char ch;
scanf("%c", &ch);
getchar();
gets(str);
int index = -1;
int right = strlen(str);
for (int i = 0; i < right; ++i) {
if (str[i] == ch) {
index = i > index ? i : index;
}
}
if (index != -1 && index < right) {
printf("index = %d", index);
}
else {
printf("Not Found");
}
return 0;
}
7-11
int main() {
char str[81] = { 0 };
gets(str);
int left = 0, right = strlen(str) - 1;
while (left < right) {
char tmp = str[left];
str[left] = str[right];
str[right] = tmp;
++left, --right;
}
printf("%s\n", str);
return 0;
}
以上题目全部测试通过,其余题目博主抽时间会写完滴,如有问题欢迎评论或私信指证,谢谢老铁的支持