读书人

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

发布时间: 2013-01-05 15:20:40 作者: 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')
[解决办法]
( 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>


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;
}


[解决办法]
第二题:
#include <stdio.h>
#include <string.h>

void bubble_sort(int a[], int n)
{
int i, j, t;

for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (a[j] > a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
}

void select_sort(int array[], int nSize)
{
int nMinIndex;
int nIndex_1, nIndex_2;

for (nIndex_1 = 0; nIndex_1 < nSize - 1; nIndex_1++)
{
nMinIndex = nIndex_1;

for (nIndex_2 = nIndex_1 + 1 ; nIndex_2 < nSize;nIndex_2++)
{
if ( array[nMinIndex] > array[nIndex_2] )
{
nMinIndex = nIndex_2;
}
}

if (nMinIndex != nIndex_1)


{
int temp = array[nIndex_1];
array[nIndex_1] = array[nMinIndex];
array[nMinIndex] = temp ;
}

}
}


#define MAX 100


int main(void)
{
int m, n, i;
int arr[MAX]/* = {-1, 3, -12, 2, 3, 7}*/;

while (scanf("%d%d", &m, &n) != EOF)
{
getchar();

memset(arr, 0, sizeof(arr));
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
getchar();

if (m == 0)
select_sort(arr, n);
else if (m == 1)
bubble_sort(arr, n);

for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n\n");
}

getch();
return 0;
}



第3题我就不写了,那位兄弟给了

读书人网 >C++

热点推荐