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

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

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-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值