在数组中找出和为目标值两个整数用c语言实现
时间: 2023-05-27 20:01:11 浏览: 92
求整数的位数及各位和c语言实现
#include <stdio.h>
int main() {
int target, n;
printf("Enter the target sum: ");
scanf("%d", &target);
printf("Enter the number of integers in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the integers in the array:\n");
for(int i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
if(arr[i]+arr[j]==target) {
printf("%d + %d = %d\n", arr[i], arr[j], target);
return 0;
}
}
}
printf("There are no two integers in the array that add up to %d\n", target);
return 0;
}
阅读全文