点击这里更换您喜欢的皮肤wtboj 首页
请点击这里登入noios   首页 入门 c++讲义 入门教程视频 其他页面 入门视频 站务 公告 | 题库 记录 竞测 测试 闯关 作业 排名 团队 讨论 | 换肤 | 登入 注册  
News >>   新增功能:各团队管理员可以发布本团队作业了 ()

讨论 Discussion
 
希尔排序
#include <iostream>
using namespace std;

// 希尔排序函数
void shellSort(int arr[], int n) {
  // 初始增量gap为数组长度的一半,每次循环后减半
  for (int gap = n / 2; gap > 0; gap /= 2) {
    // 对每个子序列进行插入排序
    for (int i = gap; i < n; i++) {
      int temp = arr[i];  // 待插入的元素
      int j;
      
      // 在子序列中进行插入排序
      for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
        arr[j] = arr[j - gap];  // 元素后移
      }
      
      arr[j] = temp;  // 插入正确位置
    }
    
    // 输出每一趟排序结果(可选)
    cout << "gap = " << gap << " 时的排序结果: ";
    for (int k = 0; k < n; k++) {
      cout << arr[k] << " ";
    }
    cout << endl;
  }
}

// 打印数组函数
void printArray(int arr[], int n) {
  for (int i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  cout << endl;
}

int main() {
  // 示例数组
  int arr[] = {49, 38, 65, 97, 76, 13, 27, 49, 55, 4};
  int n = sizeof(arr) / sizeof(arr[0]);
  
  cout << "原始数组: ";
  printArray(arr, n);
  cout << endl;
  
  cout << "希尔排序过程:" << endl;
  shellSort(arr, n);
  
  cout << "\n排序后数组: ";
  printArray(arr, n);
  
  return 0;
}

( 2026/5/27 17:39:57 )

#include <bits/stdc++.h>
using namespace std;
struct Student {
int id;
int math;
int chinese;
int english;
int total;
};
bool cmp(Student a, Student b) {
if (a.total != b.total) {
return a.total > b.total;
} else {
return a.id < b.id;
}
}
int main() {
freopen("p1000.in","r",stdin);
freopen("p1000.out","w",stdout);
int n;
cin >> n;
Student stu[60];
for (int i = 0; i < n; i++) {
cin >> stu[i].id >> stu[i].math >> stu[i].chinese >> stu[i].english;
stu[i].total = stu[i].math + stu[i].chinese + stu[i].english;
}
sort(stu, stu + n, cmp);
for (int i = 0; i < n; i++) {
cout << stu[i].id << " " << stu[i].math << " " << stu[i].chinese << " " << stu[i].english << " " << stu[i].total << endl;
}
fclose(stdin);
fclose(stdout);
return 0;
}
( )
#include<bits/stdc++.h>
using namespace std;
int main(){
freopen("p1649.in","r",stdin);
freopen("p1649.out","w",stdout);
int n,k,i=0;
cin>>n;
if(n%7==0){
cout<<n;
return 0;
}else{
k=n;
while(k>0){
i=i+k%10;
k=k/10;
}
cout<<i%7;
return 0;
}
fclose(stdin);
fclose(stdout);
return 0;
}
( )
#include<bits/stdc++.h>
using namespace std;
int main() {
freopen("p1730.in","r",stdin);
freopen("p1730.out","w",stdout);
int a[101][101]= {0},i,j,n;
cin>>n;
for(i=1; i<=n; i++) {
for(j=1; j<=n; j++) {
a[i][j]=i;
a[j][i]=i;
}
}
for(i=n; i>=1; i--) {
for(j=1; j<=n; j++)cout<<setw(3)<<a[j][i];
cout<<endl;
}
fclose(stdin);
fclose(stdout);
return 0;
}
( )
发布讨论主题 回复讨论主题
 Copyright wtboj © 2005-2006. www.wutuobang.date Powered by wtboj 关于 联系 帮助
 wtboj Information ---- Total Users : 1383 | Online Users / Processes : 0 / 155 | Processed Time : 32 ms | Server Time : 2026/6/15 1:43:32