解题思路:
分奇偶两种情况考虑。
注意事项:
参考代码:
#include <stdio.h> #include <iostream> using namespace std; int gcd(int a, int b){ int t; for(int i = 1; i <= a && i <= b; i++){ if(a%i == 0 && b % i == 0){ t = i; } } return t; } int main(){ int n; scanf("%d", &n); for(int i = 0; i < n; i++){ int l; scanf("%d", &l); if(l % 2 == 0){ printf("4\n"); }else{ int max = 1; int s1; for(int j = 1; j <= l/2; j++){ if(max <= gcd(j, l-j)){ s1 = j; max = gcd(j, l-j); } } int res = 2; int x = l-s1, y = s1; while(x > 0 && y > 0){ if(x > y){ res += 2 * (x/y); x %= y; }else{ res += 2 * (y/x); y %= x; } } printf("%d\n", res); return 0; } } }
0.0分
0 人评分
点我有惊喜!你懂得!浏览:2212 |
C语言训练-计算1~N之间所有奇数之和 (C语言代码)浏览:720 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:632 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:930 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:735 |
简单的a+b (C语言代码)浏览:489 |
printf基础练习2 (有点不明白)浏览:837 |
C语言程序设计教程(第三版)课后习题6.8 (C++代码)浏览:575 |
1012题解浏览:861 |
C语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:672 |