赞
踩
作者:莫启飞
环境:vs2019
Problem Description
已知一个数列,它的循环周期为30,且前30项的值分别为0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1。现在给定该数列的前两项,你能计算得到第三项的值吗?
Input
第一行数据组数T(T <= 30),对于每组数据两个整数a, b(0<= a, b<=15),表示前两项的大小。
Output
每组数据占一行,表示第三项的值。
Sample Input
2 0 1 11 10Sample Output
2 9
//代码1 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> const int A[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; int T; int a, b; int main(void) { scanf("%d", &T); while (T--) { scanf("%d%d", &a, &b); for (int i = 0;; ++i) { if (A[i] == a && A[i + 1] == b) { printf("%d\n", A[i + 2]); break; } } } return 0; } 代码2: #include <iostream> using namespace std; int main() { int T; cin >> T; int a, b, flag; while (T--) { cin >> a >> b; if (a < b) { flag = (b == 15) ? -1 : 1; } if (a > b) { flag = (b == 0) ? 1 : -1; } cout << b + flag << endl; } return 0; } 我的错误代码; #include<iostream> using namespace std; const int arr[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1 }; void sum() { int a, b; cin >> a; cin >> b; if (a < b) { cout << arr[b + 1] << endl; } else if(a>b){ cout << arr[a + 1 ] << endl; } } int main() { int n; cin >> n; while (n--) { sum(); } cout << endl; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。