C++高手乱如,几道程序设计题,望高手尝试解答

weihuan 2005-08-11 12:06:51
.
题目1:
有一个集合,集合中有n个元素,每个集合元素都是正整数,存放在一维数组A中,每个数组元素存放一个集合

元素.对给定的整数total(假定集合的每个元素值均小于total),求出所有满足下列条件的子集:子集中各

元素之和等于total.
注:
用试探法找出全部答案,设total=10,n=6,a[]={8,4,1,2,5,3}.


题目2:
从正文文件 text.txt 中读入一篇英文短文,统计该短文中不同单词和它的出现次数,并按词典次序将单词

及他的出现次数输出到文本文件 word.txt 中.
注:
用有序二叉树存储单词及其出现的次数,一边读入一边建立,然后遍历该二叉树,将遍历经过的二叉树上的

结点内容输出.


题目3:
在3x3的方格中添入数字1~N(N>=10)内的某9个互不相同的整数,使所有的相邻两个方格内的两个整数之和

为质数.试求出满足这个要求的所有添法. N=12.
注:
用试探法,从序号0的方格开始,寻找一个合理的整数,填入后,试探下一个方格,如果当前方格找不到合理的

整数,退回上一个方格,调整填入的整数,知道找到所有的添法为止.


题目4:
从一个指定的文本文件中,查询一个指定单词的出现次数,要求:
a.文本文件由界面元素指定;
b.查询的单词由界面元素指定;
c.可以指定查询是否分大小写;
d.把查询结果显示在界面元素中,并指出其在文本文件中的位置.

题目5:
完成一个函数,将给定的字符串例如:"Happy New Year!"中的字符按照ASCII码的升序排列,输出至另一个

字符串中.(" !HNYaaeeppryw")
注:
1.平常工作用到VC的同事,请用CSting来做.
2.答案请附.c和.h或.cpp和.h文件.
3.请注意运用Coding Standard.
...全文
931 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
KingI 2005-08-23
  • 打赏
  • 举报
回复
这是第一题的:
#include <iostream>
using namespace std;
#include <vector>

vector<int> res;

void show(int *a, int total, int n)
{
for (int i=0; i < n; i++) {
int temp=0, sum=0;
if (a[i] == total) {
cout<<a[i]<<endl;
continue;
}
for (int j=i; j < n; j++) {
temp=(sum += a[j]);
res.push_back(a[j]);
for (int m=j+1; m < n; m++) {
if ((sum += a[m]) == total) {
for (int k=0; k < res.size(); k++)
cout<<res[k]<<',';
cout<<a[m]<<endl;
} else {
sum=temp;
}
}
sum=temp;
}
res.clear();
}
}


void main()
{
int a[]={8,4,1,2,5,3};
int total=10;

show(a, total, 6);
}


hhcjb 2005-08-23
  • 打赏
  • 举报
回复
第一题好像是个背包问题,怎么用个试探法再做啊?
葫芦鬼 2005-08-23
  • 打赏
  • 举报
回复
新手不懂.留名关注
HaoyuTan 2005-08-16
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int charcmp(const void* c1, const void* c2) {
return *((char*)c1) - *((char*)c2);
}

int main()
{
char str[] = "alksjglkasjlfkjaslk;gao30";
qsort(str, strlen(str), sizeof(str[0]), charcmp);
printf(str);

system("pause");

return 0;
}
HaoyuTan 2005-08-16
  • 打赏
  • 举报
回复
第五题,用标准C库中的qsort函数,或者用STL做
lyclowlevel 2005-08-16
  • 打赏
  • 举报
回复
qhfu(崩溃) 的算法也是递归的。我觉得他的比较好。楼上的递归好像跟我非递归的一样复杂啊!^_^
happyxx2050 2005-08-15
  • 打赏
  • 举报
回复
各位师兄师姐,我是一个新手,
因为以前当兵,所以没有机会
学习计算机技术,现在想好好
学习一下,我以前是计算机(中专)
毕业,学过c语言,但是不知道
目前的计算机行业的发展趋势,
我想请教各位指点一下该往哪
方面发展.
syscoder 2005-08-15
  • 打赏
  • 举报
回复
第一道,觉得还是递归比较容易理解吧,vc6.0下编译通过

#include <iostream>
#include <string>
#include <vector>
using namespace std;

//set:存储集合的类
class set{
private:
int _size;
vector< vector<int> * > _set;
public:
set(){
_size = 0;
}
size(){
return _size;
}
vector<int> * get_set( int index ){
if( index < 0 || index >= _set.size() )
return NULL;
return _set[ index ];
}
void push_set( vector<int> * new_set ){
_set.push_back( new_set );
_size++;
}
//向指定的一个子集压入一个元素
void push_ele( int new_ele, int index ){
if( index < 0 || index >= _set.size() )
return;
_set[index]->push_back( new_ele );
}
void display(){
for( int i = 0; i < _size; i++ ){
for( int j = 0; j < _set[i]->size() ; j++ ){
cout << (*_set[i])[j];
cout << ", ";
}
cout << endl;
}
}
};

set * fun( const int a[], int size, int total ){
int i = 0;
set * rsl = new set();
if( size <=0 ){
rsl = NULL;
return rsl;
}
else if( size == 1 ){
if( total == a[0] ){
rsl->push_set( new vector<int> );
rsl->push_ele( a[0], 0 );
}
return rsl;
}
else{
if( total == a[0] ){
rsl->push_set( new vector<int> );
rsl->push_ele( a[0], 0 );
}
set * temp = fun( a+1, size-1, total );
for( i = 0; i < temp->size(); i++ )
rsl->push_set( temp->get_set( i ) );
if( total > a[0] ){
temp = fun( a+1, size-1, total-a[0] );
for( i = 0; i < temp->size(); i++ ){
temp->push_ele( a[0], i );
rsl->push_set( temp->get_set( i ) );
}
}
return rsl;
}
}

