1.Hermite Polynomials多项式
int hermite(int n, int x)
{
if(n <= 0)
return 1;
else if(n == 1)
return 2 * x;
else
return 2 * x * hermite(n - 1, x) - 2 * (n - 1) * hermite(n - 2, x);
}
2.两个整型值M和N(M、N均大于0)的最大公约数
#include<iostream>
using namespace std;
int gcd(int M, int N)
{
if(M % N == 0)
return N;
else
{
int R = M % N;
return gcd(N,R);
}
}
int main()
{
cout<<gcd(12,16);
system("pause");
return 0;
}
输出结果
3.为下面函数原型编写函数定义:
int ascii_to_integer(char *str)
这个字符串必须包含一个或者多个数字,函数应该把这些数字字符转化为整数并返回这个整数,如果字符串参数包含了任何非数字字符,函数就返回0.
#include<iostream>
using namespace std;
int ascii_to_integer(char *str)
{
int value = 0;
while(*str >='0' && *str <='9')
{
value *= 10;
value += *str - '0';
str++;
}
if(*str !='\0')
value = 0;
return value;
}
int main()
{
char *str1 = "12334424";
cout<<ascii_to_integer(str1);
system("pause");
return 0;
}
输出结果
4.编写一个名叫max_list的函数,它用于检查任意数目的整型参数并返回它们当中的最大值。参数列表必须以一个负值结尾,提示列表的结束。
//检查任意数目的整型参数并返回他们当中的最大值
#include<stdarg.h>
#include<iostream>
int max_list(int first_arg, ...)
{
va_list var_arg;
int max;
int this_arg = 0;
if(first_arg > 0)
{
max = first_arg;
va_start(var_arg, first_arg);
while((this_arg = va_arg(var_arg,int))>=0)
if(max < this_arg)
max = this_arg;
va_end(var_arg);
}
return max;
}
int main()
{
std::cout<<max_list(2,12,3,14,13,-1);
system("pause");
return 0;
}
输出结果
5.实现一个简化的printf函数,它能够处理%d,%f,%s和%c格式码。
//实现简化printf函数,能够处理%d,%f,%s,%c和格式码
#include <iostream>
#include <stdarg.h>
void print_integer(int value)
{
printf("%d", value);
}
void print_float(float value)
{
printf("%.1f", value);
}
void my_printf(char *string, ...)
{
va_list arg;
va_start(arg, string);
char *str1 = string;
char *str2;
while (*str1 != '\0')
{
if (*str1 != '%')
{
putchar(*str1++);
continue;
}
switch (*(str1 + 1))
{
case 'd':
print_integer(va_arg(arg, int));
str1 += 2;
break;
case 'f':
print_float(va_arg(arg, double));
str1 += 2;
break;
case 'c':
putchar(va_arg(arg, int));
str1 += 2;
break;
case 's':
str2 = va_arg(arg, char *);
while (*str2 != '\0')
putchar(*str2++);
str1 += 2;
break;
default:
str1 += 2;
break;
}
}
}
int main()
{
int a = 5;
float b = 4.3;
char c = 'c';
char *s = "Hello,world";
my_printf("my_printf a is %d and b is %f and c is %c and d is %s", a, b, c, s);
system("pause");
return 0;
}
输出结果
6.
编写函数
void written_amount(unsigned int amount,char *buffer);
它把amount表示的值转换为单词形式,并储存于buffer中。这个函数可以在一个打印支票的程序中使用。例如,如果amount的值是16312,那么buffer中存储的字符串应该是
SIXTEEN THOUSAND THREE HUNDRED TWELVE
#include <iostream>
#include <string.h>
#define LONG 1000
char *unit[10] = {" zero"," one"," two"," three",
" four"," five"," six"," seven",
" eight"," nine"};
char *ten[10] = {" ten"," eleven"," twelve",
" thirteen"," fourteen"," fifteen",
" sixteen"," seventeen"," eighteen",
" nineteen"};
char *decade[10] = {" "," "," twenty"," thirty",
" forty"," fifty"," sixty",
" seventy"," eighty"," ninety"};
void written_amount(unsigned long int amount,char *buffer);
int main()
{
unsigned long int amount;
char buffer[LONG] = "";
scanf("%lu",&amount);
written_amount(amount,buffer);
printf("%s",buffer);
system("pause");
return 0;
}
void written_amount(unsigned long int amount,char *buffer)
{
unsigned long int n = 100000000;
if(amount / n != 0){
int temp = amount / n;
amount %= n;
written_amount(temp,buffer);
strcat(buffer," billion");
}
n = 100000;
if(amount / n != 0){
int temp = amount / n;
amount %= n;
written_amount(temp,buffer);
strcat(buffer," million");
}
n = 100;
if(amount / n != 0){
int temp = amount / n;
amount %= n;
written_amount(temp,buffer);
strcat(buffer," hundred");
}
n = 10;
if(amount / n != 0){
int temp = amount / n;
amount %= n;
if(temp == 1){
strcat(buffer,ten[amount]);
}else{
strcat(buffer,decade[temp]);
if(amount != 0)
strcat(buffer,unit[amount]);
}
}else{
strcat(buffer,unit[amount]);
}
}