2012年1月8日 星期日
ACM 10222 Decode The Mad Man
[心得] :0.004
1. ACM 10082 WERTYU差不多
2.注意大寫要轉換成小寫
3.\ 要打成\\ 上次打錯 XD!
#include <stdio.h>
#include <string.h>
int main()
{
int c;
char *pos;
char key[]="1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
while((c=getchar())!=EOF)
{
if(isupper(c))
c= tolower(c);
pos= strchr(key,c);
putchar((pos)?key[pos-key-2]:c);
}
system("pause");
//return 0;
}
1. ACM 10082 WERTYU差不多
2.注意大寫要轉換成小寫
3.\ 要打成\\ 上次打錯 XD!
#include <stdio.h>
#include <string.h>
int main()
{
int c;
char *pos;
char key[]="1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
while((c=getchar())!=EOF)
{
if(isupper(c))
c= tolower(c);
pos= strchr(key,c);
putchar((pos)?key[pos-key-2]:c);
}
system("pause");
//return 0;
}
ACM 10209 Is This Integration ?
[心得] :0.044
#include <stdio.h>
#include <math.h>
#define PI 2.0*acos(0.0)
#define line_area(r) (r) * (r) * (1 + PI * 1.0 / 3.0 - sqrt(3))
#define point_area(r) (r) * (r) * (-4 + PI * 1.0 / 3.0 + sqrt(3) * 2)
#define square_area(r) (r) * (r) * (4 - PI * 2.0 / 3.0 - sqrt(3))
int main()
{
double a;
while(scanf("%lf",&a)==1)
{
printf("%.3lf %.3lf %.3lf\n",line_area(a),point_area(a),square_area(a));
}
system("pause");
// return 0;
}
#include <stdio.h>
#include <math.h>
#define PI 2.0*acos(0.0)
#define line_area(r) (r) * (r) * (1 + PI * 1.0 / 3.0 - sqrt(3))
#define point_area(r) (r) * (r) * (-4 + PI * 1.0 / 3.0 + sqrt(3) * 2)
#define square_area(r) (r) * (r) * (4 - PI * 2.0 / 3.0 - sqrt(3))
int main()
{
double a;
while(scanf("%lf",&a)==1)
{
printf("%.3lf %.3lf %.3lf\n",line_area(a),point_area(a),square_area(a));
}
system("pause");
// return 0;
}
ACM 10041 Request for Proposal
[心得]: //0.008
1.scanf()如果遇到名字有空白,就會有問題,所以用gets()
2. 比較需要注意的是 scanf() 後面接 gets(),gets()會多讀一個'\n',所以第二個 gets(nothing)的
for 迴圈,counter 從0開始!
3.只要注意廠商名稱和價錢,提共的項目數量,對於項目名稱可以不用理會!
for(i=1;i<=n;i++)
gets(nothing);
4.ACM的格式如果沒搞清楚,就會WA,但是比較高興的是,現在自己想和寫出來的Code終於比較
少WA了! 可以很快就AC!
#include <stdio.h>
#include <ctype.h>
#include <string.h>
struct Corp
{
char name[90];
float money;
int supply;
};
struct Corp Answer,Temp;
int main()
{
int i,n,p,q=0;
char output[1001][90];
char nothing[90];
while(scanf("%d %d",&n,&p)==2)
{
if(n==0 || p==0)
break;
gets(nothing);
int counter=0;
Answer.supply=0;
for(i=1;i<=n;i++)
gets(nothing);
while(gets(Temp.name)!=NULL)
{
if(isalpha(Temp.name[0]))
{
counter++;
scanf("%f %d",&Temp.money,&Temp.supply);
/*printf("%s\n",&Temp.name);
printf("%f %d\n",Temp.money,Temp.supply);*/
for(i=0;i<=Temp.supply;i++)
gets(nothing);
if(Temp.supply > Answer.supply || (Temp.supply == Answer.supply && Temp.money < Answer.money))
{
strcpy(Answer.name,Temp.name);
Answer.supply = Temp.supply;
Answer.money = Temp.money;
}
}
if(counter == p)
break;
}
strcpy(output[q],Answer.name);
q++;
}
for(i=0;i<q;i++)
{
if(i!=0) printf("\n");
printf("RFP #%d\n%s\n",i+1,output[i]);
}
system("pause");
//return 0;
}
1.scanf()如果遇到名字有空白,就會有問題,所以用gets()
2. 比較需要注意的是 scanf() 後面接 gets(),gets()會多讀一個'\n',所以第二個 gets(nothing)的
for 迴圈,counter 從0開始!
3.只要注意廠商名稱和價錢,提共的項目數量,對於項目名稱可以不用理會!
for(i=1;i<=n;i++)
gets(nothing);
4.ACM的格式如果沒搞清楚,就會WA,但是比較高興的是,現在自己想和寫出來的Code終於比較
少WA了! 可以很快就AC!
#include <stdio.h>
#include <ctype.h>
#include <string.h>
struct Corp
{
char name[90];
float money;
int supply;
};
struct Corp Answer,Temp;
int main()
{
int i,n,p,q=0;
char output[1001][90];
char nothing[90];
while(scanf("%d %d",&n,&p)==2)
{
if(n==0 || p==0)
break;
gets(nothing);
int counter=0;
Answer.supply=0;
for(i=1;i<=n;i++)
gets(nothing);
while(gets(Temp.name)!=NULL)
{
if(isalpha(Temp.name[0]))
{
counter++;
scanf("%f %d",&Temp.money,&Temp.supply);
/*printf("%s\n",&Temp.name);
printf("%f %d\n",Temp.money,Temp.supply);*/
for(i=0;i<=Temp.supply;i++)
gets(nothing);
if(Temp.supply > Answer.supply || (Temp.supply == Answer.supply && Temp.money < Answer.money))
{
strcpy(Answer.name,Temp.name);
Answer.supply = Temp.supply;
Answer.money = Temp.money;
}
}
if(counter == p)
break;
}
strcpy(output[q],Answer.name);
q++;
}
for(i=0;i<q;i++)
{
if(i!=0) printf("\n");
printf("RFP #%d\n%s\n",i+1,output[i]);
}
system("pause");
//return 0;
}
2012年1月7日 星期六
C ctype.h
C 語言標準函數庫分類導覽 - 字元測試 ctype.h
以下為字元測試的函數
| 函數名稱 | 功能 | 函數原型 |
|---|---|---|
| isdigit | 測試字元是否為數字 | int isdigit(int); |
| isalpha | 測試字元是否為字母 | int isalpha(int); |
| isalnum | 測試字元是否為數字或字母 | int isalnum(int); |
| isxdigit | 測試字元是否為十六進位數字 | int isxdigit(int); |
| islower | 測試字元是否為小寫字母 | int islower(int); |
| isupper | 測試字元是否為大寫字母 | int isupper(int); |
| isascii | 測試字元是否為 ASCII | int isascii(int); |
| isblank | 測試是否為空白字元 | int isblank(int); |
| isspace | 測試字元是否為空格 | int isspace(int); |
| iscntrl | 測試是否為控制字元 | int iscntrl(int); |
| ispunct | 測試是否為空格、數字、字母以外的可列印字元 | int ispunct(int); |
| isprint | 測試是否為含括空格以內的可列印字元 | int isprint(int); |
| isgraph | 測試是否為空格以外的可列印字元 | int isgraph(int); |
以下為英文字母大、小寫轉換的函數
| 函數名稱 | 功能 | 函數原型 |
|---|---|---|
| tolower | 將大寫字母轉換為小寫 | int tolower(int); |
| toupper | 將小寫字母轉換為大寫 | int toupper(int); |
string.h 字串操作
網路上別人寫好的文章,寫的很詳細!很適合作備忘錄!
附上原文網址:http://pydoing.blogspot.com/2010/07/c-stdstring.html
C 語言標準函數庫分類導覽 - 字串處理 string.h
標頭檔 string.h 宣告許多字串處理相關的函數,包括拷貝、相接、搜尋、測試相等、計算長度等。以 str 起頭的函數作為處理字串之用,另有以 mem 起頭的函數,這些函數則可以進行記憶體區塊的操作。 size_t 作為 sizeof 運算子的回傳型態,實際上可能為 unsigned int 或 unsigned long 。
以下函數可以拷貝字串
| 函數名稱 | 功能 | 函數原型 |
|---|---|---|
| strcpy | 將字串 s2 拷貝到 s1 | char *strcpy(char *s1, const char *s2); |
| strncpy | 將字串 s2 最多 n 個字元拷貝到 s1 | char *strncpy(char *s1, const char *s2, size_t n); |
以下函數可以將字串相接
| 函數名稱 | 功能 | 函數原型 |
|---|---|---|
| strcat | 將字串 s2 接到 s1 的尾端 | char *strcat(char *s1, const char *s2); |
| strncat | 將字串 s2 最多 n 個字元接到 s1 的尾端 | char *strncat(char *s1, const char *s2, size_t); |
以下函數測試兩個字串是否相等
| 函數名稱 | 功能 | 函數原型 |
|---|---|---|
| strcmp | 比較 s1 與 s2 兩個字串是否相等 | int strcmp(const char *s1, const char *s2); |
| strncmp | 比較 s1 與 s2 兩個字串前 n 個字元是否相等 | int strncmp(const char *s1, const char *s2, size_t n); |
以下函數作為字串的搜尋處理之用
| 函數名稱 | 功能 | 函數原型 |
|---|---|---|
| strchr | 回傳在字串 s 中,字元 c 第一次出現位置的指標 | char *strchr(const char *s, int c); |
| strcspn | 計算經過幾個字元會在字串 s1 中遇到屬於 s2 中的字元 | size_t strcspn(const char *s1, const char *s2); |
| strspn | 計算經過幾個字元會在字串 s1 中遇到不屬於 s2 中的字元 | size_t strspn(const char *s1, const char *s2); |
| strpbrk | 回傳在字串 s2 中的任何字元在 s1 第一次出現位置的指標 | char *strpbrk(const char *s1, const char *s2); |
| strrchr | 回傳在字串 s 中,字元 c 最後一次出現位置的指標 | char *strrchr(const char *s, int c); |
| strstr | 回傳在字串 s2 在 s1 第一次出現位置的指標 | char *strstr(const char *s1, const char *s2); |
| strtok | 以字串 s2 的內容切割 s1 | char *strtok(char *s1, const char *s2); |
以下函數計算字串的長度
| 函數名稱 | 功能 | 函數原型 |
|---|---|---|
| strlen | 計算字串的長度 | size_t strlen(const char *s); |
以下函數為進行記憶體區塊操作之用
| 函數名稱 | 功能 | 函數原型 |
|---|---|---|
| memcpy | 從 s2 所指向的資料複製 n 個字元到 s1 | void *memcpy(void *s1, const void *s2, size_t n); |
| memmove | 從 s2 所指向的資料複製 n 個字元到 s1 | void *memmove(void *s1, const void *s2, size_t n); |
| memcmp | 比較 s1 與 s2 前 n 個字元的資料 | int memcmp(const void *s1, const void *s2, size_t n); |
| memchr | 找出字元 c 在 s 前 n 個字元第一次出現的位置 | void *memchr(const void *s, int c, size_t n); |
| memset | 將 s 中前 n 個字元全部設定為 c | void *memset(void *s, int c, size_t n); |
字串的宣告
一般只要是用 " 符號所包起來的字元就是字串,如下的宣告
char s[5]="abcd";
字串"abcd"除了四個字元所佔的空間外還包含一個'\0'字元在最後面當字串結束符號
所以總字元數是5
字串就是一連續字元的最末端以一個'\0'字符做為結束的連續字元
編譯器對字串的處理以及字串相關的函式,都是依照這個規格在處理字串,這也是字串與一般字元陣列不一樣的地方
你也可以這樣子來宣告char s[]="abcd";
雖沒有指明陣列索引值,但編譯器會自動幫你補上,所以陣列 s 將會是一個索引值為 5 的陣列
字串與char*
以下是一個最簡單的應用
char s[]="abcd";
char* p=s;
p 就相當於是一個指著 s 的指標
char* str="abcd";
指標str相當於是一個字串,編譯器會自動配置一塊空間擺放"abcd"這個字串,然後讓str指標指向
但他和char s[]是不一樣的地方,是你不能修改那塊空間裡的值,像這樣 *str='z';
但你能再讓他指向別的地方
字串的處理
C裡頭有一組現成的專用拿來處理字串的函式,需引入 string.h 這個標頭檔
以下簡單的介紹這些常用的函式
size_t strlen(const char* source)
取得字串長度
char* strcpy(char* destn,const char* source)
拷貝字串,將source字串拷貝至destn的空間,回傳值其實就是destn的位置
char* strncpy(char* destn,const char* source,size_t n)
拷貝字串,將source字串的前n個字元拷貝至destn的空間,回傳值為destn的位置
char* strcat(char* destn,const char* source)
拷貝字串,將source字串串接至destn字串後面,回傳值為destn的位置,請確定destn的空間足夠儲放串接後的字串
char* strncat(char* destn,const char* source,,size_t n)
串接兩字串,將source的前n個字串串接至destn字串後面,回傳值為destn的位置,請確定destn的空間足夠儲放串接後的字串
int strcmp(const char* str1,const char* str2)
比對字串,若完全相等則回傳0,若有差異,回傳其字元值差
int strncmp(const char* str1,const char* str2,size_t n)
比對字串只比對前n個字元,若完全相等則回傳0,若有差異,回傳其字元值差
int strcasecmp(const char* str1,const char* str2)
忽略大小寫比對字串,比對字串,若完全相等則回傳0,若有差異,回傳其字元值差
int strncasecmp(const char* str1,const char* str2,size_t n)
忽略大小寫比對字串只比對前n個字元,若完全相等則回傳0,若有差異,回傳其字元值差
char* strchr(const char* str,int ch)
找出字串中第一個指定字元的位置,若找不到傳回0
char* strrchr(const char* str,char ch)
找出str字串中,最後一個出現ch字元的字元,回傳該字元位址
char str[]="123123";
strrchr(str,'1');
回傳值將會是&str[3]
size_t strcspn(const char* str,const char* reject)
傳回str字串中第一個不含reject字串中字元的前字元數
char str[]="ABCD123456789";
strcspn(str,"4321");
將會得到4
char* strdup(const char* string)
複製字串,其字串空間是使用molloc()所配置,事後也能用free()釋放掉,失敗回傳NULL
char* strfry(char* string)
隨機重組字串內的字元
char* strpbrk(const char* str,const char* accept)
找出str字串中,有accept字串中字元的字元,回傳該字元的位址
char str[]="ABCD123456789";
strpbrk(str,"4321");
回傳值將會是&str[4]
char* strrchr(const char* str,char ch)
找出str字串中,最後一個出現ch字元的字元,回傳該字元位址
char str[]="123123";
strrchr(str,'1');
回傳值將會是&str[3]
size_t strspn(const char* str,const char* accept)
傳回str字串中前面含有accept字串中字元的字元數
char str[]="ABCD123456789";
strspn(str,"BA");
將會得到2
char* strstr(const char* str1,const char* str2)
搜尋字串,在str1的字串搜尋str2字串,傳回搜尋到的子字串位址
char* strtok(char* str,const char* delimit)
分解字串,str字串為要被分解的字串,delimit為要做為分解依據的字串
找到了要分解依據的字元便將其設為='\0',即回傳該字串
第一次使用strtok()引數str必須指定,往後可設為NULL,如此將持續一直分解下去,直到回傳值為NULL,便不能再分解下去
char str[]="abc def:ghi-xyz try"
char* p;
printf("%s=",strtok(str," :-"));
while(p=strtok(NULL," :-"))printf("%s=",p);
將印出 abc=def=ghi=xyz=try
以上所例函式從較常用者先列出string.h不只有專用處理字串的函式,也有專門用來處理記憶體的相關函式
它們的差別在於字串函式處理的對象是字串,也就是'\0'符號做為結尾的一串字元,所傳入的char*型別的參數都必須要是字串
而記憶體函式是不管內容是什麼字元,都一視同仁處理
string.h - memchr,memset,memcmp,memapy,memmove
memchr
Declaration: vo搜尋mem記憶體內容的前n個字元是否有ch字元,回傳值為該ch字元的位址,若無則回傳0id *memchr(const void *str, intc, size_tn);
#include <stdio.h>
#include <string.h>
int main()
{
char s[]="I LOVE C and C++.";
printf("%s\n",(memchr(s,'L',18)));
printf("%s\n",(strchr(s,'L')));
char bs[]="1111 1010 0101 1100 0011";
printf("%s\n",(memchr(bs,'11',40)));
printf("%s\n",(strchr(bs,'11')));
char nulls[]="'\0' 1111 1010 0101 1100 0011";
printf("%s\n",(memchr(nulls,'11',40)));
printf("%s\n",(strchr(nulls,'11')));
system("pause");
}
RESULT:
LOVE C and C++.
LOVE C and C++.
1111 1010 0101 1100 0011
1111 1010 0101 1100 0011
1111 1010 0101 1100 0011
(null) <=如果字串裡有null('\0') ,strchr一遇到 NULL就會停止 */
memchr與strchr 不同的地方是
strchr stops when it hits a null character but memchr does not;
this is why the former does not need a length parameter but the latter does.
memcmp
Declaration:int memcmp(const void *str1, const void *str2, size_tn);
記憶體比對,比對mem1和mem2所指的記憶體空間的前n個字元,回傳值為相異的字元差
#include <stdio.h>
#include <string.h>
int main()
{
char s[]="LOVE C and C++";
char t[]="LOVE Is A Clothes";
printf("%d\n",(memcmp(s,t,4)));
printf("%d\n",(memcmp(s,t,7)));
printf("%d\n",(memcmp(t,s,7)));
/*RESULT:
0
-1
1 */
system("pause");
}
memcpy
Declaration:void* memcpy(void* destn,const void* source,size_t n)
記憶體拷貝,從soutce中拷貝n個字元至destn,回傳值為destn位址
memccpy
void* memccpy(void* destn,const void* source,int ch,size_t n)
記憶體拷貝,拷貝從ch出現以後的n各字元,從soutce中拷貝n個字元至destn,,並回傳出現ch字元的下一個字元的位址 若未出現ch字元則回傳0
#include <stdio.h>
#include <string.h>
int main()
{
char s[]="123456789 I LOVE C and C++";
char t[35];
printf("memcpy result :%s\n",(memcpy(t,s,30)));
printf("memccpy result:%s\n",(memccpy(t,s,'I',30)));
/*RESULT:
memcpy result :123456789 I LOVE C and C++
memccpy result: LOVE C and C++
*/
system("pause");
memmove
Declaration:Copies n characters from str2 to str1. If str1 and str2 overlap the information is first completely read from str1 and then written to str2 so that the characters are copied correctly. Returns the argument str1.void *memmove(void *str1, const void *str2, size_tn);
記憶體拷貝,從soutce中拷貝n個字元至destn,但destn和source的區域若有重疊,也能進行拷貝
但執行效率比memcpy()略慢些
#include <stdio.h>
#include <string.h>
int main()
{
char s[40] = ".............out of work";
char t[20] = "future Job";
memmove(s, t, 10);
printf("%s\n", s);
/*RESULT:
future Job...out of work */
system("pause");
}
memset
Declaration:Copies the character c (anvoid *memset(void *str, intc, size_tn);
unsigned char) to the first n characters of the string pointed to by the argument str. The argument str is returned.可以參考 memset那篇
訂閱:
文章 (Atom)