算法课习题

这篇博客涵盖了多种算法习题,包括最大公约数、鸡兔同笼问题、按位输出、阶乘计算、求中位数等。文章讨论了如何解决这些问题,提供了不同算法思路,如循环、递归和数组存储,并强调了在处理整数按位输出时的注意事项。此外,还涉及了数字翻译、找零钱问题和整除判断等实用算法。
摘要由CSDN通过智能技术生成

题目

最大公约数

在这里插入图片描述

#include <stdio.h>

void fun(int a,int b){
   
    int c;
    c = a % b;
    if(c == 0)
        printf("%d\n",b);
    else
        fun(b,c);
}
int main()
{
   
   int n,a,b;
   scanf("%d",&n);
   while (n--)
   {
   
        scanf("%d %d",&a,&b);
        fun(a,b);
   }
   return 0;
}

鸡兔同笼

在这里插入图片描述

#include <stdio.h>

int main()
{
   
   int n,i,a;
   scanf("%d",&n);
   while (n--)
   {
   
        scanf("%d",&a);
              if (a%2!=0)
          printf("0 0\n");
        else if (a % 4!=0)
          printf("%d %d\n",a/4+1,a/2);
        else
          printf("%d %d\n",a/4,a/2);
   }
   return 0;
}

按位输出

在这里插入图片描述

#include<stdio.h>
#include<string.h>
char *letter[]={
   "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","twenty-one","twenty-two",
"twenty-three","twenty-four","twenty-five","twenty-six","twenty-seven","twenty-eight","twenty-nine","thirty","thirty","thirty-one","thirty-two"};

void change(char str[],int a[]){
   
    int i;
    a[0]=strlen(str);
    for(i=1;i<=a[0];i++){
   
        a[i]=str[a[0]-i]-'0';
    }
    if(str[0]=='-') a[0]--;
}

int main()
{
   
    int num[100];
    char str[100];
    while(scanf("%s",str)!=EOF){
   
        change(str,num);
        int i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值