2020年1月30日 星期四

羅拉.特萊斯 建議我們說謝謝

影片連結

這位講者的語速好快,字幕都要跟不上了orz...

take in:接受
rehab facility:勒戒中心
facing life and death:面對生死
breadwinner:養家餬口的人
medium rare:(牛排)三分熟
→raw 生
→rare 一分熟
→medium 五分熟
→medium well 七分熟
→well done 全熟
size six shoes:六號鞋
inner circle:死黨、朋友圈
true the wheels:矯正車輪(似乎是指讓車輪受力均衡,以恢復安全性與性能)
wrap:扭曲
show up:出席

d041: 11219 - How old are you?

題目連結

解題思路:
date=(y1-y2)-(m1<m2)-(m1==m2&&d2>d1);
1.從(y1-y2)可以得知大概的年齡。而可能因為最新的天數未滿一年,需要減1,所以繼續判斷。
2.需要減一有兩種情況:第一是(m2>m1),第二是當同月份時(d2>d1)
3.所以把這兩個情況減掉即可

程式碼:
#include <stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        int d1,m1,y1,d2,m2,y2,date=0;
        scanf("%d/%d/%d",&d1,&m1,&y1);
        scanf("%d/%d/%d",&d2,&m2,&y2);
        date=(y1-y2)-(m1<m2)-(m1==m2&&d2>d1);
        printf("Case #%d: ",i);
        if(date<0)
            printf("Invalid birth date\n");
        else if(date>130)
            printf("Check birth date\n");
        else
            printf("%d\n",date);
    }
    return 0;
}

2020年1月29日 星期三

a263: 日期差幾天

題目連結

(算日期的題目都好麻煩....把我的解法分享,希望能幫到其他同樣抓狂的人)

解法:
首先,我是參考這份講義中提供的想法(章節3.3.2),把從西元元年到所求日期的總天數分別加起來後,相減即是答案。

判斷閏年照定義做。

計算天數的詳細流程:
1.先算出從元年到(y-1)年的天數。全當作平年計算是因為相減後會相消,不影響。
(真的不影響嗎?其實我不是很清楚,但反正有對)
2.再來找出有幾個閏年。因為閏年比平年天數多1,所以求出有n個閏年則總天數多n。
3.再計算第y年的天數。先算m個月的天數,如果超過二月且當年是閏年則多加1。然後加上d。

其他就很簡單了。


程式碼:AC (4ms, 64KB)
#include <stdio.h>
int isleap(int year)
{
    if((year%400==0)||((year%100!=0)&&(year%4==0)))
        return 1;
    return 0;
}
int countdaysum(int y,int m,int d)
{
    int dayofmonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int sum=(y-1)*365;
    sum+=(y-1)/4;
    sum-=(y-1)/100;
    sum+=(y-1)/400;
    for(int i=1;i<m;i++)
        sum+=dayofmonth[i];
    if(m>2&&isleap(y))
        sum++;
    sum+=d;
    return sum;
}
int main()
{
    int y1,m1,d1,y2,m2,d2;
    while(scanf("%d %d %d %d %d %d",&y1,&m1,&d1,&y2,&m2,&d2)!=EOF)
    {
        int sum1,sum2;
        sum1=countdaysum(y1,m1,d1);
        sum2=countdaysum(y2,m2,d2);
        if(sum1>sum2)
            printf("%d\n",sum1-sum2);
        else
            printf("%d\n",sum2-sum1);
    }
    return 0;
}

2020年1月26日 星期日

How to start a movement

影片連結

dissect:分析、剖析
underestimated:被低估的
take guts:需要勇氣
nut:瘋子、堅果
emulate:模仿、效仿
get momentum:獲得動力
tipping point:引爆點、臨界點
recap:概括
over-glorified:過度崇拜

2020年1月25日 星期六

scanf相關問題 筆記

