#include <stdio.h>int main() { int a, b, temp; printf("请输入两个整数:\n"); scanf("%d %d", &a, &b); b=a; a = b; printf("交换后的值为:%d %d\n", a, b); return 0;}
时间: 2024-01-26 22:03:19 浏览: 36
这段代码是实现两个整数的交换,但是其中的交换部分有问题。正确的代码应该是:
```
#include <stdio.h>
int main() {
int a, b, temp;
printf("请输入两个整数:\n");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("交换后的值为:%d %d\n", a, b);
return 0;
}
```
这样才能正确地交换两个变量的值。
相关问题
用c语言补全以下代码用于将十进制整数num转化成二进制字符串输出“ #include <stdio.h> #include <stdio.h> void main( void ) { int num; char StringNum [20]; int i, j. Num[20]; puts("请输入十进制整数num:"); scanf ("8d", &num); for (i=0; num!=0; ++i) { Num [il= num/=2;”
以下是补全后的代码:
```
#include <stdio.h>
int main(void)
{
int num;
char str[20];
int i, j;
printf("请输入十进制整数num:");
scanf("%d", &num);
if (num == 0)
{
str[0] = '0';
str[1] = '\0';
}
else
{
for (i = 0; num > 0; i++)
{
str[i] = num % 2 + '0';
num = num / 2;
}
str[i] = '\0';
for (j = 0; j < i / 2; j++)
{
char temp = str[j];
str[j] = str[i - 1 - j];
str[i - 1 - j] = temp;
}
}
printf("二进制字符串为:%s\n", str);
return 0;
}
```
运行结果:
输入125:
```
请输入十进制整数num:125
二进制字符串为:1111101
```
输入256:
```
请输入十进制整数num:256
二进制字符串为:100000000
```
#include <stdio.h> #include <Windows.h> #include <math.h> int main() {int n; printf("输入整数个数为:"); scanf("%d",n); paixu(n); system("pause"); } int paixu(int n) { int i,j; int a[n]; for(i=0;i<n;i++) {scanf("%d",&a[i]);} for (i=0;i<n-1;i++) for (j=0;j<n-1-i;j++) if (a[j]>a[j+1]) { int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } printf("排序后:"); for(i=0;i<n;i++) {printf("%d ",a[i]);} }有什么错误并改正
在主函数中,输入整数个数时应该使用取地址符"&",改为:
```
scanf("%d", &n);
```
另外,在主函数中调用paixu函数时应该加上返回值类型,改为:
```
int paixu(int n);
```
完整的代码如下:
```c
#include <stdio.h>
#include <Windows.h>
#include <math.h>
int paixu(int n);
int main() {
int n;
printf("输入整数个数为:");
scanf("%d", &n);
paixu(n);
system("pause");
}
int paixu(int n) {
int i,j;
int a[n];
for(i=0; i<n; i++) {
scanf("%d",&a[i]);
}
for (i=0; i<n-1; i++) {
for (j=0; j<n-1-i; j++) {
if (a[j]>a[j+1]) {
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("排序后:");
for(i=0; i<n; i++) {
printf("%d ",a[i]);
}
return 0;
}
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)