1. 程式人生 > >Friend Number II(簡單思路題)

Friend Number II(簡單思路題)

Given a positive integer x, let S(x) denotes the sum of all x’s digits. Two integers x and y are friend numbers if S(x)=S(y). Here comes the problem: Given a positive integer x, of course it has a lot of friend numbers, find the smallest one which is greater than x,please.

Input

There are multiple test cases. The first line of input is an integer T (0

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f3f3f3f3f
const int maxn=2009; #define mem(aa,bb) memset(aa,bb,sizeof(aa)) #define PI acos(-1.0) int cun[maxn]; char a[maxn]; int main() { int len; int t; scanf("%d",&t); while(t--) { scanf("%s",a+1); int la=strlen(a+1); len=0; a[0]='0';//加一個前導零 int i; for
(i=la; i>=1&&a[i]=='0'; i--)//把0都放入陣列,等待排序 cun[len++]=0; if(a[i-1]!='9')//如果i-1位不是9 { for(int j=1; j<i-1; j++) printf("%c",a[j]); printf("%d",(a[i-1]-'0')+1); cun[len++]=(a[i]-'0'); } else//如果是9 { for(; i>0; i--)//找第一個不是9的數字 { if(a[i-1]!='9'&&a[i]=='9') { a[i-1]=((a[i-1]-'0')+1)+'0'; cun[len++]=(a[i]-'0'); break; } else cun[len++]=a[i]-'0'; } if(a[0]!='0')//如果找到了前導零的位置 printf("%c",a[0]); for(int j=1; j<i; j++) printf("%c",a[j]); } sort(cun,cun+len);//從小到大排序 int flag=1; for(int j=0; j<len; j++) if(flag&&cun[j]!=0) printf("%d",cun[j]-1),flag=0;//因為上面加一了,所以這裡要減一 else printf("%d",cun[j]); printf("\n"); } return 0; }

相關推薦

Friend Number II簡單思路

Given a positive integer x, let S(x) denotes the sum of all x’s digits. Two integers x and y are friend numbers if S(x)=S(y). Her

HDU 6124 17多校7 Euler theorem簡單思維

panel please problem urn return bottom -1 fan std Problem Description HazelFan is given two positive integers a,b, and he wants to calcu

HihoCoder1052基因工程簡單模擬

修改方法 ace lib stream 方法 div 最小 mes 大寫 描述 小Hi和小Ho正在進行一項基因工程實驗。他們要修改一段長度為N的DNA序列,使得這段DNA上最前面的K個堿基組成的序列與最後面的K個堿基組成的序列完全一致。 例如對於序列"ATCGA

Fibonacci Again 簡單規律

There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).  Input Input consists of a sequence of

Climbing Worm HDU - 1049 簡單模擬

#include <stdio.h> int main() {     int n, u, d;     while(~scanf("%d %d %d", &n, &u, &d)) &nbs

PAT-PAT (Advanced Level) Practise 1005. Spell It Right (20) 簡單模擬【一星級】

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English. Input Specification:

Harmonic Number (II)模擬找規律

題目來源:https://cn.vjudge.net/problem/LightOJ-1245 【題意】 求前n項和,每一項是n/i; 【思路】 分解單個數,可以找到規律,舉例說明。 當n為10時: i ___1 2 3 4 5

2019浙大校賽--G--Postman簡單思維

src += std amp 畫圖 long div scanf fin 一個思維水題 題目大意為,一個郵遞員要投遞N封信,一次從郵局來回只能投遞K封。求最短的投遞總距離。需註意,最後一次投遞後無需返回郵局。 本題思路要點: 1、最後一次投遞無需返回郵局,故最後一

[LeetCode] 137. Single Number II 位運算

清零 一次 .com blog 適用於 soft hat ever rip 傳送門 Description Given an array of integers, every element appears three times except for one, w

【HDOJ】find your present (2)思路

利用 自己 names namespace pro ble get nbsp tdi find your present (2) 題意:找有唯一一個出現奇數次的數 思路:利用位異或。一個數異或自己本身是0; #include<stdio.h> int

Yin and Yang Stones思路

enc last cas black ear ive close script test Problem Description: A mysterious circular arrangement of black stones and white stones has

BetThe 2016 ACM-ICPC Asia China-Final Contest 思路

suppose spec case cpc mon 輸入 under strong ces 題目:   The Codejamon game is on fire! Fans across the world are predicting and betting on wh

Kattis - missinggnomesD Missing Gnomes 思路

題目: 題意: 給出已經去除了幾個數的一個序列,任務是將去除的數字插回去補全這個序列,輸出字典序排在第一的那個補全的序列。 例如: 樣例輸入: 5 3 1 4 2 樣例輸出: 1 3 4 2 5 思路: 用陣列存一下給出的序列,用一個佇列存一下被去除的數字的序列(由小到大排列),然後分

簡單的列舉人機互動

Example input 2 1 0 1 2 output 01 11 10 00 思路:先利用mt19937這個隨機數,隨機出正確n/2的字串。 接著,就是比較精妙的地方,用O(n)的複雜度猜出完整的字串,設我們隨機出來正確n/2的字串是s.

Leetcode題解系列——264. Ugly Number IIc++版

題目連結:264. Ugly Number II 題目大意:輸出第n個順序的醜數,醜數的定義為一個數的質因子只能是2,3,5. 例子: 6是醜數,6 = 2 * 3 8是醜數,8 = 2 * 2 * 2 14不是醜數, 14 = 7 * 2. 包含了7

POJ 2392 Space Elevator 多重揹包 + 思路

題目連結:http://poj.org/problem?id=2392 Space Elevator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7783 Accepted: 3690

LeetCode 137 Single Number II只出現一次的數字 II*

翻譯 給定一個整型陣列,除了某個元素外其餘的均出現了三次。找出這個元素。 備註: 你的演算法應該是線性時間複雜度。你可以不用額外的空間來實現它嗎? 原文 Given an array

bzoj4719 [Noip2016]天天愛跑步樹+lca+樹上差分+思路

進入bzoj法眼的noip(lus)題hh。題解太麻煩啦。。。不寫啦。。。就體會一下思想:要是對於每條路徑操作,看他會影響哪些點的貢獻,鐵鐵的會t。所以考慮怎樣的一條路徑會對一個點產生貢獻,先討論簡化版的鏈的情況,發現要討論左右。結合另外兩個部分分,感覺就是把路

HDU 1711 Number Sequence (KMP找子串第一次出現的位置基礎模板

題意:給出若干個樣例,每個樣例包括兩個字串,如果第二個字串是第一個字串的子串,則求他第一次出現的位置,不是子串的話輸出-1. 思路:kmp演算法的模板題 對nexts陣列如何構造解釋: 根據定義ne

簡單的程式碼讓turtlebot動起來II使用kobuki底座走三角形

接著上一篇turtlebot的直行和旋轉。 ROS中,節點之間通過訊息溝通,主題相當於是對訊息進行路由和管理的匯流排。節點向主題釋出訊息即表示一個節點在傳送資料;節點訂閱某個主題即可以接受其他節點的訊息。在上一篇的直行和旋轉當中,我們只寫一個publisher,僅僅有話題