1、用cin语句输入x,y的值,分别利用if和swich两种分支语句完成;
//if结构 #include<iostream> #include<cmath> using namespace std; int main() { double x; cout << "输入具体x的值: "; cin >> x; if (x < 0) cout << x; else if (x >= 0 && x < 10) cout << 2 * x - 1; else cout << 3 * x - 1; return 0; } //switch结构 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> int main() { int x, t; int y; scanf("%d", &x); t = (x < 0) + (x < 10) ; //括号中的关系表达式有几个为真(1),确定了t的值,实则反映的是x的范围 switch (t) { case 2: //(x<0) 、 (x<10) 为真有2,自然x>=0,且x<10 y =x; break; case 1: //(x<0),(x<10) 为真有1,自然…… y = 2 * x -1 ; break; case 0://只有(x<0) , (x<10) 全0了.也可写作default: y = 3*x-1; } printf("%d\n", y); return 0; }
#include <iostream> using namespace std; int main(int argc, char* argv[]) { int c, wushi, ershi, shi, wu, yi; cin >> c; wushi = c > 50 ? c / 50 : 0; cout << wushi; ershi = c - 50; ershi = (ershi > 0) ? ershi / 20 : 0; cout << " " << ershi; shi = c - wushi * 50 - ershi * 20; shi = (shi > 0) ? shi / 10 : 0; cout << " " << shi; wu = c - 50 - ershi * 20 - shi * 10; wu = (wu > 0) ? wu / 5 : 0; cout << " " << wu; yi = c - 50 - ershi * 20 - shi * 10 - wu * 5; yi = (yi > 0) ? yi : 0; cout << " " << yi << endl; return 0; }
#include<iostream> using namespace std; int main() { //对冒泡排序进行升序排序 int arr[10] = { 4, 1, 2, 5, 3, 7, 6, 9, 8, 10 }; int b = sizeof(arr) / sizeof(arr[0]);//定义数组中元素个数; cout << "原数组为:" << endl; for (int i = 0; i < b; i++) { cout << arr[i] << " "; } //冒泡排序 for (int j = 1; j < b; j++) { for (int i = 0; i < b - j; i++) { if (arr[i] > arr[i + 1])//交换数组元素 { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } } cout << endl << "排序后的数组为:" << endl; for (int i = 0; i < b; i++) { cout << arr[i] << " "; } cout << endl; system("pause"); return 0; }
#include <iostream> using namespace std; int BinarySearch(int myarray[], int n, int key) { int low = 0, high = n - 1; int mid = 0; while (low <= high) { mid = (low + high) / 2; if (myarray[mid] == key) return mid; if (myarray[mid] < key) low = mid + 1; else high = mid - 1; } return -1; } int main() { int myarray[] = { 1,3,4,6,8,7 }; int index = BinarySearch(myarray, 6, 8); if (index != -1) { cout << "Find it!" << endl; cout << "index is:" << index << endl; } else cout << "Not Found!" << endl; return 0; }
#include<stdio.h> void main() { int i, j, colum = 0, row = 0, max; int a[3][4] = { {1,2,3,4},{9,8,7,6},{-10,10,-5,2} }; max = a[0][0]; for (i = 0; i <= 2; i++) { for (j = 0; j <= 3; j++) { if (a[i][j] > max) { max = a[i][j]; colum = j; row = i; } } } printf("max=%d,row=%d,colum=%d", max, colum, row); printf("\n"); }
#include<iostream> using namespace std; double FA(int n) { double m = 1; for (int i = 1; i <= n; i++) m *= i; return m; } int main() { double ans = 0, n; cin >> n; for (int i = 1; i <= n; i++) ans += FA(i); cout << ans; return 0; }
#include<iostream> using namespace std; int imax(int array[], int count) { int max = array[0]; for (int i = 1;i < count ; i++) { if (array[i] > max) { max = array[i]; } } return max; } int imin(int array[], int count) { int min = array[0]; for (int i = 1; i < count; i++) { if (array[i] < min) { min = array[i]; } } return min; } int main() { int count; int a[100]; cin >> count; for (int i = 0; i < count; i++) { cin >> a[i]; } int max = imax(a, count); int min = imin(a, count); cout << max << endl; cout << min << endl; return 0; }
#include<iostream> #include<cmath> using namespace std; int main() { int i, j ; int k = 0; for (i = 100; i <= 200; i++) { for (j = 2; j < i; j++) if (i % j == 0)break; if (j == i) { cout << '\t' << i; k++; if (k % 5 == 0)cout << endl; } } system("pause"); return 0; }
#include<iostream> using namespace std; int main() { char str1[] = "I love CHINA!", str2[20], * p1, * p2; p1 = str1; p2 = str2; for (; *p1!= '\0'; p1++, p2++) *p2 = *p1; *p2 = '\0'; p1 = str1; p2 = str2; cout << "str1 is:" << p1 << endl; cout << "str2 is:" << p2 << endl; return 0; }
#include<iostream> using namespace std; void main() { char a[10] = "fdadegda"; int i, j; for (i = j = 0; a[i] != '\0'; i++) if (a[i] != 'a') a[j++] = a[i]; a[j ]= '\0'; cout << a << endl; }
#include <iostream> using namespace std; struct Student { char num[13]; char name[10]; int cpp; int math; int english; int grade; double average; }; const int N = 5; int main() { int i, j, k; Student stu[N]; for (i = 0; i < N; i++) { cin >> stu[i].num >> stu[i].name >> stu[i].cpp >> stu[i].math >> stu[i].english; stu[i].grade = stu[i].cpp + stu[i].math + stu[i].english; stu[i].average = stu[i].grade / 3.0; } cout << "学号\t姓名\t总分\t均分" << endl; for (i = 0; i < N; i++) cout << stu[i].num << '\t' << stu[i].name << '\t' << stu[i].grade << '\t' << stu[i].average << endl; return 0; }
#include<iostream> using namespace std; void reset(int* a, int* b) { if ((*a + *b) % 2 != 0) *a = *b = (*a + *b) / 2 + 1; else *a = *b = (*a + *b) / 2; } int main() { int x, y; cin >> x >> y; reset(&x, &y); cout << x << " " << y << endl; return 0; }
#include <iostream> using namespace std; struct Student { char num[13]; char name[10]; int cpp; int math; int english; int grade; double average; }; const int N = 5; int main() { int i, j, k; Student stu[N]; for (i = 0; i < N; i++) { cin >> stu[i].num >> stu[i].name >> stu[i].cpp >> stu[i].math >> stu[i].english; stu[i].grade = stu[i].cpp + stu[i].math + stu[i].english; stu[i].average = stu[i].grade / 3.0; } cout << "学号\t姓名\t总分\t均分" << endl; for (i = 0; i < N; i++) cout << stu[i].num << '\t' << stu[i].name << '\t' << stu[i].grade << '\t' << stu[i].average << endl; return 0; }