读书人

今宵12点前求大神给答案吖!吖

发布时间: 2012-12-16 12:02:32 作者: rapoo

今晚12点前求大神给答案吖!急急急吖
第一题
Description

输入一个字符串,统计此字符串中字母、数字、空格和其它字符的个数。(不准使用strlen方法)。
Input

包含多组数据,每组数据包含一行,为要统计的字符串,输入以EOF结束。
Output

对每组数据,输出4个整数,分别对应输入字符串中字母、数字、空格和其它字符的个数,以空格分隔。
第二题
Description

编写函数sort,实现对整数数组排序的功能,具体排序方法可以使用冒泡或选择法,由用户自定。
Input

包含多组数据,每组数据包含两部分,输入以EOF结束:

1、第一行包含两个整数N和M,N为数组元素个数,M为排序方法(1为冒泡法,0为选择法);

2、从第二行起为N个要排序的整数(每行十个),以空格分隔。
Output

对每组数据,输出两行:

1、第一行为排序前数组元素的值;

2、第二行为排序后数组元素的值。
第三题
Description

编写程序,判断两个单词是否为变位词,即单词长度相同,使用字母相同,只是出现位置不同的一对词。例如:smartest 和 sttesmar。
Input

包含多组数据,每组数据包含一行,包含两个单词。输入以EOF结束。
Output

对于每组数据,输出一行:

1、如果两个单词是变位词,输出yes;

2、否则输出no。

[最优解释]
http://bbs.csdn.net/topics/390276082第一题
[其他解释]
第一题


#include <stdio.h>
int main()
{
char s;
int let=0;
int dig=0;
int oth=0;
int spa=0;
while((s=getchar())!=EOF)
{
if(s == '\n')
{
printf("%d %d %d %d\n",let,dig,spa,oth);
let=0;
dig=0;
oth=0;
spa=0;
}
else
{
if((s >= 'a' && s <= 'z')
[其他解释]
睡吧!
#include <stdio.h>  
#include <string.h>


int Is_Match(const char *strOne,const char *strTwo)
{
int lenOfOne = strlen(strOne);
int lenOfTwo = strlen(strTwo);




int hash[26] = {0};
int i,j,index;

if (lenOfOne != lenOfTwo)
return 0;

for (i = 0; i < strlen(strOne); i++)
{
index = strOne[i] - 'A';


hash[index]++;
}

for (j = 0; j < strlen(strTwo); j++)
{
index = strTwo[j] - 'A';



if (hash[index] != 0)
hash[index]--;
else
return 0;
}
return 1;
}

int main()
{
char strOne[20];
char strTwo[20];

while(scanf("%s%s",strOne,strTwo)!=EOF)
{
if (Is_Match(strOne, strTwo))
printf("match");
else
printf("not match");
}
return 0;
}


[其他解释]
引用:
执迷不悟
C/C++ code1234567891011121314151617181920212223242526272829303132333435363738394041#include <stdio.h>int main(){ char s; int let=0; int dig=0; int oth=0; int spa=0; ……
第一题过了O(∩_∩)O~现在只剩下2,3题吖
[其他解释]
http://bbs.csdn.net/topics/390275628第二题
[其他解释]
第三题
//copyright@ 2011 yansha   
//July、updated,2011.04.24。
#include <iostream>
#include <string>
using namespace std;

bool Is_Match(const char *strOne,const char *strTwo)
{
int lenOfOne = strlen(strOne);
int lenOfTwo = strlen(strTwo);

// 如果长度不相等则返回false
if (lenOfOne != lenOfTwo)
return false;

// 开辟一个辅助数组并清零
int hash[26] = {0};

// 扫描字符串
for (int i = 0; i < strlen(strOne); i++)
{
// 将字符转换成对应辅助数组中的索引
int index = strOne[i] - 'A';

// 辅助数组中该索引对应元素加1,表示该字符的个数
hash[index]++;
}

// 扫描字符串
for (int j = 0; j < strlen(strTwo); j++)
{


int index = strTwo[j] - 'A';

// 如果辅助数组中该索引对应元素不为0则减1,否则返回false
if (hash[index] != 0)
hash[index]--;
else
return false;
}
return true;
}

int main()
{
string strOne = "ABBA";
string strTwo = "BBAA";

bool flag = Is_Match(strOne.c_str(), strTwo.c_str());

// 如果为true则匹配,否则不匹配
if (flag == true)
cout << "Match" << endl;
else
cout << "No Match" << endl;
return 0;
}


[其他解释]
一楼好人吖CSDN真心给力~
[其他解释]
第三题貌似不是C额。。。求C~第二题???找不到具体是哪一楼的代码
[其他解释]
第三题链接的博客是个好地方 就是算法思想的问题
[其他解释]
( s >= 'A' && s <= 'Z'))
{
let++;
}
else if(s==' ')
{
spa++;
}
else if(s>='0' && s<='9')
{
dig++;
}
else
{
oth++;
}
}

}
return 0;
}

第二题

