理解了下題意,應該就是計算達到木板伸出的要求長度,所需的最少塊數
碰到的一點小問題,表達式1/2默認爲整型,所以結果爲0。
輸出是要求輸入0.00爲結束符,而不是輸入一個,計算一個,由於要一次性輸出,本想考慮動態數組,可是不知道預先輸入幾個數字,後改用鏈表,使用鏈表時,犯了個致命錯誤,頭節點未申請就使用,尾節點未串起來,好久沒用都生疏了,看到AC的瞬間,無比開心。
#include <iostream>
using namespace std;
struct Node
{
public:
int data;
Node* next;
};
class List
{
Node*head;
Node* tail;
public:
//默認構造函數
List(){head=NULL;}
void pushList(int a);//鏈表結點的插入
void outputList();//鏈表結點的輸出
};
void List::pushList(int a)
{
if(head == NULL)
{
head=(Node*)new Node;
head->data=a;
head->next=NULL;
tail=head;
}
else
{
Node* s=(Node*)new Node;
s->data = a;
s->next = NULL;
tail->next = s;
tail = s;
}
}
void List::outputList()
{
Node* current = head;
while(current!=NULL)
{
cout<<current->data<<" card(s)"<<endl;
current=current->next;
}
}
int main()
{
float m;
List mlist;
while(cin>>m)
{
if(m == 0.00)
{
break;
}
double x=0.5;
int n = 1;
while(x<m)
{
n++;
x = x+1.0/(n+1);
}
mlist.pushList(n);
}
mlist.outputList();
return 0;
}
一點容易忘記的C++語言特性:
1. 默認的繼承訪問權限。struct是public的,class是private的
2. 類成員函數可以訪問私有成員