251106 算法

问题 A: C002 赌徒

cpp 复制代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
ll arr[1005];
int main(){
    int n;
    while(scanf("%d",&n) != EOF){
        if(n == 0)break;
        for(int i = 0;i < n;i++){
            scanf("%lld",&arr[i]);
        }
        sort(arr,arr + n);
        long long max_winner = -1;
        bool found = false;

        for(int i = n - 1;i >= 0;i--){
            ll target = arr[i];

            for(int j = 0;j < n;j++){
                if(j == i)continue;
                int left = j + 1;
                int right = n - 1;
                while(left < right){
                    if(left == i){
                        left++;
                        continue;
                    }
                    if (right == i) {
                        right--;
                        continue;
                    }
                    ll sum = arr[j] + arr[left] + arr[right];
                    if(sum == target){
                        if(!found || target > max_winner){
                            max_winner = target;
                            found = true;
                        }
                        break;
                    }
                    else if(sum < target){
                        left++;
                    }else{
                        right--;
                    }
                }

                if(found && max_winner == target){
                    break;
                }

        }
        if(found)break;
    }
    if(found){
        cout << max_winner << endl;
    }else{
        cout << "no solution"<< endl;
    }
    }
}
cpp 复制代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
int arr[10005];

int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    int max_len = 0;
    for(int i = 0;i < n;i++){
        double temp;
        scanf("%lf",&temp);
        arr[i] = temp * 100;
        if(arr[i] > max_len) max_len = arr[i];
    }

    int left = 1;
    int right = max_len;
    int res = 0;
    while(left <= right){
        int mid = left + (right - left)/2;
        int cnt = 0;
        for(int i = 0;i < n;i++){
            int c = arr[i] / mid;
            cnt += c;
        }
        if(cnt >= k){
            res = mid;
            left = mid + 1;
        }else{
            right = mid - 1;
        }
    }
    double r = (double)res / 100;
    printf("%.2f\n",r);

}
cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int x, y;
} Point;


int compare_points(const void *a, const void *b) {
    Point *p1 = (Point *)a;
    Point *p2 = (Point *)b;
    if (p1->x != p2->x) return p1->x - p2->x;
    return p1->y - p2->y;
}


int point_exists(Point *points, int n, Point p) {
    int left = 0, right = n - 1;
    while (left <= right) {
        int mid = (left + right) / 2;
        int cmp = compare_points(&points[mid], &p);
        if (cmp == 0) return 1;
        if (cmp < 0) left = mid + 1;
        else right = mid - 1;
    }
    return 0;
}

int main() {
    int n;
    while (scanf("%d", &n) == 1 && n != 0) {
        Point *points = (Point *)malloc(n * sizeof(Point));
        for (int i = 0; i < n; i++) {
            scanf("%d %d", &points[i].x, &points[i].y);
        }
        
        
        qsort(points, n, sizeof(Point), compare_points);
        
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int x1 = points[i].x, y1 = points[i].y;
                int x2 = points[j].x, y2 = points[j].y;
                
                
                int dx = x2 - x1, dy = y2 - y1;
                
                
                Point p3 = {x1 - dy, y1 + dx};
                Point p4 = {x2 - dy, y2 + dx};
                if (point_exists(points, n, p3) && point_exists(points, n, p4)) {
                    ans++;
                }
                
                
                Point p5 = {x1 + dy, y1 - dx};
                Point p6 = {x2 + dy, y2 - dx};
                if (point_exists(points, n, p5) && point_exists(points, n, p6)) {
                    ans++;
                }
            }
        }
        
        
        ans /= 4;
        printf("%d\n", ans);
        
        free(points);
    }
    return 0;
}
cpp 复制代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
ll arr[1005];
int main(){
    int z;
    while(scanf("%d",&z) != EOF){
        while(z--){
            ll n;//硬币个数
            scanf("%lld",&n);
            ll sum = 0;
            ll i = 1;
            for(i = 1;sum < n;i++){
                sum += i;
            }
            if(sum == n)cout << i - 1 << endl;
            else{
                cout << i - 2 << endl;
            }
        }
    }
}
相关推荐
Bona Sun1 天前
单片机手搓掌上游戏机(十四)—pico运行fc模拟器之电路连接
c语言·c++·单片机·游戏机
Zero-Talent1 天前
位运算算法
算法
oioihoii1 天前
性能提升11.4%!C++ Vector的reserve()方法让我大吃一惊
开发语言·c++
不穿格子的程序员1 天前
从零开始刷算法——双指针-三数之和&接雨水
算法·双指针
合方圆~小文1 天前
AI摄像头精准识别技术依赖于深度算法
数据结构·数据库·数码相机·模块测试
小狗爱吃黄桃罐头1 天前
《C++ Primer Plus》模板类 Template 课本实验
c++
无限进步_1 天前
C语言数组元素删除算法详解:从基础实现到性能优化
c语言·开发语言·windows·git·算法·github·visual studio
松涛和鸣1 天前
16、C 语言高级指针与结构体
linux·c语言·开发语言·数据结构·git·算法
Booksort1 天前
【LeetCode】算法技巧专题(持续更新)
算法·leetcode·职场和发展
OJAC1111 天前
2026高校毕业生1270万!但这些学生却被名企用高薪“提前预定”!
算法