#include
#include
double dist(double x1, double y1, double x2, double y2);
int main()
{
double x1, y1, x2, y2;
scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
printf("dist = %.2f\n", dist(x1, y1, x2, y2));
return 0;
}
double dist(double x1, double y1, double x2, double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
#include
int sign(int x);
int main()
{
int x;
scanf("%d", &x);
printf("sign(%d) = %d\n", x, sign(x));
return 0;
}
int sign(int x)
{
if (x == 0)
return 0;
return (x > 0) ? 1 : -1;
}
#include
int fib(int n);
int main()
{
int n;
scanf("%d", &n);
printf("%d\n", fib(n));
return 0;
}
1.递归,但会超时
Vint fib(int n)
{
if (n == 1 || n == 2)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
2.一般方法
int fib(int n)
{
if (n == 1 || n == 2)
return 1;
int a = 1, b = 1;
int tmp = 0;
for (int i = 3; i <= n; i++)
{
tmp = b;
b = a + b;
a = tmp;
}
return b;
}
#include
int MonthDays(int year, int month);
int main()
{
int ndays, month, year;
scanf("%d", &year);
for (month = 1; month <= 12; month++) {
ndays = MonthDays(year, month);
printf("%d ", ndays);
}
return 0;
}
int MonthDays(int year, int month) { int flag = 1; if (year % 4 == 0 && year % 100 != 0) flag = 0; if (year % 400 == 0) flag = 0; switch (month) { case 1:return 31; case 2:if (flag) return 28; else return 29; case 3:return 31; case 4:return 30; case 5:return 31; case 6:return 30; case 7:return 31; case 8:return 31; case 9:return 30; case 10:return 31; case 11:return 30; case 12:return 31; } }
#include
#include
int prime(int p);
int PrimeSum(int m, int n);
int main()
{
int m, n, p;
scanf("%d %d", &m, &n);
printf("Sum of ( ");
for (p = m; p <= n; p++) {
if (prime(p) != 0)
printf("%d ", p);
}
printf(") = %d\n", PrimeSum(m, n));
return 0;
}
1.
int prime(int n)
{
int flag = 1;
if (n <= 1)
return 0;
for (int i = 2; i < n; i++)
{
if (n % i == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
return n;
else
return 0;
}
2.
int PrimeSum(int m, int n)
{
int sum = 0;
for (int ret = 0, j = m; j <= n; j++)
{
ret = prime(j);
if (ret)
{
sum += ret;
}
}
return sum;
}
#include
void CharPyramid(int n, char ch);
int main()
{
int n;
char ch;
scanf("%d %c", &n, &ch);
CharPyramid(n, ch);
return 0;
}
void CharPyramid(int n, char ch) {
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++) {
printf(" ");
}for (k = 1; k <= i; k++) {
printf("%c ", ch);
}printf("\n");
}
}
#include
double result_real, result_imag;
void complex_prod(double x1, double y1, double x2, double y2);
int main(void)
{
double imag1, imag2, real1, real2;
scanf("%lf %lf", &real1, &imag1);
scanf("%lf %lf", &real2, &imag2);
complex_prod(real1, imag1, real2, imag2);
printf("product of complex is (%f)+(%f)i\n", result_real, result_imag);
return 0;
}
void complex_prod(double x1, double y1, double x2, double y2)
{
result_real = (x1 * x2 - y1 * y2);
result_imag = (x1 * y2 + x2 * y1);
}
//思路一定要清晰
#include
#include
double funcos(double e, double x);
int main()
{
double e, x;
scanf("%lf %lf", &e, &x);
printf("cos(%.2f) = %.6f\n", x, funcos(e, x));
return 0;
}
double funcos(double e, double x)
{
double res = 1, nn = 2,tmp=e,sum=1.0,p=1.0;
int flag = -1;
double i = 0;
while (tmp >= e)
{
for (i = nn-1; i <= nn; i++)
{
p *= x;
res *= i;
}
nn += 2;
tmp = p / res;
sum = sum + flag * tmp;
flag = -flag;
}
return sum;
}
#include
int CountDigit(int number, int digit);
int main()
{
int number, digit;
scanf("%d %d", &number, &digit);
printf("Number of digit %d in %d: %d\n", digit, number, CountDigit(number, digit));
return 0;
}
int CountDigit(int number, int digit)
{
int pows = 0, n = number;
if (n < 0)
n = -n;
else if (n == 0 && digit == 0)
return 1;
while (n > 0)
{
if (n % 10 == digit)
pows++;
n /= 10;
}
return pows;
}
#include
void hollowPyramid(int n);
int main()
{
int n;
scanf("%d", &n);
hollowPyramid(n);
return 0;
}
void hollowPyramid(int n)
{
for (int i = 0; i < n-1; i++)
{
for (int j = n-i-1; j > 0; j--)
{
printf(" ");
}
printf("%d", i+1);
for (int k = 2 * i - 1; k >= 1; k--)
{
printf(" ");
}
if (i)
{
printf("%d", i+1);
}
printf("\n");
}
for (int i = 0; i < 2 * n - 1; i++)
{
printf("%d", n);
}
}
#include
int sum(int n);
int main()
{
int n;
scanf("%d", &n);
printf("%d\n", sum(n));
return 0;
}
int sum(int n)
{
if (n > 0)
return n + sum(n - 1);
else
return 0;
}
#include
double calc_pow(double x, int n);
int main()
{
double x;
int n;
scanf("%lf %d", &x, &n);
printf("%.0f\n", calc_pow(x, n));
return 0;
}
double calc_pow(double x, int n)
{
if (n > 1)
return x * calc_pow(x, n - 1);
else
return x;
}
#include
int Ack(int m, int n);
int main()
{
int m, n;
scanf("%d %d", &m, &n);
printf("%d\n", Ack(m, n));
return 0;
}
int Ack(int m, int n)
{
if (!m)
return n + 1;
else if (n == 0 && m > 0)
return Ack(m - 1, 1);
else if (m > 0 && n > 0)
return Ack(m - 1, Ack(m, n - 1));
}
#include
int f(int n);
int main()
{
int n;
scanf("%d", &n);
printf("%d\n", f(n));
return 0;
}
int f(int n)
{
if (n == 0)
return 0;
if (n<=2&&n>0)
return 1;
else
return f(n - 1) + f(n - 2);
}
double fn(double x, int n);
int main()
{
double x;
int n;
scanf("%lf %d", &x, &n);
printf("%.2f\n", fn(x, n));
return 0;
}
double fn(double x, int n)
{
if (n == 1)
return x;
else
return -x * fn(x, n - 1)+x;
}
void printdigits(int n);
int main()
{
int n;
scanf("%d", &n);
printdigits(n);
return 0;
}
void printdigits(int n)
{
int k = 1;
if (n > 9)
{
k = n % 10;
printdigits(n / 10);
printf("%d\n", k);
}
else
printf("%d\n", n);
}
#include
#include
int search(int n);
int main()
{
int number;
scanf("%d", &number);
printf("count=%d\n", search(number));
return 0;
}
1.普通方法
int search(int n)
{
int i, cnt = 0;
for (i = 101; i <= n; i++) {
double digit = sqrt(i);
int a = i / 100;
int b = i / 10 % 10;
int c = i % 10;
if ((digit == (int)digit) && (a == b || a == c || b == c))
cnt++;
}
return cnt;
}
2.递归!!
int search(int n)
{
if (n > 100 && (sqrt(n) ==(int) sqrt(n))&&(n/100==n%10||n/100==n/10%10||n%10==n/10%10))
return 1 + search(n - 1);
else if (n > 100)
return 0 + search(n - 1);
else
return 0;
}
//错误点,else if中的定义错误了
//******************************
//** (n>100&&sqrt(n)!=(int)sqrt(n))
///开什么玩笑这是判断平方数的,不是题目中的返li
#include
# include
unsigned int reverse(unsigned int number);
int main()
{
unsigned int n;
scanf("%u", &n);
printf("%u\n", reverse(n));
return 0;
}
失败品
//unsigned int reverse(unsigned int number)
//{
// //static int k = 1 ;
//
// double p = 0.1;
// while (m > 9)
// {
// k *= 10;
// m /= 10;
// }
// if (number > 9)
// {
// p *= 10;
// return number / k * (int)p + reverse(number-k*number);
// }
// else
// return m;
//}
unsigned int reverse(unsigned int number)
{
int po = 0, res = number;
while (res) res /= 10, po++;
if (number < 10) return number;
return reverse(number / 10) + (number % 10) * pow(10, po - 1);
}
#include
void dectobin(int n);
int main()
{
int n;
scanf("%d", &n);
dectobin(n);
return 0;
}
void dectobin(int n)
{
if (n > 1)
{
dectobin(n / 2);
printf("%d", n % 2);
}
else
printf("%d", n);
}
#include
#define MAXN 10
void ArrayShift(int a[], int n, int m);
int main()
{
int a[MAXN], n, m;
int i;
scanf("%d %d", &n, &m);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
ArrayShift(a, n, m);
for (i = 0; i < n; i++) {
if (i != 0) printf(" ");
printf("%d", a[i]);
}
printf("\n");
return 0;
}
void ArrayShift(int a[], int n, int m) {
while (m--)
{
for (int i = n-1; i > 0 ; i--)
{
int tmp = a[i];
a[i] = a[i -1];
a[i -1] = tmp;
}
}
}