用函数实现:从键盘上输入16个int数,去掉重复的然后在屏幕上输出
时间: 2024-05-25 15:18:04 浏览: 9
#include <iostream>
#include <algorithm>
using namespace std;
void remove_duplicates(int arr[], int n){
sort(arr, arr+n); //排序
int j = 0;
for(int i=0; i<n; i++){
if(arr[i] != arr[j]){
arr[++j] = arr[i];
}
}
for(int i=0; i<=j; i++){
cout << arr[i] << " ";
}
}
int main(){
int arr[16];
cout << "请输入16个整数:";
for(int i=0; i<16; i++){
cin >> arr[i];
}
remove_duplicates(arr, 16);
return 0;
}
相关问题
用c语言基础知识编写程序编写函数int sum(int x),求整数x的各位数字之和。 在main函数中测试该函数:从键盘输入一非负整数,然后调用sum函数计算各位数字之和并输出结果。
好的,这是一个关于C语言基础编程的问题,我可以回答。下面是该程序的示例代码:
```
#include <stdio.h>
int sum(int x)
{
int digit, total = 0;
while (x > 0)
{
digit = x % 10; // 取出x的最低位数字
total += digit; // 累加到total中
x /= 10; // x右移一位,去除最低位数字
}
return total;
}
int main(void)
{
int x, s;
printf("请输入一个非负整数:");
scanf("%d", &x);
s = sum(x);
printf("各位数字之和为:%d\n", s);
return 0;
}
```
这个程序中,sum函数接受一个整数x作为参数,返回x的各位数字之和。在main函数中,首先从键盘上读入一个非负整数x,然后调用sum函数计算x的各位数字之和并输出结果。
本关任务:从键盘输入一个字符串,将其按逆序输出。C语言
以下是一个简单的 C 语言程序,可以从键盘读取一个字符串,然后按逆序输出该字符串。
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("请输入一个字符串:");
fgets(str, 100, stdin);
str[strcspn(str, "\n")] = '\0'; // 去掉 fgets() 函数读入的换行符
printf("原字符串:%s\n", str);
int len = strlen(str);
for (int i = len - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");
return 0;
}
```
在上面的程序中,我们使用了 `fgets()` 函数从键盘读取一个字符串,并将其存储在 `str` 变量中。然后,我们使用 `strlen()` 函数计算字符串长度,然后从字符串末尾开始循环并逐个输出字符。最后,我们在末尾输出一个换行符,以便下一次输出可以在新的一行开始。
注意,在使用 `fgets()` 函数读取字符串后,字符串末尾会包含一个换行符,因此我们使用 `strcspn()` 函数将其替换为空字符 `\0`。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)