void sort1(){g[11];//jia for(int i=0;i<a-1;i++){ for (int j = 0; j < a - i - 1; j++){ if(g[j].prices>g[j+1].prices){ g[11]=g[j]; g[j]=g[j+1]; g[j+1]=g[11]; //TODO } } //TODO } } void sort2(){g[12];//pian for(int i=0;i<a-1;i++){ for (int j = 0; j < a - i - 1; j++){ if(g[j].num>g[j+1].num){ g[12]=g[j]; g[j]=g[j+1]; g[j+1]=g[12]; //TODO } } //TODO } }
时间: 2024-03-31 12:37:30 浏览: 16
根据你提供的代码,sort1() 函数和 sort2() 函数的实现中都有一个问题,就是定义了一个没有意义的数组 g[11] 或 g[12],但并没有实际使用。这会导致程序在执行到这一行时出现不必要的浪费。你可以将这两行定义数组的代码删除,程序应该就能正常运行了。
至于你提到输入1直接退出的问题,代码中并没有体现出这种情况。可能是其他部分的代码中出现了问题,需要你提供更多的信息来让我能够帮助你解决问题。
相关问题
#include<stdio.h> int A[101][101]; void guiling() { for(int i=1;i<=10;i++) { for(int j=1;j<=10;j++) { A[i][j]=0; } } } void shuchu(int a) { for(int i=1;i<=a;i++) for(int j=1;j<=a;j++) { printf("%4d ",A[i][j]); if(j==a) { printf("\n"); } } printf("\n"); guiling(); } void jishu(int a) { A[1][(a+1)/2]=1; int a1=1,b1=(a+1)/2; int a2,b2; for(int k=2;k<=a*a;k++) { a2=a1,b2=b1; if(--a1==0) a1=a; if(++b1>a) b1=1; if(A[a1][b1]!=0) { a1=a2+1; if(a1>a) { a1=1; } b1=b2; } A[a1][b1]=k; } shuchu(a); } //void Kjie(int a,int b) //{ // // // //} // //void jie(int a,int b) //{ // // // //} void panduan() { for(int i=3;i<11;i++) { for(int j=3;j<11;j++) { if((i-1)%2==0&&i==j) { jishu(j); } // if(i%2==0&&i==j&&i!=6&&i!=10) // { // Kjie(i,j); // } // // if((i==6||i==10)&&i==j) // { // jie(i,j); // } } } } int main() { guiling(); panduan(); return 0; }为什么这段代码输出不正确
这段代码的输出不正确是因为 `guiling()` 函数在 `shuchu()` 函数中被调用,导致每次在输出完矩阵后,矩阵都被重新初始化了,因此无法正确输出幻方。
解决方法是将 `guiling()` 函数的调用放在 `panduan()` 函数中,在生成幻方之前初始化矩阵,如下所示:
```
void panduan() {
guiling(); // 初始化矩阵
for(int i=3;i<11;i++) {
for(int j=3;j<11;j++) {
if((i-1)%2==0&&i==j) {
jishu(j);
}
// 其他生成幻方的方法
}
}
}
```
这样就能够正确地输出幻方了。
#include<stdio.h> const int N = 1e5; int a[N]; void swap(int &x,int &y){ int t = x; x = y; y = t; }//交换数字函数 //这是降序 int main() { int n; scanf("%d",&n); for(int i = 0; i < n; i ++)scanf("%d",&a[i]); //冒泡排序 for(int i = 1; i <= n; i ++){ for(int j = 0; j < n; j ++){ if(a[j]<a[j + 1])swap(a[j],a[j + 1]); } } for(int i = 0; i < n; i ++)printf("%d",a[i]); return 0; }这段代码有错误吗
这段代码中存在错误,具体来说有两处错误:
1. 冒泡排序的内层循环中,判断条件应该是 j < n - i - 1,而不是 j < n。因为每一轮冒泡排序最后一个数字已经排好序了,不需要再次比较。
2. 在输出排序后的数字时,应该在每个数字后面加上空格或换行符,否则数字会连在一起输出。
下面是修改后的代码:
```
#include <stdio.h>
const int N = 1e5;
int a[N];
void swap(int &x, int &y) {
int t = x; x = y; y = t;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
// 冒泡排序
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n - i; j++) {
if (a[j] < a[j + 1]) {
swap(a[j], a[j + 1]);
}
}
}
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
```
经过修改后,这段代码可以正确地对输入的一组数字进行降序排序并输出。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)