一、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 问题描述
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 问题描述
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. 计算;用计算机计算