当前位置:首页 » 编程软件 » 编程题晨练

编程题晨练

发布时间: 2023-04-01 22:09:12

❶ 有几个java编程的题各位好心人有时间的能帮忙写下吗

没那么多时间,帮着写个第1题吧

//编写求一个整数数组A[10,15,12,9,7]中最小元素min和元素之和sum的

int[]a={10,15,15,9,7};

//最小元素

intmin=0;

//数组和

intsum=0;


for(inti=0;i<a.length;i++){

sum+=a[i];

if(i==0){

min=a[i];

}else{

if(a[i]<min){

min=a[i];

}

}

}


System.out.println("当前数组中最小的元素值是:"+min);

System.out.println("当前数组和是:"+sum);

❷ 一 个简单的编程题目

#include <iostream.h>
int A[15]={14,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
/*A[1..14];为了下标从1开始,我用15个空间存14个数,第一个空间存数组长度
程序运行不结束是正确的因为14个数的全排列要1278945280个输出
*/
void Write(int a[])
{
// output the the elements of an array;
//input : an array
for(int i = 1;i<=14;i++)
cout<<a[i]<<'\t';
cout<<endl;
sum++;
}
void HeapPermute(int n)
{
/*实现生成全排列的Heap算法
输入全局数组A[1..n]
输出 A的全排列*/
if (n==1)
Write(A);
else
for(int i = 1;i<=n;i++)
{
HeapPermute(n-1);
if (n%2 != 0)
Swap(A[1],A[n]);
else
Swap(A[i],A[n]);
}
}

int main()
{
HeapPermute(14);
//cout<<fun(6);
//cout<<sum<<endl;
//cout<<SearchMin(A,1,5)<<endl;

return 0;
}

❸ 初学编程,大家帮忙看下这道c语言题怎么做万分感谢

先给你第一题的,网络知道的这个编辑器真的不适合粘贴代码

#include<stdio.h>

#include<string.h>

#define MAX_ARRAY_SIZE 1024

#define MAX_MAP_SIZE 10


/* 输入数组,连续输入,如:aedabcdaeas */

int inputArray(char *buff) {

int len = 0;

/* 使用fgets来防止缓冲区溢出 */

if (NULL == fgets(buff, MAX_ARRAY_SIZE, stdin)) {

return 0;

}

len = strlen(buff);

/* fgets 返回的数据可能是换行符结尾的,也可能不是,对换行符结尾的进行处理 */

if (buff[len - 1] == ' ') {

buff[len - 1] = '';

len -= 1;

}

return len;

}

int processArray(int len, char *chars, char *map) {

/* 保存反向映射便于查找 */

int tmap[128];

int maplen = 0;

int i = 0;

char *p = chars;

memset(tmap, -1, sizeof(int) * 128);

for (i = 0; i < len; i++) {


if (*p > 'z' || *p < 'a') {

return -*p;

}

if (tmap[*p] == -1) {

if (maplen >= MAX_MAP_SIZE) {

return -1;

}

tmap[*p] = maplen;

map[maplen] = *p;

maplen += 1;

}

*p = '0' + tmap[*p];

p++;

}

return maplen;

}

int main() {

/* 用于输入的字符数组 */

char buff[MAX_ARRAY_SIZE];

/* 用于保存转换规则的数组 */

char map[MAX_MAP_SIZE];

/* 保存字符数组长度 */

int len = 0;

int maplen = 0;

int i = 0;


len = inputArray(buff);


if (len <= 0) {

puts("Cancelled");

} else if (len < 10) {

puts("Not enough 10 chars");

} else {


maplen = processArray(len, buff, map);

if (maplen >= 0) {

puts("转换结果:");

for (i = 0; i < len; i++) {

printf("%c ", buff[i]);

}

puts("");

puts("映射规则:");

for (i = 0; i < maplen; i++) {

printf("%c -> %d ", map[i], i);

}

puts("");

} else if (maplen == -1) {

puts("Different Chars count is OverLimit of 10");

} else if (maplen <= -2) {

printf("Unexpected char %c ", -maplen);

}

}

return 0;

}

执行结果:

❹ 两道编程算法题(两图一道),题目如下,可以给出算法思路或者源代码,源代码最好是C语言的

就会个第一题(因为第一题上已经给出了大致思路)

思路:用map容器(它的内部数据结构是一颗红黑树,查找和插入数据速度非常快)
map<int,st>a;//key(int):设置为1~n的数;value(st):设置为key的前驱和后继;

这样一来就可以像链表快速插入数据,又可以像数组随机访问元素(key,就相当于数组的下标)

下面是代码和运行截图;

看代码前建议先了解一下map容器的具体用法;

#include<iostream>

#include<map>

#include<vector>

using namespace std;

struct st{//两个成员变量用来储存前驱和后继

int left;//0

int right;//1

st()

{

left=0;

right=0;

}

};

void input(map<int,st> &a)//输出

{

st t;

int s=0;

map<int,st>::iterator it;//迭代器(指针)

for(it=a.begin();it!=a.end();it++)//循环迭代

{

t=it->second;

if(t.left==0)//左边等于0,说明该数是第一个数

{

s=it->first;//记录key

break;

}

}

t=a[s];

cout<<s<<" ";

while(t.right!=0)//循环找当前数的右边的数(右边的为0,就说明该数是最后一个数)

{

cout<<t.right<<" ";

t=a[t.right];

}

}

int main()

{

st t,t_i,t_x,t_k,s;

map<int,st>a;

map<int,st>::iterator it;

int n,x,p,x_l,x_r;

cin>>n;

for(int i=1;i<=n;i++)//map容器赋初值(i,t)

//i:(key)下标;t:(value)结构体变量

{

a.insert(make_pair(i,t));

}

for(int i=2;i<=n;i++)

{

cin>>x>>p;

if(p==0)//x的左边插入i

{

t=a[x];

if(t.left==0)//x的左边没有数

{

t_x.left=i;

t_i.right=x;

a[x]=t_x;

a[i]=t_i;

}

else//x的左边有数

{

int x_left;

t_x=a[x];

x_left=t_x.left;

t_k=a[x_left];

t_i.right=x;

t_i.left=t_x.left;

t_k.right=i;

t_x.left=i;

a[x]=t_x;

a[i]=t_i;

a[x_left]=t_k;

}

}

else//x的右边插入i

{

t=a[x];

if(t.right==0)//x的右边没有数

{

t_x.right=i;

t_i.left=x;

a[x]=t_x;

a[i]=t_i;

}

else//x的左边有数

{

int x_right;

t_x=a[x];

x_right=t_x.right;

t_k=a[x_right];

t_i.left=x;

t_i.right=t_x.right;

t_k.left=i;

t_x.right=i;

a[x]=t_x;

a[i]=t_i;

a[x_right]=t_k;

}

}

}

for(it=a.begin();it!=a.end();it++)//循环迭代打印各个数之间的关系

{

cout<<it->first<<" ";

cout<<"左边:";

cout<<it->second.left<<" ";

cout<<"右边:";

cout<<it->second.right<<endl;

}

input(a);//打印序列

return 0;

}

/*

4

1 0

2 1

1 0

2 3 4 1

*/

热点内容
php日记本 发布:2024-05-02 17:28:22 浏览:850
msc拒绝访问 发布:2024-05-02 17:19:09 浏览:122
php函数漏洞 发布:2024-05-02 17:15:26 浏览:963
linux访问localhost 发布:2024-05-02 17:04:11 浏览:880
剑三自动任务脚本 发布:2024-05-02 16:59:42 浏览:526
哪里有java视频教程 发布:2024-05-02 16:59:31 浏览:346
零食盒子密码多少 发布:2024-05-02 16:52:24 浏览:354
win10怎么访问局域网 发布:2024-05-02 16:51:37 浏览:471
功能点估算法是 发布:2024-05-02 16:24:38 浏览:166
b站非法访问 发布:2024-05-02 16:09:59 浏览:456