习题6

1、编程实现任意输入一个正整数,输出这个正整数之内的所有奇数的和以及所有偶数的和。 (for实现)

#include <stdio.h>

void main()

{

       int n,a,b;

       printf("请输入一个正整数:");

       scanf("%d",&n);

       if(n<0)   

              printf("输入错误");

       else

              for(a=2,b=0;b<=n;a+=2)

                     b=b+a;

              printf("偶数和为:%d\n",a);

 

              for(b=0,a=1;a<=n;a+=2)

                     b=b+a;

              printf("奇数和为:%d\n",b);

      

}

 

 

2、编程实现输入n和a(a是一位数字),求a+aa+aaa+…+aa…a。若n为4,a为2,则求2+22+222+2222。(for实现)

#include <stdio.h>

void main()

{     

       int n,a,m,p,i;

       printf("请输人两个数:");

       scanf("%d%d",&n,&a);

       for(m=a,p=a,i=1;i<n;i++)

       {

              p=p*10+a;

              m=m+p;

       }

       printf("%d\n",m);

}

 

 

3、编程实现猜数字游戏,随机产生一个1到100之间的整数,用户通过不停从键盘输入整数进行猜测,若大了就显示‘猜大了’,若小了就显示‘猜小了’,若猜对了就显示‘恭喜!猜中了!你共猜了n次’。(break)

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

void main()

{

       int i,x,n;//x输人的数,i随机值,n猜的次数

       printf("请输入一个1-100的整数:\n");

       scanf("%d",&x);

       srand((unsigned)time(NULL));

       i=rand()%100+1;

       if(x<0 || x>100)

              printf("输入错误");

       else

       {

              for(;;)

              {

             

                     if(x == i){

                            printf("恭喜!猜中了!你共猜了%d次",n);

                            break;

                     }

                     else if(x > i){

                            printf("猜大了\n");

                                   printf("请输入一个1-100的整数:\n");

                                   n++;

                                   scanf("%d",&x);

                     }

                     else {

                            printf("猜小了\n");

                                   printf("请输入一个1-100的整数:\n");

                                   n++;

                                   scanf("%d",&x);

                     }

 

              }

       }

}

 

 

4、用1张100元的人民币兑换5元、2元和1元的纸币(每一种都要有)共50张,问共有几种兑换方案?每种方案各个纸币有多少枚? (循环嵌套)

 

#include <stdio.h>

void main()

{

       int i,j,count=0;

       for(i=1;i<20;i++)

       {

              for(j=1;j<50;j++)

              {

                     if(5*i+2*j+(50-i-j)==100)

                     {

                            printf("5元有%d,2元有%d,1元有%d\n",i,j,50-i-j);

                            count++;

                     }

              }

      

       }

       printf("总共有%d种\n",count);

}

 

 

 

5、编程实现根据输入的正整数输出其相应的图形,输入5或9的图形如下。(循环嵌套)

 

#include<stdio.h>

int main()

{

       int i,j,n;

       printf("请输入一个数字:");

       scanf("%d",&n);

       if(n>0 && n<26)

       {

              for(i=1;i<=n;i++)

              {

                     for(j=0;j<=n-i;j++)

                     printf(" ");

                     for(j=1;j<=2*i-1;j++)

                            printf("%c",j+'@');

                     printf("\n");

              }

       }

       else

              printf("输入错误\n");

}

 

posted @ 2021-12-14 20:13  小耿想努力呀  阅读(141)  评论(0编辑  收藏  举报