void main()
{
int i;
int data[] = {2,3,7,9,6,4,8,1,11,10,13};
set * result = fun( data, 11 , 15 );
result->display();
for( i = 0; i < result->size(); i++ )
delete result->get_set( i );
}
qhfu 2005-08-14
  • 打赏
  • 举报
回复
第一道
#include <iostream>
using namespace std;
void DFS(int a[],int p,int n,int m,int cur,int rct [])
{
if (cur == 0 )
{
for(int i = 0;i<m;i++)
{
cout<<rct[i]<<" ";
}
cout<<endl;
}
for (int i = p;i<n;i++)
{
if (cur-a[i] >=0)
{
rct[m] = a[i];
DFS(a,i+1,n,m+1,cur-a[i],rct);
}
}
}
int main()
{
int a[]={8,4,1,2,5,3};
int c[6];
DFS(a,0,6,0,10,c);
system("pause");
}
lyclowlevel 2005-08-14
  • 打赏
  • 举报
回复
以下是第一题的可执行代码(觉得差,不要骂我^_^):
其中MyStack是我自定义的一个模板栈类。
#include<vector>
#include<iostream>
using namespace std;

#include"MyStack.h"
void Process(int *,int,int);

class Pair
{
public:
int data;
int i;
Pair operator=(const Pair &a)
{
this->data=a.data;
this->i=a.i;
return *this;
}
};
void main()
{
int array[]={8,4,1,2,5,3};
int total=10;
int n=6;
cout<<"求得的所有子集为:"<<endl;
Process(array,6,10);
}
void Process(int *array,int length,int total)
{//length is the length of array.
if(!array)
return;
Pair d;
MyStack<Pair>s,t;
int temp=0;
for(int i=0;i<length;i++)
{
temp+=array[i];
if(temp>total)
{
temp-=array[i];
if(i==length-1)
{
s.Pop(d);
temp-=d.data;
i=d.i;
}
continue;
}
if(temp<total)
{
if(i==length-1)
{
temp=0;
while(s.Pop(d))
i=d.i;
}
else
{
d.data=array[i];
d.i=i;
s.Push(d);
}
continue;
}
if(temp==total)
{
d.data=array[i];
d.i=i;
t.Push(d);
temp-=array[i];
if(temp==0)
{
cout<<array[i]<<endl;
while(t.Pop(d))
i=d.i;
}
else
{
s.Pop(d);
temp-=d.data;
i=d.i;
t.Push(d);
while(s.Pop(d))
t.Push(d);
while(t.Pop(d))
{
if(d.i<i)
s.Push(d);
cout<<d.data<<' ';
}
cout<<endl;
}
}
}
}
na1ve 2005-08-12
  • 打赏
  • 举报
回复
好多题啊!
超叔csdn 2005-08-12
  • 打赏
  • 举报
回复
这绝对不是面试题,我猜的话多半是期末考试题!
klime 2005-08-12
  • 打赏
  • 举报
回复
改正!。。。。。可能就2题。。。可悲。。。:(
klime 2005-08-12
  • 打赏
  • 举报
回复
哈!我才学了20天C,大概能做个3题左右,楼主好好努力啊。。。
lyclowlevel 2005-08-12
  • 打赏
  • 举报
回复
To:LoveYouJustOneDay(哈哈)

瞒强的!花了两三分钟,算是看明白了。
巧用哈希,但空间开销挺大的。
sankt 2005-08-12
  • 打赏
  • 举报
回复
up
黄昏清晨 2005-08-12
  • 打赏
  • 举报
回复
嗯,应该是2、3个小时就差不多了
qingfenglz 2005-08-12
  • 打赏
  • 举报
回复
第二个题目我以前帮同学做过类似的题目
LoveYouJustOneDay 2005-08-12
  • 打赏
  • 举报
回复
最后一道其实没那么麻烦的

#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
int hash[256]={0};
char input[1000], *p=input;
cin.getline(input, 999);
while(*p)
hash[*p++]++;
for(int i=0; i<256; i++)
while(hash[i]--)
cout<<(char)i;
return 0;
}
lyclowlevel 2005-08-12
  • 打赏
  • 举报
回复
前面四题比较麻烦,你如果《数据结构》学得可以的话,应该就行了。
最后一题比较简单,我就把我的代码给你吧!
#include <iostream>
#include <algorithm>
using namespace std;
char*Orderlize(const char *str,int size)
{
if(!str)
return 0;//str is empty.
int * TempArray=new int[size];
int i;
for(i=0;i<size;str++,i++)
TempArray[i]=static_cast<int>(*str);
for(i=0;i<size-1;i++)//冒泡排序
{
for(int j=0;j<size-1-i;j++)
{
if(TempArray[j]>=TempArray[j+1])
{
int temp=TempArray[j];
TempArray[j]=TempArray[j+1];
TempArray[j+1]=temp;
}
}
}
char *result=new char[size+1];
for(i=0;i<size;i++)
result[i]=static_cast<char>(TempArray[i]);
result[size]='\0';
return result;
}
void main()
{
char *test="Happy New Year!";
char *result=Orderlize(test,15);
if(result)
cout<<result<<endl;
}

可以运行的。
加载更多回复(8)

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