#include<stdio.h> //6章7题
#include<string.h>
int main()
{
char arr[20] = { 0 };
scanf("%20s", arr);
int i = 0;
for (i = strlen(arr)-1; i >= 0; i--)
{
printf("%c", arr[i]);
}
return 0;
}
#include<stdio.h>//6章8题
int main()
{
double f, s;
while (scanf("%lf %lf", &f, &s) == 2)
{
printf("%f\n", (f - s) / (f * s));
printf("enter the two next number:\n");
}
printf("break\n");
}
#include<stdio.h> //6章9题
double YB(double x, double y)
{
double z = (x - y) / (x * y);
return z;
}
int main()
{
double f, s;
while (scanf("%lf %lf", &f, &s) == 2)
{
double i = YB(f, s);
printf("%g\n", i);
printf("please enter again(q ot quit):");
}
return 0;
}
#include<stdio.h>//6章10题
int main()
{
int i = 0;
int j = 0;
printf("please enter lower and upper integer limits:");
while (scanf("%d %d", &i, &j)==2 && (i < j))
{
int sums = 0;
int d = 0;
for (d = i; d <= j; d++)
{
sums += d * d;
}
printf("The sums is the squares from %d to %d is %d\n", i * i, j * j, sums);
printf("Enter next set of limits\n");
}
return 0;
}
#include<stdio.h> //6章11题
#define limit 8
int main()
{
int arr[limit] = { 0 };
int i = 0;
for (i = 0; i <= limit-1; i++)
{
scanf_s("%d", &arr[i]);
}
for (i = limit - 1; i >= 0; i--)
{
printf("%d ", arr[i]);
}
return 0;
}
//第6章12题
#include<stdio.h>
int main()
{
int n = 0;
while (scanf("%d", &n) == 1 && n > 0)
{
int i = 0;
double sum1 = 0.0;
double sum2 = 0.0 ;
int j = 1;
for (i = 1; i <=n; i++)
{
sum1 += 1.0 / i;
}
for (i = 1; i <= n; i++)
{
sum2 += (1.0 / i)*j;
j *= -1;
}
printf("%g %g", sum1, sum2);
}
return 0;
}
//第6章12题
#include<stdio.h>
int main()
{
int n = 0;
int i = 0;
while (scanf("%d", &n)==1 && n > 0)
{
double res1 = 0.0;
double res2 = 0.0;
for (i = 1; i <= n; i++)
{
res1 += 1.0 / i;
if (i % 2 == 1)
{
res2 += 1.0 / i;
}
else
{
res2 -= 1.0 / i;
}
}
printf("%g %g %g", res1, res2,res1+res2);
}
return 0;
}
//第6章13题
#include<stdio.h>
int main()
{
int arr[8] = { 0 };
int i = 0;
int t = 2;
for (i = 0; i < 8; i++)
{
arr[i] = t;
t *= 2;
}
for (i = 0; i < 8; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
//第6章14题
#include<stdio.h>
int main()
{
double arr1[8] = { 0 };
double arr2[8] = { 0 };
int i = 0;
int j = 0;
//for (i = 0; i < 8; i++) 嵌套循环实现
//{
// scanf("%lf", &s);
// arr1[i] = s;
// for (j = 0; j <= i; j++)
// {
// arr2[i] +=arr1[j] ;
// }
//}
for (i = 0; i < 8; i++) 单循环实现
{
scanf("%lf", &arr1[i]);
arr2[0] = arr1[0];
if (i >= 1)
{
arr2[i] = arr1[i] + arr2[i - 1];
}
}
for (i = 0; i < 8; i++)
{
printf("%-3g ", arr1[i]);
}
printf("\n");
for (i = 0; i < 8; i++)
{
printf("%-3g ", arr2[i]);
}
return 0;
}
//第6章15题
#include<stdio.h>
#define len 255
int main()
{
int i = 0;
char input[len];
while (i < len)
{
scanf("%c", &input[i]);
if (input[i] == '\n')
{
break;
}
i++;
}
for (i=i-1; i >= 0; i--)
{
printf("%c", input[i]);
}
return 0;
}
//第6章16题
#include<stdio.h>
int main()
{
double De = 100;
double Da = 100;
int year = 0;
while (De <= Da)
{
Da += 100 * 0.1;
De += De * 0.05;
year++;
}
printf("after %d year %g beyond %g",year,De,Da);
return 0;
}
//第6章17题
#include<stdio.h>
int main()
{
double i = 100.0;
int year = 0;
do
{
i += i * 0.08;
i -= 10;
year++;
} while (i > 9);
printf("%d %g\n",++year, i);
return 0;
}
//第6章18题
#include <stdio.h>
int main(void)
{
int i = 1;
int friends = 5;
while (friends < 150)
{
printf("At %d weeks, Rabnud has", i);
printf("%4d friends.\n", friends);
friends = 2 * (friends - i++);
}
printf("At %d weeks, over Dunbar's number(150).\n", i);
return 0;
}