42
介介 UVa & I/O

介紹 UVa & I/O

  • Upload
    urban

  • View
    98

  • Download
    0

Embed Size (px)

DESCRIPTION

介紹 UVa & I/O. 介紹 U Va & ICPC & NCPC I/O 資料型態 I/O 範例 程式技巧. Coding 突破. 什麼是 ACM?. ACM=Association of Computing Machinery 美國計算機協會. 註冊 UVa. 跟著做 http://uva.onlinejudge.org/. What are ICPC & NCPC. ICPC= International Collegiate Programming Contest - PowerPoint PPT Presentation

Citation preview

Page 1: 介紹    UVa & I/O

介紹 UVa & I/O

Page 2: 介紹    UVa & I/O

Coding 突破

• 介紹 UVa & ICPC & NCPC

• I/O 資料型態• I/O 範例• 程式技巧

Page 3: 介紹    UVa & I/O

什麼是 ACM?

• ACM=Association of Computing Machinery

•美國計算機協會

Page 4: 介紹    UVa & I/O

註冊 UVa

• 跟著做• http://uva.onlinejudge.org/

Page 5: 介紹    UVa & I/O

What are ICPC & NCPC

ICPC= International Collegiate

Programming Contest

= 國際大專體設計競賽NCPC= 全國大專軟體設計競賽

Page 6: 介紹    UVa & I/O

你可能得到的結果• Judge Situation

• Accept (AC) 接受 (通過 )• Presentation Error (P.E.) 格式錯誤• Wrong Answer (WA) 錯誤答案• Time Limit Exceeded (TLE) 超過時間• Memory Limit Exceeded (MLE) 超過記憶體上限• Output Limit Exceeded (OLE) 超過輸出上限• Compile Error (CE) 編譯錯誤• Submission Error (SE) 提交錯誤• Runtime Error (RE) 執行錯誤• Restricted Function (RF) 使用禁止函式

Page 7: 介紹    UVa & I/O

資料型態

Page 8: 介紹    UVa & I/O

I/O 型態

Page 9: 介紹    UVa & I/O

C Input Output

Page 10: 介紹    UVa & I/O

scanf(“%c”,&input);

資料型態 讀取方式

char “%c”

char “ %c”

int “%d”

long “%ld”

long long “%lld”

unsigned int “%u”

unsigned long “%lu”

float “%f”

double “%lf”

string “%s”

Page 11: 介紹    UVa & I/O

output 是 int:

printf(“%d\n”,output);

printf(“%3d\n”,output);

printf(“%-3d\n”,output);

printf(“%03d\n”,output);

output 是 int:

printf(“%f\n”,output);

printf(“%2.3f\n”,output);

Page 12: 介紹    UVa & I/O

output 是 char 或是 int :

printf(“%c\n”,output);

printf(“%d\n”,output);

string 是 char 的陣列printf(“%s\n”,string); // string is char[]

Page 13: 介紹    UVa & I/O

實驗看看吧 !!

Page 14: 介紹    UVa & I/O

第一次 AC.

10055

一起完成吧 !!

Page 15: 介紹    UVa & I/O

I/O 範例

Page 16: 介紹    UVa & I/O

讀到檔案結束while(scanf(“%d”,&input)!=EOF){

//code

}

條件式結束while(scanf(“%d”,&input) && input != 0 ){

           //code

}

已知有幾筆測資while(datas--){ //code

}

Page 17: 介紹    UVa & I/O

陣列大小要注意 ~~

int number[10];

for(int counter=1 ;counter<=10 ;counter++){

scanf(“%d”,&number[counter]);

}

當 counter=10 就會發生錯誤

Page 18: 介紹    UVa & I/O
Page 19: 介紹    UVa & I/O

字元是以 ASCII 做運算 char code; // 有一個密碼

code = ‘a’; // 他是 a

code+= 3; // 加 3 後

printf(“%c”,code);

將會印出 d

Page 20: 介紹    UVa & I/O

The second AC

458

Page 21: 介紹    UVa & I/O

苦工題 switch 是好朋友 switch 版 :

switch(){

case ‘ ‘:

…..

case ‘ ‘:

}

