解题思路:
分奇偶两种情况考虑。
注意事项:
参考代码:
#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 人评分
剔除相关数 (C语言代码)浏览:1015 |
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:541 |
C语言训练-求函数值 (C语言代码)浏览:580 |
C语言训练-数字母 (C语言代码)浏览:649 |
WU-链表数据求和操作 (C++代码)浏览:1316 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:543 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:514 |
蚂蚁感冒 (C语言代码)浏览:774 |
简单的a+b (C语言代码)浏览:950 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:476 |