C语言--赎金信

赎金信

给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true ;否则返回 false。

假设两个字符串均只含有小写字母

  canConstruct("a", "b") -> false
  canConstruct("aa", "ab") -> false
  canConstruct("aa", "aab") -> true
#include<stdio.h>
#include<string.h>
int canConstruct(char* ransomNote, char* magazine) {
 int n = strlen(ransomNote);
 int m = strlen(magazine);
 int i, j;
 int k = 0;
 for (i = 0; i < n; i++) {
  for (j = 0; j < m; j++) {
   if (ransomNote[i] == magazine[j]) {
    magazine[j] = '1';
    k++;
    break;
    }
  }
 }
 if (k == n) {
  return 1;
 }
 else {
  return 0;
 }
}
int main() {
 char ransomNote[] = "abc";
 char magazine[] = "abhjjgg";
 int a=canConstruct(ransomNote, magazine);
if (a == 1) {
  printf("true");
 }
 else {
  printf("false");
 }
 return 0;
}
发布了40 篇原创文章 · 获赞 0 · 访问量 567

猜你喜欢

转载自blog.csdn.net/loreal8/article/details/104066324
今日推荐