if 版 :

if(){

}else{

if(){

}else{

……..

}

}

記得要加 break 喔 !!

Page 22: 介紹    UVa & I/O

善用 strcmp

strcmp( string1,string2); // 比較兩字串 strcmp( string , “command\0”);

// 看 string 是不是 command

warning1 : 要有 #include<string.h>

Page 23: 介紹    UVa & I/O

strcmp 的回傳值

strcmp 的回傳值為• 前者的字典排序小於後者時 回傳負數• 當前者的字典排序等於後者時 回傳零• 當前者的字典排序大於後者時 回傳正數

Page 24: 介紹    UVa & I/O

Tips about programming

• 好習慣• 測資很奸詐• 註解• CMD

Page 25: 介紹    UVa & I/O

好習慣

Page 26: 介紹    UVa & I/O

變數要有意義

int number1;

int number2;

int sum;

scanf(“%d%d”,&number1,&number2);

sum = num1 + num2;

int sasdf;

int sdfdf;

int eafesgf;

scanf(“%d%d”,&sasdf,& sdfdf);

eafesgf = sasdf + sdfdf ;

// 久了你自已都不知這是什麼

Page 27: 介紹    UVa & I/O

記得要排版有排版while( ){

for(){

sum+=1;

}

counter++;

}

沒排版while(){

for()

{ sum+=1;

} counter++

}

// 是不是比較難讀了 ?

Page 28: 介紹    UVa & I/O

變數不能沒有值錯誤範例 :

// 找最大的數 int input;

int biggest;

while(scanf(“%d”,&input)!=EOF){

if(biggest<input) biggest = input ;

}

// 程式第一次執行會出問題

Page 29: 介紹    UVa & I/O

變數周期要注意錯誤例 :

for(int sum=0, i =0 ; i <10 ; i ++){

sum += i ;

}

printf(“%d\n”,sum);

Page 30: 介紹    UVa & I/O

測資很奸詐 !!

Page 31: 介紹    UVa & I/O

註解有利找出程式的錯誤

Page 32: 介紹    UVa & I/O

CMD 測資處理• windows的 cmd檔案 IO的功能 •指令:• test<input.txt <enter>從檔案 input讀入 test.exe執行

• test>output.txt <enter>把你在 test.exe所 print的結果存到output.txt中

混用法test<input.txt>output.txt <enter>

Page 33: 介紹    UVa & I/O

實做一次看看吧

Page 34: 介紹    UVa & I/O

參考資料• 2009 年講議• <C How to pragram>

Page 35: 介紹    UVa & I/O

練習一下吧 !!!

• 基本題• 272

• 10071

• 10209 // float 不夠準喔 ~~

• 進階題• 591

• 10222

Page 36: 介紹    UVa & I/O

補充 C++

Page 37: 介紹    UVa & I/O

C++ IO

• tip : #include<iostream>

• using namespace std;

• Input

• cin

ex:cin>>num1>>num2;

= scanf(“%d %d”,&num1,&num2);

Page 38: 介紹    UVa & I/O

cin about string

• cin.get()• Ex: char test;• cin.get(test);

• cin.getline();• Ex: char test[10];• cin.getline(test,9,’\n’);• Ps: 最後的參數為斷句的 key char

Page 39: 介紹    UVa & I/O

cout

• cout

• Ex:int n1=0,n2=1;

• cout<<“n1=”<<n1<<“n2=”<<n2<<endl;

• 可以不需設定 type!

Page 40: 介紹    UVa & I/O

cout tips

• cout.setprecision()

• #include <iomanip>• double n=3.14159;

cout << setprecision(2) <<fixed << n<<endl;

// 輸出 n 到小數點下 2 位

Page 41: 介紹    UVa & I/O

cout tips

• cout.width();

• Ex: printf(“%-10d\n”,a);

printf(“%10d\n”,a); = cout.width(10);

cout<<left<<a<<endl;

cout<<right<<a<<endl;

Page 42: 介紹    UVa & I/O

cout tips

• cout.fill();

• Ex: printf(“%010d\n”,a);

= cout.width(10);

cout.fill(‘0’);

cout<<right<<a<<b<<endl;