1、一堆硬幣,一個機器人,如果是反的就翻正,如果是正的就拋擲一次,無窮多次后,求正反的比例
解答:是不是題目不完整啊,我算的是3:1
2、一個汽車公司的產(chǎn)品,甲廠占40%,乙廠占60%,甲的次品率是1%,乙的次品率是2%,現(xiàn)在抽出一件汽車時次品,問是甲生產(chǎn)的可能性
解答:典型的貝葉斯公式,p(甲|廢品) = p(甲 && 廢品) / p(廢品) = (0.4 × 0.01) /(0.4 × 0.01 + 0.6 × 0.02) = 0.25
3、k鏈表翻轉(zhuǎn)。給出一個鏈表和一個數(shù)k,比如鏈表1→2→3→4→5→6,k=2,則翻轉(zhuǎn)后2→1→4→3→6→5,若k=3,翻轉(zhuǎn)后3→2→1→6→5→4,若k=4,翻轉(zhuǎn)后4→3→2→1→5→6,用程序?qū)崿F(xiàn)
非遞歸可運行代碼:
#include
#include
#include
typedef struct node {
struct node next;
int data;
} node;
void createList(node head, int data)
{
node P, cur, new;
P = NULL;
cur = head;
while (cur != NULL) {
P = cur;
cur = cur->next;
}
new = (node )malloc(sizeof(node));
new->data = data;
new->next = cur;
if (P == NULL)
head = new;
else
P->next = new;
}
void printLink(node head)
{
while (head->next != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("%d ", head->data);
}
int linkLen(node head)
{
int len = 0;
while (head != NULL) {
len ++;
head = head->next;
}
return len;
}
node reverseK(node head, int k)
{
int i, len, time, now;
len = linkLen(head);
if (len < k) {
return head;
} else {
time = len / k;
}
node newhead, Pv, next, old, tail;
for (now = 0, tail = NULL; now < time; now ++) {
old = head;
for (i = 0, Pv = NULL; i < k; i ++) {
next = head->next;
head->next = Pv;
Pv = head;
head = next;
}
if (now == 0) {
newhead = Pv;
}
old->next = head;
if (tail != NULL) {
tail->next = Pv;
}
tail = old;
}
if (head != NULL) {
tail->next = head;
}
return newhead;
}
int main(void)
{
int i, n, k, data;
node head, newhead;
while (scanf("%d %d", &n, &k) != EOF) {
for (i = 0, head = NULL; i < n; i ++) {
scanf("%d", &data);
createList(&head, data);
}
printLink(head);
newhead = reverseK(head, k);
printLink(newhead);
}
return 0;
}