输入的第一行包含一个整数
,表示询问的数量。
接下来
行,第
行包含两个整数
——本次询问的参数。
对于每个询问,输出一行一个整数,表示答案。
4 2 3 5 17 8 2 4 4
1 2 2 4
以第一组询问
为例:
订单编号序列为
,第
个编号为
,故输出
。
其余询问均可按相同规则得到答案。
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int q = in.nextInt(); while (q-- != 0) { int m = in.nextInt(), x = in.nextInt(); System.out.println((x - 1) % m + 1); } } }
直接取余判断:
#include #include #include using namespace std; int q,m,x; int main() { cin>>q; while(q--){ scanf("%d%d", &m,&x); //如果不能整除,x要么就是小于m,要么不是m的整数倍 if(x % m != 0){ printf("%d\n", x % m); }else { //x是m的整数倍,直接取m printf("%d\n", m); } } return 0; } // 64 位输出请用 printf("%lld")
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); while (in.hasNextInt()) { int left = in.nextInt(); int right = in.nextInt(); if(right > left){ int temp = right % left; System.out.println(temp == 0 ? left: temp); }else{ System.out.println(right); } } } }
#include <iostream> using namespace std; void solve() { int m, x; cin >> m >> x; cout << (x - 1) % m + 1 << endl; } int main() { int q; cin >> q; while(q--) { solve(); } } // 64 位输出请用 printf("%lld")