當前位置:首頁 » 編程軟體 » 編程題晨練

編程題晨練

發布時間: 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

*/

熱點內容
雲伺服器2m寬是多少 發布:2024-04-19 11:56:36 瀏覽:727
android層布局 發布:2024-04-19 11:52:13 瀏覽:770
1500元組裝伺服器電腦 發布:2024-04-19 11:47:25 瀏覽:468
qq改密碼怎麼改手機 發布:2024-04-19 11:39:17 瀏覽:968
電腦上如何看wifi密碼 發布:2024-04-19 11:34:14 瀏覽:415
java性能測試腳本 發布:2024-04-19 11:25:24 瀏覽:980
存儲成本與性能 發布:2024-04-19 11:16:18 瀏覽:168
linux根文件系統製作 發布:2024-04-19 11:16:12 瀏覽:746
光遇夏日活動什麼時候安卓上線 發布:2024-04-19 11:08:15 瀏覽:854
Java開羅 發布:2024-04-19 10:50:55 瀏覽:959