C语言练习4

编程入门 行业动态 更新时间:2024-10-11 19:16:46

C<a href=https://www.elefans.com/category/jswz/34/1770116.html style=语言练习4"/>

C语言练习4

1. 编写程序, 提示用户输入一个数n, 打印出 1 ~n 的所有偶数平方值。

#include <stdlib.h>
#include <stdio.h>int main()
{printf("请输入一个数\n");int n = 0;scanf("%d", &n);int i = 0;int count = 0;for (i = 1; i < n; ++i){if (0 == i*i % 2){printf("%-4d ", i*i);count++;}if (0 == count % 5){printf("\n");}}system("pause");return 0;
}

 2. 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

#include <stdio.h>
#include <stdlib.h>
int main()
{float h = 100.0f;  //100米高度float s = 100.0f;    //路程for (int i = 0; i < 10; i++){s = s + h / 2 * 2;h = h / 2;}printf("第十次落地共经过%f米\n", s);printf("第十次反弹的高度为%f米\n", h);system("pause");return 0;
}

 3.题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

循环

#include <stdio.h>
#include <stdlib.h>
int main()
{int a = 1, b = 1, c = 1;int i = 2;int month = 0;scanf("%d", &month);while (1){if (month == 1){c = 1;break;}else if (month == 2){c = 1;break;}else{a = b;b = c;c = a + b;}i++;if (i == month){break;}}printf("%d", c*2);system("pause");return 0;
}

递归

#include <stdio.h>
#include <stdlib.h>int fun(int month);int main()
{int month = 0;scanf("%d", &month);int count = fun(month);printf("第%d月的兔子数为%d\n", month, count);system("pause");return 0;
}int fun(int month)
{int count = 0;if (1 == month){return 2;}else if (2 == month){return 2;}else{return  fun(month-2)+fun(month-1);}
}

5对,十只没有什么毛病。

4.牛顿迭代法求开方

#include <stdlib.h>
#include <stdio.h>
double mysqrt(double a, double x);
int main()
{double a = 0.0;scanf("%lf", &a);double x = a / 2;double y=mysqrt(a,x);printf("%.8lf", y);system("pause");return 0;
}double mysqrt(doublea, double x)
{double y = 0.0;y = (x + a / x) / 2;while ((x - y) > 0.0000001){x = y;y = (x + a / x) / 2;}return y;
}

5. 如果一个数等于它的因子之和,则称该数为“完数”(或“完全数”)。例如,6的因子为1、2、3,而
6 = 1 + 2 + 3,因此6是“完数”。编程找出1000 之内的所有完数。

#include <stdio.h>
#include <stdlib.h>
int main()
{int i = 0;int tmp = 0;for (i = 1; i < 1000; i++){for (int j = 1 ; j < i; j++){if (i%j == 0){tmp = tmp + j;}}if (tmp == i){printf("%d ", i);}tmp = 0;}system("pause");return 0;
}

 6. 编写程序,输出9 * 9口诀。

#include <stdlib.h>
#include <stdio.h>int main()
{int i = 0;int count = 0;for (i = 1; i < 10; i++){for (int j = 1; j <=i; j++){printf("%d*%d=%-2d ", i, j, i*j);}printf("\n");}system("pause");return 0;
}

 

 7.编写程序,找出用户输入的一串数中的最大数,程序需要提示用户一个一个地输入数,当用户输入0或负数时,程序显示已输入的最大非负数

初级版

#include <stdio.h>
#include <stdlib.h>
#define SIZE 100int format(int input, int arr[]);
int Max(int arr[], int n);
void swap(int *ap, int *bp);int main()
{int input = 0;int arr[SIZE] = { 0 };int count=format(input,arr);int max=Max(arr, count);printf("max=%d\n", max);system("pause");return 0;
}int format(int input,int arr[])
{int i = 0;printf("每输入一个数请用空格分开!\n");printf("\n");printf("输入0或负数终止输入!\n");while (scanf("%d", &input),input != 0 && input > 0){arr[i] = input;if (i > SIZE - 1){break;}i++;}return i;
}int Max(int arr[], int n)
{int i = 0;for (i = 0; i < n; i++){if (arr[i]>arr[i + 1]){swap(&arr[i], &arr[i + 1]);}}return arr[n];
}void swap(int *ap, int *bp)
{int tmp = *bp;*bp = *ap;*ap = tmp;
}

优化版

#include <stdlib.h>
#include <stdio.h>int main()
{int val = 0;  //inputint max = 0;  while (1){printf("intput a numbmer\n");scanf("%d", &val);if (val < 0 || val == 0){break;}if (val>max){max = val;}}printf("max=%d\n", max);system("pause");return 0;
}

 8.用户输入任意日期,显示更早的日期

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
struct Date
{int year;int month;int day;
};bool isLeap(int year)
{if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)){return true;}return false;
}int Get_YM_day(int year, int month)
{static const int day[13] = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (year<0 || month>12 || month < 0){printf("输入有误\n");return -1;}if (month == 2 && isLeap(year)){month = 0;}return day[month];
}int main()
{struct Date val = { 0 };struct Date min = { INT_MAX, 12, 31 };while (1){printf("input year/month/day\n");scanf("%d%d%d", &val.year, &val.month, &val.day);if (val.year < 0 || val.month < 0 || val.day < 0){break;}if (1>val.month || val.month > 12){break;}if (1 > val.day || val.day > Get_YM_day(val.year, val.month)){break;}if (val.year < min.year){min = val;}else if (val.month < min.month){min = val;}else if (val.day < min.day){min = val;}}printf("最早的日期为:%d year %d month %d day", min.year, min.month, min.day);system("pause");return 0;
}

 

9.编写程序打印单月的日历,用户指定这个月的天数和该月起始日是星期几

初级版


#include <stdlib.h>
#include <stdio.h>
#define MONTH 42void Print(int arr[]);
void sort(int day, int week, int arr[]);int main(){int day = 0;int week = 0;int arr[MONTH] = { 0 };scanf("%d%d", &day, &week);sort(day, week, arr);Print(arr);system("pause");return 0;
}void Print(int arr[])
{printf(" 一 ");printf(" 二 ");printf(" 三 ");printf(" 四 ");printf(" 五 ");printf(" 六 ");printf(" 日 ");printf("\n");int count = 0;for (int i = 0; i < MONTH; i++){printf(" %2d ", arr[i]);count++;if (count % 7 == 0){printf("\n");}}
}void sort(int day, int week,int arr[])
{for (int i = 1; i <= day; i++){arr[week-1] = i;week++;}
}

 

优化版

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>void PrintData(int day, int wx)
{if (day<28 || day>31){return;}if (wx<0 || wx>6){return;}char Space[] = { "    " };char week[] = { "  日  一  二  三  四  五  六\n" };printf(week);int k = 0;for (int i = 0; i < wx; i++){printf(Space);k++;}for (int i = 1; i <=day; i++){printf("%4d", i);k++;if (k % 7 == 0){printf("\n");}}printf("\n");
}int main()
{int day = 0, wx = 0;scanf("%d%d", &day, &wx);PrintData(day, wx);system("pause");return 0;
}

 

更多推荐

C语言练习4

本文发布于:2024-03-14 08:36:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1736074.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语言

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!