紫书第四章习题(函数和递归)

A

https://vj.ti12z.cn/b6a01d0138a4322d56d4aa6c3311794a?v=1564805519


//Full of love and hope for life

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#define inf 0x3f3f3f3f
//https://paste.ubuntu.com/

using namespace std;

int a;
char s[100],ss[100];
int win;
int lose;
int len=strlen(s);
int chance;

void guess(char ch){
    int bad=1;
    for(int i=0;i<strlen(s);i++){
        if(s[i]==ch){
            len--;
            s[i]=' ';
            bad=0;
        }
    }
    if(bad) --chance;
    if(!chance) lose=1;
    if(!len) win=1;
}

int main()
{
   while(cin >> a){
        if(a==-1){
            break;
        }
    cin >> s;
    cin >> ss;
    cout << "Round " << a << endl;
    win=0;
    lose=0;
    len=strlen(s);
    chance=7;
    for(int i=0;i<strlen(ss);i++){
        guess(ss[i]);
        if(win||lose){
            break;
        }
    }
    if(win){
        cout << "You win." << endl;
    }
    else if(lose){
        cout << "You lose." << endl;
    }
    else{
        cout << "You chickened out." << endl;
    }
   }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一些 C++ 中的函数习题: 1. 编写一个函数,计算 n 的阶乘 ```cpp int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } } ``` 2. 编写一个函数,计算斐波那契数列的第 n 项 ```cpp int fibonacci(int n) { if (n == 0 || n == 1) { return n; } else { return fibonacci(n - 1) + fibonacci(n - 2); } } ``` 3. 编写一个函数,计算一个数字的二进制表示中 1 的个数 ```cpp int countOnes(int n) { if (n == 0) { return 0; } else { return (n % 2 == 1) + countOnes(n / 2); } } ``` 4. 编写一个函数,计算一个数字的反转 ```cpp int reverse(int n) { if (n < 10) { return n; } else { return (n % 10) * pow(10, floor(log10(n))) + reverse(n / 10); } } ``` 5. 编写一个函数,计算一个字符串的长度 ```cpp int stringLength(string str) { if (str == "") { return 0; } else { return 1 + stringLength(str.substr(1)); } } ``` 6. 编写一个函数,判断一个字符串是否是回文字符串 ```cpp bool isPalindrome(string str) { if (str.length() <= 1) { return true; } else if (str[0] != str[str.length() - 1]) { return false; } else { return isPalindrome(str.substr(1, str.length() - 2)); } } ``` 7. 编写一个函数,计算一个数组中所有元素的和 ```cpp int arraySum(int arr[], int size) { if (size == 0) { return 0; } else { return arr[0] + arraySum(arr + 1, size - 1); } } ``` 8. 编写一个函数,计算一个数组中所有元素的平均值 ```cpp double arrayAverage(int arr[], int size) { if (size == 0) { return 0; } else { return (arr[0] + (size - 1) * arrayAverage(arr + 1, size - 1)) / size; } } ``` 9. 编写一个函数,计算一个数组中的最大值 ```cpp int arrayMax(int arr[], int size) { if (size == 1) { return arr[0]; } else { return max(arr[0], arrayMax(arr + 1, size - 1)); } } ``` 10. 编写一个函数,计算一个数组中的最小值 ```cpp int arrayMin(int arr[], int size) { if (size == 1) { return arr[0]; } else { return min(arr[0], arrayMin(arr + 1, size - 1)); } } ``` 希望这些练习题能够帮助你练习 C++ 中的函数

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值