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
介紹 UVa & I/O
Coding 突破
• 介紹 UVa & ICPC & NCPC
• I/O 資料型態• I/O 範例• 程式技巧
什麼是 ACM?
• ACM=Association of Computing Machinery
•美國計算機協會
What are ICPC & NCPC
ICPC= International Collegiate
Programming Contest
= 國際大專體設計競賽NCPC= 全國大專軟體設計競賽
你可能得到的結果• 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) 使用禁止函式
資料型態
I/O 型態
C Input Output
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”
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);
output 是 char 或是 int :
printf(“%c\n”,output);
printf(“%d\n”,output);
string 是 char 的陣列printf(“%s\n”,string); // string is char[]
實驗看看吧 !!
第一次 AC.
10055
一起完成吧 !!
I/O 範例
讀到檔案結束while(scanf(“%d”,&input)!=EOF){
//code
}
條件式結束while(scanf(“%d”,&input) && input != 0 ){
//code
}
已知有幾筆測資while(datas--){ //code
}
陣列大小要注意 ~~
int number[10];
for(int counter=1 ;counter<=10 ;counter++){
scanf(“%d”,&number[counter]);
}
當 counter=10 就會發生錯誤
字元是以 ASCII 做運算 char code; // 有一個密碼
code = ‘a’; // 他是 a
code+= 3; // 加 3 後
printf(“%c”,code);
將會印出 d
The second AC
458
苦工題 switch 是好朋友 switch 版 :
switch(){
case ‘ ‘:
…..
case ‘ ‘:
}
if 版 :
if(){
}else{
if(){
}else{
……..
}
}
記得要加 break 喔 !!
善用 strcmp
strcmp( string1,string2); // 比較兩字串 strcmp( string , “command\0”);
// 看 string 是不是 command
warning1 : 要有 #include<string.h>
strcmp 的回傳值
strcmp 的回傳值為• 前者的字典排序小於後者時 回傳負數• 當前者的字典排序等於後者時 回傳零• 當前者的字典排序大於後者時 回傳正數
Tips about programming
• 好習慣• 測資很奸詐• 註解• CMD
好習慣
變數要有意義
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 ;
// 久了你自已都不知這是什麼
記得要排版有排版while( ){
for(){
sum+=1;
}
counter++;
}
沒排版while(){
for()
{ sum+=1;
} counter++
}
// 是不是比較難讀了 ?
變數不能沒有值錯誤範例 :
// 找最大的數 int input;
int biggest;
while(scanf(“%d”,&input)!=EOF){
if(biggest<input) biggest = input ;
}
// 程式第一次執行會出問題
變數周期要注意錯誤例 :
for(int sum=0, i =0 ; i <10 ; i ++){
sum += i ;
}
printf(“%d\n”,sum);
測資很奸詐 !!
註解有利找出程式的錯誤
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>
實做一次看看吧
參考資料• 2009 年講議• <C How to pragram>
練習一下吧 !!!
• 基本題• 272
• 10071
• 10209 // float 不夠準喔 ~~
• 進階題• 591
• 10222
補充 C++
C++ IO
• tip : #include<iostream>
• using namespace std;
• Input
• cin
ex:cin>>num1>>num2;
= scanf(“%d %d”,&num1,&num2);
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
cout
• cout
• Ex:int n1=0,n2=1;
• cout<<“n1=”<<n1<<“n2=”<<n2<<endl;
• 可以不需設定 type!
cout tips
• cout.setprecision()
• #include <iomanip>• double n=3.14159;
cout << setprecision(2) <<fixed << n<<endl;
// 輸出 n 到小數點下 2 位
cout tips
• cout.width();
• Ex: printf(“%-10d\n”,a);
printf(“%10d\n”,a); = cout.width(10);
cout<<left<<a<<endl;
cout<<right<<a<<endl;
cout tips
• cout.fill();
• Ex: printf(“%010d\n”,a);
= cout.width(10);
cout.fill(‘0’);
cout<<right<<a<<b<<endl;