关闭

C初级阶段练习题目(二)

标签: C语言 初级 练习题目
73人阅读 评论(0) 收藏 举报
分类:

//***********11、有1,2,3,4排成三位数,无重复且互不相同**************

intmain()
{
                inti = 0;
                intj = 0;
                intk = 0;
                intcount = 0;
                for(i = 1; i < 5; ++i)
                {
                                for(j = 1; j < 5; ++j)
                                {
                                                for(k = 1; k < 5; ++k)
                                                {
                                                                if(i != j&&j != k&&i != k)
                                                                {
                                                                                printf("%d%d%d   ", i, j, k);
                                                                                count++;
                                                                                if(count % 4 == 0)
                                                                                {
                                                                                                printf("\n");
                                                                                }
                                                                }
                                                }
                                }
                }
                return0;
}

//***********12、在屏幕上输出2000年之前的所有闰年和闰年的个数**********
intmain()
{
                intyear = 0;
                intcount = 0;
                for(year = 0; year < 2000; ++year)
                {
                                if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                                {
                                                printf("%5d", year);
                                                count++;
                                                if(count % 15 == 0)
                                                {
                                                                printf("\n");
                                                }
                                }
                }
                printf("\n");
                printf("闰年的个数=%d", count);
                return0;
}
//********13、求2/1,3/2,5/3,8/5,13/8,21/13...前20项之和**********
intmain()
{
                inti = 0;
                floatsum = 0.0;
                floata = 2.0;
                floatb = 1.0;
                for(i = 0; i < 20; ++i)
                {
                                sum = sum + a / b;
                                a = a + b;
                                b = a - b;
                }
                printf("%f", sum);
                return0;
}
//****************14、s=a+aa+aaa+aaaa+aaaaa+aa...aaaaaa***********
intmain()
{
                inta = 0;
                intn = 0;
                inti = 0;
                intsum = 0;
                intnum = 0;
                intlastnum = 0;
                printf("请输入数据:");
                scanf("%d", &a);
                printf("请输入循环因子:");
                scanf("%d", &n);
                for(i = 0; i < n; ++i)
                {
                                num = a * pow(10, i);
                                sum = sum + num + lastnum;
                                lastnum = num;
                }
                printf("%d", sum);
                return0;
}

//****************15、一球从100米高度自由落下,每次落地后反跳原高度的一**************
//****************半再落下,求第10次落地时,共经过多少米?第10次反弹多高*************

intmain()
{
                floathigh = 100;
                inti = 0;
                floattotal_length = high;
                for(i = 2; i <= 10; i++)
                {
                                high = high / 2;
                                total_length = total_length + high*2;
                }
                printf("总长度=%f\n", total_length);
                printf("第10次反弹高度=%f\n", high);
                return0;
}

//***********16、猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
//第二天早上将剩下的桃子吃掉了一半,又多吃了一个,以后每天早上吃前一天剩下的一半零一个
//到第10天早上想再吃时,见只剩下一个桃子,求第一天共摘了多少桃子?********************
intmain()
{
                intlastnum = 0;
                intnownum = 1;
                intday = 9;
                while(day > 0)
                {
                                lastnum = (nownum + 1) * 2;
                                nownum = lastnum;
                                day--;
                }
                printf("总数=%d", nownum);
                return0;
}

//***************17、1-1/2+1/3-1/4...+1/99-1/100*******************
intmain()
{
                inti = 0;
                floatevennum = 0.0;
                floatoddnum = 0.0;
                for(i = 2; i <= 100; i = i + 2)
                {
                                evennum = evennum - 1.0 / i;
                }
                for(i = 3; i < 100; i = i + 2)
                {
                                oddnum = oddnum + 1.0 / i;
                }
                printf("总数和=%f\n", evennum + oddnum + 1);
                return0;
}

//**************18、1+2!+3!+...+20!*******************
intmain()
{
                inti = 0;
                longsum = 1;
                longnum = 0;
                longlastnum = 1;
                for(i = 2; i <= 20; i++)
                {
                                num = i * lastnum;
                                sum = sum + num;
                                lastnum = num;
                }
                printf("sum=%d", sum);
                return0;
}

//**************19、判断一个数是否为回文数(正反序时同一个数字如12321、3443)
intmain()
{
                intnum = 0;
                intcount = 0;
                scanf("%d", &num);
                while(num > 0)
                {
                                count++;
                                num /= 10;
                }
                switch(count)
                {
                case1:
                                printf("该数不是回文数");
                                break;
                case2:
                                if(num / 1 % 10 == num / 10 % 10)
                                {
                                                printf("该数是回文数");
                                                break;
                                }
                                else
                                {
                                                printf("该数不是回文数");
                                                break;
                                }
                case3:
                                if(num / 1 % 10 == num / 100 % 10)
                                {
                                                printf("该数是回文数");
                                                break;
                                }
                                else
                                {
                                                printf("该数不是回文数");
                                                break;
                                }
                case4:
                                if((num / 1 % 10 == num / 1000 % 10) && (num / 10 % 10 == num / 100 % 10))
                                {
                                                printf("该数是回文数");
                                                break;
                                }
                                else
                                {
                                                printf("该数不是回文数");
                                                break;
                                }
                case5:
                                if((num / 1 % 10 == num / 10000 % 10) && (num / 10 % 10 == num / 1000 % 10))
                                {
                                                printf("该数是回文数");
                                                break;
                                }
                                else
                                {
                                                printf("该数不是回文数");
                                                break;
                                }
                default:
                                break;
                }
                return0;
}

   
  

//****************20、判断一个字符串是否为回文字符串********************
#include<string.h>
int  IsPalindrome(char*a)
{
                char*p =a;
                while(*p)
                {
                                p++;  //找到\0的位置
                }
                p--; //找到最后一个字符的位置
                while(a< p)   //a为数组首元素地址,p为最后一个字符的位置
                {
                                if(*a!= *p)
                                {
                                                return-1;
                                }
                                a++;
                                p--;
                }
                return1;
}


intmain()
{
                chararr[] ="abcddcbb";
                intret = IsPalindrome(arr);
                if(ret == 1)
                 {
                                 printf("该字符串是回文字符串");
                 }
                else
                                printf("该字符串不是回文字符串");
                return0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:17186次
    • 积分:512
    • 等级:
    • 排名:千里之外
    • 原创:54篇
    • 转载:9篇
    • 译文:0篇
    • 评论:4条
    最新评论