//计算指定数量的值的平均值
//#include<stdarg.h>
//#include<stdio.h>
//
//float average(int n_values, ...) {
// va_list var_arg;
// int count;
// float sum = 0;
// //准备访问可变参数
// va_start(var_arg, n_values);
// for (count = 0; count < n_values; count += 1) {
// sum += va_arg(var_arg, int);
// }
// //完成处理可变参数
// va_end(var_arg);
// return sum / n_values;
//}
//int main() {
// float avg = 0;
// avg = average(10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// printf("%f", avg);
// return 0;
//}
//1.
//#include<stdio.h>
//int Hermite_Polynomials(int num,int x) {
// if (num <= 0) {
// return 1;
// }
// if (num == 1) {
// return 2 * x;
// }
// return 2 * x*Hermite_Polynomials(num - 1, x) - 2 * (num - 1)*Hermite_Polynomials(num - 2, x);
//
//}
//int main() {
// int result = Hermite_Polynomials(3, 2);
// printf("%d", result);
// return 0;
//}
//2.
//int
//gcd(int m, int n)
//{
// int r;
// if(m < = 0 || n < = 0)
// return 0;
// r = m % n;
// return r > 0 ? g c d(n, r) : n;
//}
//3.
//int ascii_to_integer(char *string) {
// int value = 0;
// while (*string >= '0'&&*string <= '9') {
// value *= 10;
// value += *string - '0';
// string++;
//
// }
// if (*string != '\0')
// value = 0;
// return value;
//}
//4.
//#include<stdarg.h>
//int max_list(int first_arg, ...)
//{
// va_list var_arg;
// int max = 0;
//
// if (first_arg >= 0) {
// int this_arg;
// max = first_arg;
//
// va_start(var_arg, first_arg);
// while ((this_arg = va_arg(var_arg, int)) >=0)
// if (this_arg > max)
// max = this_arg;
// va_end(var_arg);
// }
// return max;
//}
//5.
//#include <stdarg.h>
//#include<stdio.h>
//
//void
//printf(char *format, ...)
//{
// va_list arg;
// char ch;
// char *str;
// va_start(arg, format);
// /*
// ** Get the format characters one by one.
// */
// while ((ch = *format++) != '\0') {
// if (ch != '%') {
// /*
// ** Not a format code –– print the character verbatim.
// */
// putchar(ch);
// continue;
// }
// /*
// * * W e g o t a % – – n o w g e t t h e format code and use it to format
// ** the next argument.
// */
// switch (*format != '\0' ? *format++ : '\0') {
// case 'd':
// print_integer(va_arg(arg, int));
// break;
// case'f':
// print_float(va_arg(arg, float));
// break;
// case 'c':
// putchar(va_arg(arg, int));
// break;
// case 's':
// str = va_arg(arg, char *);
// while (*str != '\0')
// putchar(*str++);
// break;
// }
// }
//}
//6.
#include<string.h>
static const char *digits[] = {"", "ONE ", "TWO ", "THREE ", "FOUR ", "FIVE ", "SIX ", "SEVEN ",
"EIGHT ", "NINE ", "TEN ", "ELEVEN ", "TWELVE ", "THIRTEEN ",
"FOURTEEN ", "FIFTEEN ", "SIXTEEN ", "SEVENTEEN ", "EIGHTEEN ",
"NINETEEN "
};
static const char *tens[] = {
"", "", "TWENTY ", "THIRTY ", "FORTY ", "FIFTY ", "SIXTY ", "SEVENTY ",
"EIGHTY ", "NINETY "
};
static const char *magnitudes[] = {
"", "THOUSAND ", "MILLION ", "BILLION "
};
/*
** Convert the last 3–digit group of amount to words. Amount is the value
** to be converted, buffer is where to put the words, and magnitude is the
** name of the 3–digit group we’re working on.
*/
static void
do_one_group(unsigned int amount, char *buffer, char **magnitude)
{
int value;
/*
** Get all the digits beyond the last three. If we have any value
** there, process those digits first. Note that they are in the next
** magnitude.
*/
value = amount / 1000;
if (value > 0)
do_one_group(value, buffer, magnitude + 1);
/*
** Now process this group of digits. Any hundreds?
*/
amount %= 1000;
value = amount / 100;
if (value > 0) {
strcat(buffer, digits[value]);
strcat(buffer, "HUNDRED ");
}
/*
** Now do the rest of the value. If less than 20, treat it as a single
** digit to get the teens names.
*/
value = amount % 100;
if (value >= 20) {
/*
** Greater than 20. Do a tens name and leave the units to be
** printed next.
*/
strcat(buffer, tens[value / 10]);
value %= 10;
}
if (value > 0)
strcat(buffer, digits[value]);
/*
** If we had any value in this group at all, print the magnitude.
*/
if (amount > 0)
strcat(buffer, *magnitude);
}
void
written_amount(unsigned int amount, char *buffer)
{
if (amount == 0)
/*
** Special case for zero.
*/
strcpy(buffer, "ZERO ");
else {
/*
** Store an empty string in the buffer, then begin.
*/
*buffer = '\0';
do_one_group(amount, buffer, (char **)magnitudes);
}
}