您的位置:首页 > 其它

程序设计与算法 第七周测验

2016-09-19 15:00 429 查看
程序设计与算法 第七周测验

1:统计数字字符个数

#include <iostream>
#include <cstring>
using namespace std;
#define LEN 255
int main(void)
{
char s[LEN+1] = {'\0'};
int cnt = 0;
cin.getline(s, sizeof(s));
for (int i=0; s[i]!='\0'; i++)
if ('0' <= s[i] && s[i] <= '9')
cnt++;
cout << cnt << endl;
return 0;
}


2:找第一个只出现一次的字符

#include <iostream>
#include <cstring>
using namespace std;
#define ML 100000
#define AL 26
int main(void)
{
char s[ML+1] = {'\0'};
int cnt[AL] = {0};
cin.getline(s, sizeof(s));
for (int i=0; s[i]!='\0'; i++)
cnt[s[i]-'a']++;
for (int i=0; s[i]!='\0'; i++)
{
if(cnt[s[i]-'a']==1)
{
cout << s[i] << endl;
return 0;
}
}
cout << "no" << endl;
return 0;
}


3:石头剪子布

#include <iostream>
#include <cstring>
using namespace std;
#define L 10
#define ROCK 2
#define SCISSORS 1
#define PAPER 0
int s2i(char s[])
{
if (strcmp(s,"Rock")==0) return ROCK;
else if (strcmp(s,"Scissors")==0) return SCISSORS;
else return PAPER;
}
int cmp(char s1[], char s2[])
{
int i1 = s2i(s1);
int i2 = s2i(s2);
if (i1==i2)
return 0;
else if (i1<i2) {
if (i1==PAPER && i2==ROCK)
return 1;
else
return -1;
} else {
if (i1==ROCK && i2==PAPER)
return -1;
else
return 1;
}
}
int main(void)
{
int N;
char s1[L], s2[L];
cin >> N;
while(N--)
{
cin >> s1 >> s2;
int n = cmp(s1, s2);
if (n<0)
cout << "Player2" << endl;
else if (n==0)
cout << "Tie" << endl;
else
cout << "Player1" << endl;
}
return 0;
}


4:最长最短单词

#include <iostream>
#include <cstring>
using namespace std;
int main(void)
{
char word[101], min_word[101]={'\0'}, max_word[101]={'\0'};
char c, *w = word;
while(cin.get(c))
{
if(c==' ' || c==',' || c=='\n') {
*w = '\0';
int len = w - word;
if (len>0) {
if(len<strlen(min_word)||strlen(min_word)==0)
strcpy(min_word,word);
if (len>strlen(max_word))
strcpy(max_word,word);
}
if (c=='\n')
break;
w = word;
} else {
*w++ = c;
}
}
cout << max_word << endl;
cout << min_word << endl;
return 0;
}


5:密码翻译

#include <iostream>
#include <cstring>
using namespace std;
#define ML 80
int main(void)
{
char s[ML+1] = {'\0'};
cin.getline(s, sizeof(s));
for (int i=0; s[i]!='\0'; i++)
{
if (s[i]=='z')
cout << 'a';
else if (s[i]=='Z')
cout << 'A';
else if ( ('a'<=s[i] && s[i]<='y') || ('A'<=s[i] && s[i]<='Y') )
cout << (char)(s[i]+1);
else
cout << s[i];
}
cout << endl;
return 0;
}


-eof-
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