%s:“跳過0或多個 white space,由鍵盤緩衝區裡讀取連續不是 white space 的字元”(white space=空格、 '\t、 '\n' )
%c:只讀取緩衝區的一個字元
%d:跳過所有white space讀取數字

所以像在輸入數字時,多打空格仍然能被正確存進去。

scanf(" %c",&c);
%c前加空格,代表跳過緩衝區所有空格直到遇到字元。

scanf("%[^\n]", str);
代表直到碰到換行字元前的字元都存進去str,等同於gets(str),都是避免遇到空白讀不進去。

sscanf()用法:http://programmermagazine.github.io/201312/htm/article2.html

參考文章:
http://squall.cs.ntou.edu.tw/cprog/practices/scanfCommonTraps.pdf

2020年1月22日 星期三

Weird, or just different?

影片連結

downbeat:重拍、強拍
the beginning of the musical phrase:音樂段落的開端
count off:報數
There's a saying that:有句話說

2020年1月19日 星期日

Keep your goals to yourself

影片連結

for real:認真的、嚴肅的
becoming part of your identity:變成你的一部份
conventional wisdom:普遍的看法
gratification:感足感
mistakes the talking for the doing:把說到誤以為做到

2020年1月17日 星期五

Matt Cutts:用30天嘗試新事物

影片連結

stuck in a rut :一成不變
right amount of time :非常適合的時間(長度)
desk-dwelling computer nerd:常駐書桌的電腦書呆子
So here's my question to you:所以,我想問你
I guarantee you the next 30 days are going to pass whether you like it or not:我保證不論你喜不喜歡,接下來的30天照樣會過去(跟ted中文字幕翻的不一樣欸.....)
give it a shot:試試看

2020年1月14日 星期二

猜數字遊戲

很簡單的一個小遊戲,單純猜數字~


程式碼:
#include <stdio.h>
#include <time.h>

int main()
{
 srand(time(NULL));
 int n=rand()%1000;//0~999
 printf("Hi.Let's start the game!\n");
 while (1)
 {
  int temp;
  printf("Enter one number(0~999): ");
  scanf_s("%d", &temp);
  if (temp == n)
  {
   printf("Congratulation! You win!\n");
   break;
  }
  else if (temp > n)
   printf("This number is too big!\n");
  else
   printf("This number is too small!\n");
  printf("\n");
 }
 return 0;
}

Linked List--Delete

刪除第N個node
#include <stdio.h>
#include <stdlib.h>
struct Node
{
    int data;
    struct Node *next;
};
struct Node *head;
void Print()
{
    struct Node *temp=head;
    while(temp!=NULL)
    {
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}
void Delete(int n)
{
    struct Node *temp1=head;
    if(n==1)
    {
        head=temp1->next;
        free(temp1);
        return;
    }
    for(int i=0;i<n-2;i++)
        temp1=temp1->next; //temp1 points to (n-1)th Node
    struct Node *temp2=temp1->next; //nth node
    temp1->next=temp2->next; //(n+1)th Node
    free(temp2); //Delete temp2
}
void Insert(int n)
{
    struct Node *temp1=(struct Node*)malloc(sizeof(struct Node));
    temp1->data=n;
    temp1->next=NULL;
    struct Node *temp2=head;
    if(temp2==NULL)
    {
        head=temp1;
        return;
    }
    while(temp2->next!=NULL) // not while(temp2!=NULL)
    {
        temp2=temp2->next;
    }
    temp2->next=temp1;
}
int main()
{
    head=NULL; //empty list
    int n;
    char c,ch;
    Insert(1);
    Insert(2);
    Insert(3);
    Insert(4); // 1 2 3 4
    Print();
    Delete(2); // 1 3 4
    Print();
    return 0;
}

以值刪除node
#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *head;
void print()
{
    struct node *temp=head;
    while(temp!=NULL)
    {
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}
void insert(int data,int n)
{
    struct node *temp1=(struct node*)malloc(sizeof(struct node));
    temp1->data=data;
    temp1->next=NULL;
    if(n==1)
    {
        temp1->next=head;
        head=temp1;
        return ;
    }
    struct node *temp2=head;
    if(temp2==NULL&&n>1)
    {
        printf("error!\n");
        return ;
    }
    for(int i=0;i<n-2;i++)
    {
        temp2=temp2->next;
        if(temp2==NULL)
        {
            printf("error!\n");
            return ;
        }
    }
        
    temp1->next=temp2->next;
    temp2->next=temp1;
}
void delete(int n)
{
    int i=0;
    struct node *temp1=head;
    struct node *temp2=head;
    while(temp1!=NULL)
    {
        if(temp1->data==n) //points to n
        {
            if(i==0)
            {
                head=temp1->next;
                free(temp1);
                return ;
            }
            temp2->next=temp1->next;
            free(temp1);
            return ;
        }
        temp2=temp1;//temp2 is temp1-1
        temp1=temp1->next;
        i++;
    }
    printf("no this number!\n");
}
int main()
{
    head=NULL;
    int n,p;
    while(1)
    {
        printf("enter number to insert in list: ");
        scanf("%d",&n);
        if(n<0)
            break;
        printf("enter insert position: ");
        scanf("%d",&p);
        insert(n,p);
        print();
    }
    printf("delete one number: ");
    scanf("%d",&p);
    delete(p);
    print();
    return 0;
}

用scanf或getchar()讀取 y/n值 所遇問題

之前無聊想寫一個猜數字的遊戲,結果在問到要不要繼續時,每次輸入y,接下來都會印出兩次結果。

譬如說下面的code:
#include <stdio.h>

int main()
{
 char c;
 while (1)
 {
  printf("do you what to continue? ");
  scanf_s("%c", &c);
  if (c == 'n')
   break;
 }
 return 0;
}

就會出現:
輸入一個y,結果印了兩次"do you want to continue?"

後來查了一下網路,好像是因為scanf用%c讀值的時候,一次只接一個字元,但使用者是輸入了y或n被存到c之後,又再輸入了\n。第一個輸入的y/n的確被拿去判斷了沒錯,所以才會有前一個"do you want to continue? ",但後一個換行符號則被存到緩衝區,等下一次再拿出來判斷,造成多輸出了一次。

所以如果在scanf後多加一行,變成:
#include <stdio.h>

int main()
{
 char c;
 while (1)
 {
  printf("do you what to continue? ");
  scanf_s("%c", &c);
  getchar();
  if (c == 'n')
   break;
 }
 return 0;
}

也就是用getchar()把\n吃掉,就沒有問題了。



這個問題我一開始不知道怎麼下關鍵字,只好先跳過,是之後看到課本上有一行:
while(getchar()!='\n');
我才慢慢找對方向。
相關關鍵字大概是:clean input buffer, while loop error, getchar() in while loop 等等?

參考文章:
How to clear input buffer in C?
http://c-faq.com/stdio/scanfc.html

也可以參考http://squall.cs.ntou.edu.tw/cprog/practices/scanfCommonTraps.pdf