C语言基础编程题1

  1. 编写程序,求0-100内所有素数打印输出,每4个换⾏。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main(){
    int step=0;
      for(int i=0;i<100;i++){
        int num=2;
        int b=i-1;
        
        for (int a=2; a <= b; a++)
        {
            if((i%a)!=0)
            {num++;}
        }
        
        if(b==(num-1))
        {   
            step++;
            printf("%3d ",i);
            if(step%4==0)
            {printf("\n");}
            
        }
        
      }

    printf("\n"); 
         
    system("pause");
    return 0;

}
  1. 编写程序;输⼊⼀个字符,若是⼤写字⺟,转换成小写字 ⺟,若是小写字⺟,则转换成⼤写字⺟输出
#include<stdio.h>
#include<stdlib.h>

int main(){
    char a;
    printf("Please enter :");
    scanf("%c",&a);
    
    if (a>64&&a<91)
    {
        a=a+32;
        printf("%c \n",a);
    }
    else if (a>96&&a<123)
    {
        a=a-32;
        printf("%c \n",a);
    }
    

    system("pause");
    return 0;
}
  1. 题⽬内容:36块砖,36⼈搬,男搬4,⼥搬3,两个小孩 抬1块,要求⼀次全搬完。
#include<stdio.h>
#include<stdlib.h>

int main()
{
   int sum=36,num=36;

   int b=1,g=1,c=2;
   for(c=2;c<36;c=c+2){
    for(b=1;b<9;b++){
        for(g=1;g<12;g++){
            if(4*b+3*g+0.5*c==36&&b+g+c==36)
            {printf("boy:%d girl:%d child:%d \n",b,g,c);}
        }
    }
   }
    system("pause");
    return 0; 
}
  1. 题⽬内容:⼀辆卡⻋违反交通规则,撞⼈逃逸。现场三 ⼈⽬击事件,但都没有记住⻋号,只记下⻋的⼀些特征。甲 说:牌照的前两位数字是相同的;⼄说:牌照的后两位数字 是相同的;丙是位数学家,他说:四位的⻋号正好是⼀个整 数的平⽅。请根据以上线索求出⻋牌号
#include<stdio.h>
#include<stdlib.h>


int main()
{
   int num;
   int v;
   int a,b,c,d;
   for(num=32;num<100;num++){
         v=num*num;
         a=v/1000;
         b=(v/100)%10;
         c=(v%100)/10;
         d=(v%100)%10;
         if(a==b&&c==d){
            printf("The numberis :%d\n",v);
         }
   }
    system("pause");
    return 0;
}
  1. 求解S=1+(1+2)+…+(1+2+3+…+n)的结果值,其中n值由 键盘输⼊。
#include<stdio.h>
#include<stdlib.h>


int main()
{
   int n;
   printf("Please enter n:");
   scanf("%d",&n);
   int s=1,r=0;
   do{
    r=r+n*s;
    s++;
    n--;
   }while(n!=0);

   printf("The result is %d\n",r);
    system("pause");
    return 0;
}

6.打印100-1000以内所有的水仙花数
水仙花数:一个三位数的每一位的立方和等于这个数,这个数被称为水仙花数

#include<stdio.h>
#include<stdlib.h>
#include <math.h>

int main()
{
   int a;
   for(a=100;a<1000;a++)
   {
    int x=pow(a/100,3);
    int y=pow((a%100)/10,3);
    int z=pow(a%10,3);
   
    if(x+y+z==a)
    {
        printf("%d ",a);
    }

   }

    system("pause");
    return 0;
}
  1. 求1000以内所有的完数
    完数:完美的数字,一个数的所有因子,除去他本身相加还等于这个数。
    6的因子:1 2 3 6
    1 + 2 + 3 = 6
#include<stdio.h>
#include<stdlib.h>


int main()
{
   int a,b;
   int sum=0;
   for(a=1;a<1000;a++)
   {
    for(b=1;b<a;b++)
    {
        if(a%b==0)
        {
            sum=sum+b;
        }
    }

    if(sum==a){
        printf("%d ",a);
    }
    sum=0;//清零

   }

    system("pause");
    return 0;
}

8.输入一个字符串,输出字符串中有多少个空格

#include <stdio.h>

int count_space(char *str)
{
    int num=0;
    while(*str!='\0')
    {
        if(*str==' ')
        {
         num++;
         str++;
        }
        else
        {
         str++;
        }

    }
    return num;
}

int main(int argc ,char*argv[])
{
    char s1[1006];
    printf("Please enter the str:");
    gets(s1);

    int result=count_space(s1);

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

    return 0;


}

9.输入一串字符串,在指定位置插入另一串字符串

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int argc,char *argv[])
{
   int d;
   char n_str[1006];
   char src[1006];
//   char str[]="helloword";
   char str[1006];
   printf("Please enter the str:\n");
   scanf("%s",str);
  printf("Please input the place:\n");
  scanf("%d",&d);
  printf("Please input the src:\n");
  scanf("%s",src);

  int len=strlen(src);
  for(int i=0;i<strlen(str);i++)
  {
        n_str[i]=str[i];

    if(i==d-1)
    {
        for(int b=0;b<len;b++)
        {
            n_str[d+b-1]=src[b];
        }
        for(int c=0;c<strlen(str)-d+1;c++)
        {
         n_str[d+len+c-1]=str[d+c-1];
        }
        break;
    }

  }
//    printf("%d",len);
   printf("The new str is:%s\n",n_str);

    system("pause");
    return 0;
}

10.使用冒泡排序,对输入的任意长度的数组进行从小到大的排序。

#include <stdio.h>
#include <stdlib.h>

void Bubble(int src[],int a)
{
    int m,n;
    for(m=0;m<a-1;m++)
    {
        for(n=0;n<a-m-1;n++)
        {
            if(src[n]>src[n+1])
            {
                int temp=src[n];
                src[n]=src[n+1];
                src[n+1]=temp;
            }
        }
    }
}

int main(int argc,char **argv)
{   
    int src[1006];
    printf("Please enter numbers:\n");

    int i=0;
    do
    {
        scanf("%d",&src[i]);
        i++;
    }while(getchar()!='\n');

    Bubble(src,i);

    for(int j=0;j<i;j++)
    {
        printf("%d ",src[j]);
    }
    
    printf("\n");
    system("pause");
    return 0; 

}

11.输入一个字符串,将所有的大写字母转化为小写字母,所有的小写字母转化为大写字母,所有的数字型字符转 为.其它字符转为*

#include <stdio.h>

char*func(char *str)
{
    char*p=str;

    while(*p!='\0')
    {
        if (*p>64&&*p<91)
    {
        *p=*p+32;
        p++;
    }
    else if (*p>96&&*p<123)
    {
        *p=*p-32;
        p++;
    }
    else if (*p>='0'&&*p<='9')
    {
        *p='.';
        p++;
    }
    else
    {
        *p='*';
        p++;
    }
    
    }

    return str;
}

int main(int argc,char *argv[])
{
    char s1[1006];
    printf("Please enter the str:");
    gets(s1);
    printf("before str=%s\n",s1);

    char*ch=func(s1);

    printf("after str=%s\n",ch);

    return 0;

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值