#include <stdio.h>
void sort(int *,int);
main()
{
int i;
int a[10]={16,5,8,12,1,17,3,6,10,2};
int length=sizeof(a)/sizeof(int);
printf("排序前数组的值\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n排序后数组的值\n");
sort(a,length);
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
}
void sort(int *a,int length)
{
int i,j,count;
int num;
for(count=length;count>0;count--)
{
i=0;
for(j=1;j<count;j++)
{
if(a[i]>a[j])
{
num=a[i];
a[i]=a[j];
a[j]=num;
i=j;
}
else
{
i=j;
}
}
}
}

[其他解释]
吖~现在急需代码的说第二题的那个连接的代码都不满足题设要求吖
[其他解释]
第二题是冒泡排序的,自己照题目要求修改下
[其他解释]
#include <stdio.h>   


#include <string.h>


bool Is_Match(const char *strOne,const char *strTwo)
{
int lenOfOne = strlen(strOne);
int lenOfTwo = strlen(strTwo);


if (lenOfOne != lenOfTwo)
return false;


int hash[26] = {0};


for (int i = 0; i < strlen(strOne); i++)
{
// 将字符转换成对应辅助数组中的索引
int index = strOne[i] - 'A';

// 辅助数组中该索引对应元素加1,表示该字符的个数
hash[index]++;
}

// 扫描字符串
for (int j = 0; j < strlen(strTwo); j++)
{
int index = strTwo[j] - 'A';

// 如果辅助数组中该索引对应元素不为0则减1,否则返回false
if (hash[index] != 0)
hash[index]--;
else
return false;
}
return true;
}

int main()
{
char *strOne = "ABBA";
char *strTwo = "BBAA";

bool flag = Is_Match(strOne, strTwo);

// 如果为true则匹配,否则不匹配
if (flag == true)
printf("match");
else
printf("not match");
getchar();
return 0;
}

改成C格式就行了
[其他解释]
出来混迟早要还的 现在省事了以后还得写
[其他解释]
没学过C++吖。。。不知道怎么改吖
[其他解释]
引用:
出来混迟早要还的 现在省事了以后还得写
说的很对吖O(∩_∩)O~但是现在的确很急。。。
[其他解释]
#include <stdio.h>  
#include <string.h>




int Is_Match(const char *strOne,const char *strTwo)
{
int lenOfOne = strlen(strOne);
int lenOfTwo = strlen(strTwo);




int hash[26] = {0};
int i,j,index;

if (lenOfOne != lenOfTwo)
return 0;

for (i = 0; i < strlen(strOne); i++)
{
index = strOne[i] - 'A';


hash[index]++;
}

for (j = 0; j < strlen(strTwo); j++)
{
index = strTwo[j] - 'A';

if (hash[index] != 0)
hash[index]--;
else
return 0;
}
return 1;
}

int main()
{
char *strOne = "ABBA";
char *strTwo = "BBAA";

if (Is_Match(strOne, strTwo))
printf("match");
else
printf("not match");
getchar();
return 0;
}


[其他解释]
执迷不悟
#include <stdio.h>
int main()
{
char s;
int let=0;
int dig=0;
int oth=0;
int spa=0;
while((s=getchar())!=EOF)
{
if(s == '\n')
{
printf("%d %d %d %d\n",let,dig,spa,++oth); 注意这里++oth
let=0;
dig=0;
oth=0;
spa=0;
}


else
{
if((s >= 'a' && s <= 'z')
[其他解释]
好了就结贴给分哦
[其他解释]
( s >= 'A' && s <= 'Z'))
{
let++;
}
else if(s==' ')
{
spa++;
}
else if(s>='0' && s<='9')
{
dig++;
}
else
{
oth++;
}
}

}
return 0;
}


[其他解释]
\
('A' <= (c) && (c) <= 'Z'))

int main(void)
{
char line[MAX_LINE], *p;
int spaces, digits, letters, other;

while (gets(line))
{
spaces = 0;
digits = 0;
letters = 0;
other = 0;
p = line;
while (*p)
{
if (IS_SPACE(*p)) spaces++;
else if (IS_DIGIT(*p)) digits++;
else if (IS_LETTER(*p)) letters++;
else other++;
p++;
}

printf("spaces=%d, digits=%d, letters=%d, other=%d\n",
spaces, digits, letters, other);
printf("\n");
}

return 0;
}
[其他解释]
第一题:
#include <stdio.h>


#define MAX_LINE 1024


#define IS_SPACE(c)((c) == ' ')
#define IS_DIGIT(c)('0' <= (c) && (c) <= '9')
#define IS_LETTER(c)(('a' <= (c) && (c) <= 'z')
[其他解释]
时间在流逝。。。但是还是不满足题目要求交不了作业。。。
[其他解释]
睡觉了 你们继续奋战吧!
[其他解释]
都大学生了 还好像小学生似得
[其他解释]
没人写第二三题
[其他解释]
orz谢谢
------其他解决方案--------------------


大神不打扰你了。。。晚安~

读书人网 >C++

热点推荐