2021-09-28每日刷题打卡

一、POJ-1011:Sticks

1.1 问题描述

George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.

题目大意:

有个男的,把一些等长的棍子剪成不等长的棍子,但他想把它们还原,又忘记了原来有多少根棍子和棍子的长度,问你棍子可能的最小长度

1.2 问题解决

#include<iostream>
#include<algorithm>
using namespace std;
 
int cmp(const void* a,const void* b)
{
	return *(int*)b-*(int*)a;
}
 
int n;  
 
bool dfs(int* stick,bool* vist,int len,int InitLen,int s,int num)  
{                                                                  
	if(num==n)
		return true;
 
	int sample=-1;
	for(int i=s;i<n;i++)
	{
		if(vist[i] || stick[i]==sample)  
			continue;
 
		vist[i]=true;
		if(len+stick[i]<InitLen)
		{
			if(dfs(stick,vist,len+stick[i],InitLen,i,num+1))
				return true;
			else
				sample=stick[i];
		}
		else if(len+stick[i]==InitLen)
		{
			if(dfs(stick,vist,0,InitLen,0,num+1))
				return true;
			else
				sample=stick[i];
		}
		vist[i]=false;
 
		if(len==0)  
			break;  
	}
	return false;
}
 
int main(void)
{
	while(cin>>n && n)
	{
		int* stick=new int[n];
		bool* vist=new bool[n];
		int sumlen=0;
 
		for(int i=0;i<n;i++)
		{
			cin>>stick[i];
			sumlen+=stick[i];
			vist[i]=false;
		}
 
		qsort(stick,n,sizeof(stick),cmp);
		int maxlen=stick[0];   
 
		bool flag=false;
		                     
		for(int InitLen=maxlen;InitLen<=sumlen-InitLen;InitLen++)  
		{   
			if(!(sumlen%InitLen) && dfs(stick,vist,0,InitLen,0,0))
			{
				cout<<InitLen<<endl;
				flag=true;
				break;
			}
		}
		if(!flag)
			cout<<sumlen<<endl;
 
		delete stick;
		delete vist;
	}
	return 0;
}

二、Leetcode-160:相交链表

2.1 问题描述

img

2.2 问题解决

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode preheadA = new ListNode();
        preheadA.next = headA;

        while(true)
        {
            ListNode curB = headB;
            while(curB != null)
            {
                if(curB == preheadA.next)
                    return curB;
                else
                    curB = curB.next;
            }

            if(preheadA.next != null)
                preheadA = preheadA.next;
            else
                break;
        }

        return null;
    }
}

三、Leetcode-206:反转链表

3.1 问题描述

img

3.2 问题解决

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode prehead;
        prehead.next = nullptr;

        while(head != nullptr)
        {
            ListNode *tmp = head;
            head = head->next;
            tmp->next = prehead.next;
            prehead.next = tmp;
        }

        return prehead.next;
    }
};

四、生词

stick n. 树枝,棍

compute vt. 计算;用计算机计算

五、参考文章

  1. POJ1011-Sticks
  2. POJ - 1011(dfs回溯+剪枝)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值