牢固的基础是靠着长时间的锻炼积累出来的
编程靠的绝对不是天赋,而是经验和积累
为什么说这句话呢?对于我们很多刚刚学习C语言的朋友来说,我们首要做的就是通过C语言来锻炼自己的编程逻辑和编程思维,只有这样才能够在之后的多种语言,数据结构和算法的学习中能够更加的顺利。
在前期的学习之中应该保持充分的自信,不要过早的否定自己,或许一开始有着很多的困难,但是当你慢慢熟练的积累之后你会发现它没我们想象中那么难,我们只需要通过自己的努力去锻炼,多敲一些代码,将我们出现问题的原因记录下来,这样在我们之后遇见相同的问题或者难以解决的时候,看看自己的笔记,就可以顺利的完成了,加油。
1. **冒泡法** 重中之重的一个程序
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[16] = { 1, 5, 6, 9, 8, 7, 6, 12, 15, 16, 18, 13, 21, 25, 26, 28 };
int temp;
int i, m, n;
for (n = 0; n < 15; n++)//进行9次
for (m = 0; m < 15 - n; m++)//将相邻两位进行比较,将小的往后移
{
if (arr[m] < arr[m + 1])
{
temp = arr[m];
arr[m] = arr[m + 1];
arr[m + 1] = temp;
}
}
for (i = 0; i < 16; i++)
printf("The order is:%d\n", arr[i]);
system("pause");
}
2. **数字遍历**
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 123456;
int i;
for (i = n; i; i/=10)//数位遍历;
{
printf("%d ", i % 10);
}
system("pause");
return 0;
}
3.**递归输出1234**
#include <stdio.h>
#include <stdlib.h>
void PrintNum(int num){//将1234输入进;
if (num > 9){
PrintNum(num / 10);//递归进行调用 从首位开始输出
}
printf("%d ", num % 10);
}
int main()
{
PrintNum(1234);
system("pause");
return 0;
}
4. **计算一个数的位数 按位输出 逆序输出**
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int factor(int x){//计算出位数
int count = 1;
for (x; x / 10 != 0; x /= 10){
count += 1;
}
printf("%d", count);
return (count);
}
void Factor(int a){//按位输出
float n = factor(a);
int i, j;
printf("按位输出为:\n");
for (n; n> 0; n--){
i = a / pow(10, n - 1);
j = i % 10;
printf("%d ", j);
}
}
void PrintNum(int num){
int i;
for (i = num; i; i /= 10)//数位遍历;
{
printf("%d ", i % 10);
}
}
int main(){
int x;
printf("请输入一个不大于五位的正整数:\n");
scanf("%d", &x);
factor(x);
printf("\n");
Factor(x);
printf("\n");
printf("逆序输出这个正整数每一位:\n");
PrintNum(x);
system("pause");
return 0;
}
5. **完数**
#include <stdio.h>
#include <stdlib.h>
int Sum(int n)
{
int i;
int sum = 0;
for (i = 1; i < n; i++){
if (n%i==0)
sum += i;
}
return sum;
}
int main()
{
int j;
for (j = 2; j < 1000; j++)
{
if (Sum(j) == j)
printf("%d is a wanshu!\n", j);
}
system("pause");
return 0;
}
6. **反向输出字符串**
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
int i, j,k;
char c[200], c1;
printf("Enter a string :\n");
scanf("%s", c);
k = strlen(c);//求字数串长度;
for (i = 0, j = k - 1; i < j; i++, j--){
c1 = c[i];
c[i] = c[j];
c[j] = c1;
}
printf("%s", c);
system("pause");
return 0;
}
7. **杨辉三角形**
#include <stdio.h>
#include <stdlib.h>
int main(){
int i, j;
int a[7][7];
for (i = 0; i <= 6; i++){
a[i][i] = 1;//将为1的数位,先全部输出;
a[i][0] = 1;
}
for (i = 2; i <= 6; i++){
for (j = 1; j <= i - 1; j++){
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];//将不为1的数位输出
}
}
for (i = 0; i <= 6; i++){
for (j = 0; j <= i; j++){
printf("%4d",a[i][j]);
}
printf("\n");
}
system("pause");
return 0;
}
8. **回文字符串**
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char s[100];
printf("enter a string:\n");
gets(s);
int i, j, n;
n = strlen(s);
for (i = 0, j = n - 1; i < j; i++, j--){
if (s[i] != s[j]){//判断第一位和最后一位是否相等
break;
}
}
if (i>j){//当全面相等的话 ,则判定位回文数;
printf("This is a huiwen string.\n");
}
else{
printf("This not is a huiwen string.\n");
}
system("pause");
return 0;
}
9. **从字符数组中删掉d中的字符**
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char s[200];
char a;
printf("Enter a string.\n");
gets(s);
printf("Enter a charater.\n");
a = getchar();
int i,j;
for (i = j = 0; s[i] != '\0'; i++){
if (s[i] != a){
s[j++] = s[i]; //将不同的取出来放进新的字符串之中;
}
}
if (s[i] == '\0'){//将之后重复的最后一位删除掉;
s[j] = s[i];
}
printf("%s\n", s);
system("pause");
return 0;
}
10. **从100米的地方落下的球,每一次弹起来是下落的一半,则第十次弹起多高,总弹多少米**(题好像大概就是这样)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
double sum = 0, sum_down = 0, sum_up = 0, h = 0;
double i;
for (i = 0.0; i <= 9.0; i++){
sum_down =sum_down+ 100*pow(1.0/ 2.0, i);//计算每一次下落的总距离;
}
for (i = 1.0; i <= 9.0; i++){
sum_up = sum_up+100*pow(1.0 / 2.0, i);//计算每一次上升的总距离;
}
sum = sum_down + sum_up;//计算总距离;
h = 100*pow(1.0 / 2.0, 10);//计算第十次弹起来的高度
printf("在第十次落地,一共经过了%lf米\n", sum);
printf("第十次再弹起 有%lf米\n.", h);
system("pause");
return 0;
}
11.**选择法,以小到大开始排列**
#include <stdio.h>
#include <stdlib.h>
void Sort(int arr[], int n){
int i, j, temp;
for (i = 0; i < n - 1; i++){
for (j = i + 1; j < n; j++){
if (arr[j] < arr[i]){//拿第一个和第0个作比较,将小的放到0的位置上去。
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
int main(){
int a[10];
printf("Enter a array:\n");
int i;
for (i = 0; i < 10; i++){
scanf("%d", &a[i]);
}
Sort(a, 10);
printf("The sorted array:\n");
for (i = 0; i < 10; i++){
printf("%d", a[i]);
}
printf("\n");
system("pause");
return 0;
}
12.0 **输入16进制,以10进制输出**
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int i,j=0;
int x, y=0;
printf("Enter a Number:\n");
scanf("%d", &i);
while (i!=0){
x = i % 10;//除10求余取得个位
y += x*pow(16, j);//乘以进制;
i /= 10;//求上一位
j++;
}
printf("输出十进制数为:%d\n",y);
system("pause");
return 0;
}
13. **猴子摘桃**
#include <stdio.h>
#include <stdlib.h>
int main(){
int i, N=1;
for (i = 1; i < 10; i++){
N = (N + 1) * 2;
}
printf("猴子第一天摘取了%d个桃子。\n",N);
system("pause");
return 0;
}
14. 正整数分解质因数
#include <stdio.h>
#include <stdlib.h>
int main(){
int i, n;
printf("请输入您要输入的数字:\n");
scanf("%d", &n);
printf("90=");
for (i = 2; i <= n; i++){
while (n >i){
if (n%i == 0){
printf("%d*", i);
n = n / i;
}
else{
break;
}
}
}
printf("%d\n", n);
system("pause");
return 0;
}
15. **迭代法求平方根**
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Epsilon 1.0E-6
int main()
{
float a, x0, x1;
printf("请输入要求的数:\n");
scanf("%f", &a);
x0 = a / 2;
x1 = (x0 + a / x0) / 2;
while (fabs(x1 - x0) >= Epsilon){
x0 = x1;
x1 = (x0 + a / x0) / 2;
}
printf("平方根为:%f\n",x1);
system("pause");
return 0;
}
16.**输入字母 判断是周几**
#include <stdio.h>
#include <stdlib.h>
int main(){
char letter;
printf("please input the first letter of someday\n");
while ((letter = getchar()) != 'Y'){
switch (letter)
{
case 'S':printf("please input the second letter:\n");
if ((letter = getchar()) == 'a')
printf("Saturday\n");
else if ((letter = getchar()) == 'u')
printf("Sunday\n");
else
printf("data error.");
break;
case 'F':printf("Friday");
break;
case 'M':printf("Monday");
break;
case 'T':printf("please input the second letter:\n");
if ((letter = getchar()) == 'u')
printf("Tuesday\n");
else if ((letter = getchar()) == 'h')
printf("Thursday\n");
else
printf("data error.");
break;
case 'W':printf("Wednesday");
break;
default:
break;
}
}
system("pause");
return 0;
}
17.**二进制按位取反**
#include <stdio.h>
#include <stdlib.h>
int main(){
int a = 12;
int b;
b = ~a;
printf("将a取反得到b为:%d\n",b);
a = ~a;
printf("将a取反得到:%d\n",a);
system("pause");
return 0;
}
18. **用指针顺序输出a和b的最大最小值**
#include <stdio.h>
#include <stdlib.h>
int main(){
int *p1, *p2, *p, a, b;
printf("please enter two integer numbers:");
scanf("%d %d", &a, &b);
p1 = &a;//p1指向a的地址
p2 = &b;//p2指向b的地址
if (a < b){
p = p1; p1 = p2; p2 = p;
}
printf("a=%d,b=%d\n", a, b);
printf("max=%d,min=%d\n", *p1, *p2);
system("pause");
return 0;
}
在C语言的学习上,看书只是最开始的办法,我们应该明白更多的都是需要依靠自己的勤奋锻炼来进行实践,只有长时间的敲代码才能够更快的提升自己的能力。