有这样的一个数组, 其中的元素都是成对处出现的, 只有两个元素出现了一次. 要设法找出这两个元素.
首先, 我们将数组的收元素存在变量 tem 中,然后将 tem 中的元素依次与数组中的每一个元素进行对比. 如果相等则使变量 k++ .当对比完成之后如果k的值等于1,则打印tem中的值. 然后利用循环对数组中每一个元素进行此操作.
具体代码如下
#include<stdio.h>
#include<stdlib.h>
int main(){
int arr[] = { 1, 1, 2, 2, 4, 4, 6, 5, 5, 34, 88, 88 };
int len = sizeof(arr) / sizeof(arr[0]);
int tem = 0;
for (int i = 0; i < len; ++i){
tem = arr[i];
int k = 0;
for (int j = 0; j < len; ++j){
if (arr[j] == tem){
++k;
}
}
if (k == 1){
printf("%d\n", arr[i]);
}
}
system("pause");
return 0;
}
喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,
给20元,可以多少汽水。
编程实现。
#include<stdio.h>
#include<stdlib.h>
int Exchange(int x){
int ret = 0;
while (x>1)
{
ret = ret + x / 2;
x = x / 2 + x % 2;
}
return ret;
}
int main(){
int money = 20;
int ret = 20 + Exchange(money);
printf("%d \n", ret);
system("pause");
return 0;
}