1<=a<=9 0<=b,c<=9
我现在输入一个数n(n<=2000)
问 abc + acc = n 有多少种可能
输出要求:第一行一个数 x 代表可能的数量
接下来 x 行 ,每行输出 abc acc(如果有多组数组按照abc升序输出)
例子
输入
452
输出
1
241 211
#include <bits/stdc++.h> #define maxn 1005 #define ll long long using namespace std; int a[maxn], b[maxn]; int main(){ int n, part1, part2; while(scanf("%d",&n)!=EOF){ int ans = 0; for(int i = 1 ; i <= 9; i++){ for(int j = 0; j <= 9; j++){ for(int k = 0; k <= 9; k++){ if(i == j || j == k || i == k) continue; part1 = i * 100 + j * 10 + k; part2 = i * 100 + k * 10 + k; if(part1 + part2 == n){ a[ans] = part1; b[ans++] = part2; } } } } printf("%d\n",ans); for(int i = 0; i< ans;i++){ printf("%d %d\n",a[i], b[i]); } } return 0; }输出斐波那契数列
输入一个 n 代表矩形 n x n
例子:
输入
3
输出
34 21 13
1 1 8
2 3 5
输入
4
输出
987 610 377 233
5 3 2 144
8 1 1 89
13 31 34 55
#include <bits/stdc++.h> #define maxn 100 #define ll unsigned long long using namespace std; ll a[maxn]; int n; void init(){ a[1] = 1, a[2] = 1; for(int i = 3 ; i <= 81 ; i++){ a[i] = a[i - 1] + a[i - 2]; } } bool check(int x, int y){ if(x>=1 && x <= n && y >= 1 && y <= n) return true; return false; } ll ans[15][15]; bool mark[15][15]; int posi[][2] = {0,1,-1,0,0,-1,1,0}; int posiN[][2] = {0,-1,1,0,0,1,-1,0}; int main(){ init(); while(scanf("%d",&n)!=EOF){ memset(mark, false, sizeof(mark)); if(n % 2 == 0){ int beginX = n / 2 + 1; int beginY = n / 2; int pointer = 0; int k = 1; mark[beginX][beginY] = true; ans[beginX][beginY] = 1; while(beginX != 1 || beginY != 1){ beginX += posi[pointer][0]; beginY += posi[pointer][1]; if(mark[beginX][beginY] == false){ if(check(beginX, beginY)){ mark[beginX][beginY] = true; ans[beginX][beginY] = a[++k]; pointer+=1; pointer%=4; }else{ pointer+=1; pointer%=4; } }else{ beginX -= posi[pointer][0]; beginY -= posi[pointer][1]; pointer+=3; pointer%=4; } } }else{ int beginX = n / 2 + 1; int beginY = n / 2 + 1; int pointer = 0; int k = 1; mark[beginX][beginY] = true; ans[beginX][beginY] = 1; while(beginX != 1 || beginY != 1){ beginX += posiN[pointer][0]; beginY += posiN[pointer][1]; if(mark[beginX][beginY] == false){ if(check(beginX, beginY)){ mark[beginX][beginY] = true; ans[beginX][beginY] = a[++k]; pointer+=1; pointer%=4; }else{ pointer+=1; pointer%=4; } }else{ beginX -= posiN[pointer][0]; beginY -= posiN[pointer][1]; pointer+=3; pointer%=4; } } } for(int i = 1 ; i <= n; i++){ for(int j = 1 ; j <= n; j++){ if(j == n){ cout<<ans[i][j]<<endl; }else{ cout<<ans[i][j]<<" "; } } } } return 0; }
全部评论
(2) 回帖