11/27

汽水瓶
某商店规定:三个空汽水瓶可以换一瓶汽水,允许向老板借空汽水瓶(但是必须要归还)。
小张手上有n个空汽水瓶,她想知道自己最多可以喝到多少瓶汽水。
数据范围:输入的正整数满足
1

n

100

1≤n≤100

注意:本题存在多组输入。输入的 0 表示输入结束,并不用输出结果。

点击查看代码
#include <iostream>
using namespace std;

int main() {
    int a  = 10;
    while(a--){
        int s ,x , y, z, result;
        result = 0;
        cin >> x; 
        z = x % 3;
        while(x / 3 != 0){
            result += x / 3;
            x = x / 3;
        }
        s = result + z + 1;
        if(s %3 == 0){
            result = result + 1;
        }
        cout << result << endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")
自己想多了,其实相当于两个空汽水瓶换一个汽水

明明的随机数
明明生成了
N
N个1到500之间的随机整数。请你删去其中重复的数字,即相同的数字只保留一个,把其余相同的数去掉,然后再把这些数从小到大排序,按照排好的顺序输出。

数据范围:
1

n

1000

1≤n≤1000 ,输入的数字大小满足
1

v
a
l

500

1≤val≤500
``

点击查看代码
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> a;
    for(int i = 0 ; i < n; i++ ){
        int b;
        cin >> b;
        a.push_back(b);
    }
    sort(a.begin(),a.end());
    auto last = unique(a.begin(), a.end());
    a.erase(last, a.end());
    for (int i = 0; i < a.size() ; i++) {
        cout << a[i] << endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")
使用sort,unique和erase简单处理

进制转换
写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。

数据范围:保证结果在
1≤n≤2 31 −1

点击查看代码
#include <iostream>
#include <vector>
using namespace std;

int main() {
    string a;
    vector<char> ab;
    cin >> a;
    for (char c : a) {
        ab.push_back(c);
    }
    int b = ab.size();
    int result = 0;
        for (int i = 2; i < b; i++) {
        // 转换字符为相应的十六进制值
        int hexValue = 0;
        if (ab[i] >= '0' && ab[i] <= '9') {
            hexValue = ab[i] - '0';  // 数字字符转换为对应的十进制值
        } else if (ab[i] >= 'A' && ab[i] <= 'F') {
            hexValue = ab[i] - 'A' + 10;  // 字符 'A'-'F' 转换为 10-15
        } else if (ab[i] >= 'a' && ab[i] <= 'f') {
            hexValue = ab[i] - 'a' + 10;  // 字符 'a'-'f' 转换为 10-15
        }

        // 计算十六进制值的贡献并加到 result 中
        result = result * 16 + hexValue;
    }
    cout << result ;
    return 0;
}
// 64 位输出请用 printf("%lld")
很简单
posted @   Ze1da  阅读(3)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示