C语言编程题4道,简单

2024-11-27 13:36:05
推荐回答(5个)
回答1:

答案如下:

/*----------------------------------------------------------------*/
/*第一题:*/

#include

main()
{
int y;
printf("Enter the year:");
scanf("%d",&y);
if(y%4==0 && y%100!=0 || y%400==0)
{
printf("%d is a leap year.",y);
getch();
}
else
{
printf("%d is not a leap year.",y);
getch();
}
}

/*----------------------------------------------------------*/

/*第二题:*/

int main()
{
char str[] = "ABCD1234efgh";
int length = strlen(str);
char * p1 = str;
char * p2 = str + length - 1;
while(p1 < p2)
{
char c = *p1;
*p1 = *p2;
*p2 = c;
++p1;
--p2;
}
printf("str now is %s\n",str);
getch();
return 0;
}

/*-------------------------------------------------------*/

/*第三题:*/

int main()
{
int n, number = 20;
float s = 0, a = 2;
for(n=1; n<=number; n++)
{
s = s + 1 / a;
a += 2;

}
printf("sum is %9.6f\n", s);
getch();
return 0;
}

/*-----------------------------------------------------*/

/*第四题:*/

main()
{
int i, j, k, n;
printf( "'water flower'number is:" );
for (n = 100; n < 1000; n++)
{
i = n / 100; /* 分解出百位 */
j = n / 10 % 10; /* 分解出十位 */
k = n % 10; /* 分解出个位 */
if (i * 100 + j * 10 + k == i * i * i + j * j * j + k * k * k)
{
printf( "%-5d" , n);
}
}
printf( "\n" );
getch();
}

回答2:

/**********************一**************************/

#include

int main()
{
unsigned int year;
scanf("%d", &year);
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
printf("%d 年是闰年\n", year);
else
printf("%d 年是平年\n", year);

}

/**********************二**************************/

#include

int main()
{
char String[] = {"ABCD1234efgh"};
char *pstr;
for(pstr = String + sizeof String/ sizeof String[0] - 1; pstr != String - 1; --pstr)
printf("%c", *pstr);
}

/**********************三**************************/

#include

double sum(double x)
{
return x > 0 ? 1/(2*x) + sum(x-1) : 0;
}

int main()
{
int n = 20;
double sum_of = sum(n);
printf("Sum of 1/2 + 1/4 + ... + 1/%d is: %fl\n", n, sum_of);
}

/**********************四**************************/

#include

double cube(int n)
{
return n*n*n;
}

bool check(int x)
{
return cube(x%10) + cube(x%100/10) + cube(x/100) == x;
}

int main()
{
int i;
for(i = 100; i <= 999; ++i)
{
if(check(i))
printf("%d\n", i);
}
}

回答3:

1.
#include
main() {
int y;
printf(" 请输入一年份: ");
scanf("%d",&y);
if(!(y%4)&&y%100||!(y%400))
printf(" %d is a leap year\n",y);
else printf(" %d is not a leap year\n",y);
}

2.
#include
#include
#include
main() {
char s[] = "ABCD1234efgh", *p = (char*)malloc(sizeof(char));
int i, n = strlen(s);
printf("%d\n",n);
for(i = 0; i < n/2; i++) {
*p = s[i];
s[i] = s[n-1-i];
s[n-1-i] = *p;
}
puts(s);
}

3.
#include
main() {
int i;
double s = 0;
for(i = 1; i <= 20; i++)
s += 0.5 / i;
printf(" s = %g\n", s);
}

4.
#include
#include
main() {
int i, a[3];
for(i = 100; i < 1000; i++) {
a[0] = i / 100;
a[1] = i / 10 % 10;
a[2] = i % 10;
if(i==pow(a[0],3)+pow(a[1],3)+pow(a[2],3))
printf(" %d\n", i);
}
}

回答4:

main()
{ int a;
printf{"Enter the year:"};
scanf("%d",&a);
if(a%400==0||a%4==0&&a%100!=0)
printf("%d is a leap year.",a);
else
printf("%d is not a leap year.",a);
}

回答5:

关键就是在用指针的时候,数字年份除以4或者400是不是整除